11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 14• AUTHENTICATING YOUR USERSwww.it-ebooks.info• These variables do not function properly with the CGI version of <strong>PHP</strong>.Useful FunctionsTwo standard functions are commonly used when handling authentication via <strong>PHP</strong>: header() andisset(). Both are introduced in this section.Sending HTTP Headers with header()The header() function sends a raw HTTP header to the browser. The header parameter specifies theheader information sent to the browser. Its prototype follows:void header(string header [, boolean replace [, int http_response_code]])The optional replace parameter determines whether this information should replace or accompanya previously sent header. Finally, the optional http_response_code parameter defines a specific responsecode that will accompany the header information. Note that you can include this code in the string, aswill soon be demonstrated. Applied to user authentication, this function is useful for sending the WWWauthentication header to the browser, causing the pop-up authentication prompt to be displayed. It isalso useful for sending the 401 header message to the user if incorrect authentication credentials aresubmitted. An example follows:Note that unless output buffering is enabled, these commands must be executed before any outputis returned. Neglecting this rule will result in a server error because of a violation of the HTTPspecification.Determining if a Variable is Set with isset()The isset() function determines whether a variable has been assigned a value. Its prototype follows:boolean isset(mixed var [, mixed var [,...]])It returns TRUE if the variable contains a value and FALSE if it does not. As applied to userauthentication, the isset() function is useful for determining whether the $_SERVER['<strong>PHP</strong>_AUTH_USER']and $_SERVER['<strong>PHP</strong>_AUTH_PW'] variables are properly set. Listing 14-1 offers an example.Listing 14-1. Using isset() to Verify Whether a Variable Contains a Value

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!