Using HTTP Basic Authentication

March 8th, 2008

You want to use PHP to protect parts of your web site with passwords. Instead of
storing the passwords in an external file and letting the web server handle the
authentication, you want the password verification logic to be in a PHP program.

Solution
The $_SERVER[’PHP_AUTH_USER’] and $_SERVER[’PHP_AUTH_PW’] global variables contain
the username and password supplied by the user,if any. To deny access to a
page,send a WWW-Authenticate header identifying the authentication realm as part of
a response with status code 401:

header('WWW-Authenticate: Basic realm="My Website"');
header('HTTP/1.0 401 Unauthorized');
echo "You need to enter a valid username and password.";
exit;

Read the rest of this entry »

Hiding Error Messages from Users

March 7th, 2008

Set the following values in your php.ini or web server configuration file:
display_errors =off
log_errors =on
These settings tell PHP not to display errors as HTML to the browser but to put
them in the server’s error log.
When log_errors is set to on,error messages are written to the server’s error log. If
you want PHP errors to be written to a separate file,set the error_log configuration
directive with the name of that file:
error_log = /var/log/php.error.log

Read the rest of this entry »

Compressing Web Output with gzip

March 6th, 2008

You want to send compressed content to browsers that support automatic decompression.

Solution
Add this setting to your php.ini file:

zlib.output_compression=1

Discussion
Browsers tell the server that they can accept compressed responses with the Accept-Encoding header. If a browser sends Accept-Encoding: gzip or Accept-Encoding: deflate,and PHP is built with the zlib extension,the zlib.output_compression configuration directive tells PHP to compress the output with the appropriate algorithm before sending it back to the browser. The browser uncompresses the data before displaying it. You can adjust the compression level with the zlib.output_compression_level configuration directive:

; minimal compression
zlib.output_compression_level=1
; maximal compression
zlib.output_compression_level=9

At higher compression levels,less data needs to be sent from the server to the browser, but more server CPU time must be used to compress the data.

Using a Custom Error Handler

March 5th, 2008

You want to create a custom error handler that lets you control how PHP reports errors.
Solution
To set up your own error function, use set_error_handler():

set_error_handler('pc_error_handler');
function pc_error_handler($errno, $error, $file, $line) {
$message = "[ERROR][$errno][$error][$file:$line]";
error_log($message);
}

Discussion

A custom error handling function can parse errors based on their type and take the appropriate action. See Table 8-2 in Recipe 8.15 for a list of error types. Pass set_error_handler() the name of a function,and PHP forwards all errors to that function. The error handling function can take up to five parameters. The first parameter is the error type,such as 8 for E_NOTICE. The second is the message thrown by the error,such as “Undefined variable: html”. The third and fourth arguments are the name of the file and the line number in which PHP detected the error. The final parameter is an array holding all the variables defined in the current scope and their values.
For example,in this code $html is appended to without first being assigned an initial value: Read the rest of this entry »

Flushing Output to the Browser

March 4th, 2008

You want to force output to be sent to the browser. For example,before doing a
slow database query, you want to give the user a status update.
Solution
Use flush():

print 'Finding identical snowflakes...';
flush();
$sth = $dbh->query('SELECT shape,COUNT(*) AS c FROM snowflakes GROUP BY shape HAVING c > 1');

The flush() function sends all output that PHP has internally buffered to the web
server,but the web server may have internal buffering of its own that delays when
the data reaches the browser. Additionally,some browsers don’t display data immediately
upon receiving it,and some versions of Internet Explorer don’t display a page
until they’ve received at least 256 bytes. To force IE to display content,print blank
spaces at the beginning of the page:

print str_repeat(' ',300);
print 'Finding identical snowflakes...';
flush();
$sth = $dbh->query(
'SELECT shape,COUNT(*) AS c FROM snowflakes GROUP BY shape HAVING c > 1');

See Also documentation on flush()

JavaScript

March 3rd, 2008

JavaScript is a scripting language most often used for client-side web development. It was the originating dialect of the ECMAScript standard. It is a dynamic, weakly typed, prototype-based language with first-class functions. JavaScript was influenced by many languages and was designed to have a similar look to Java, but be easier for non-programmers to work with. The language is best known for its use in websites (as client-side JavaScript), but is also used to enable scripting access to objects embedded in other applications (for example Microsoft Gadgets in Windows Vista Sidebar). Read the rest of this entry »

PHP: Hypertext Preprocessor

March 3rd, 2008

PHP (PHP: Hypertext Preprocessor) is a computer scripting language originally designed for producing dynamic web pages. It is used mainly in server-side scripting, but can be used from a command line interface or in standalone graphical applications.

PHP was written as a set of Common Gateway Interface (CGI) binaries in the C programming language by the Danish/Greenlandic programmer Rasmus Lerdorf in 1994, to replace a small set of Perl scripts he had been using to maintain his personal homepage. Lerdorf initially created PHP to display his résumé and to collect certain data, such as how much traffic his page was receiving. Personal Home Page Tools was publicly released on 8 June 1995 in order to speed up the process of finding bugs and to help improve the code more quickly, after Lerdorf combined it with his own Form Interpreter to create PHP/FI (this release is considered PHP version 2), which had more functionality, including a much larger C implementation which was used to communicate with databases, and helped developers to build simple, dynamic web applications. At this point, PHP already included some of the basic functionality that exist in PHP today, such as Perl-like variables, form handling, and the ability to embed HTML. The syntax was built to be similar to Perl, but was more limited, simple, and less consistent in comparison.

PHP is currently the most popular Apache module among all servers using Apache as a web server. Among all currently existing computer programming languages, it is considered the fourth most popular, ranked only behind Java, C, and Visual Basic