Welcome, ! You are currently logged in.
Logout
Username Password A login/logout system error { background: #d33; color: white; padding: 0.2em; } A login/logout system 293 Part III: Using PHP in Practice Figure 10-4 Figure 10-5 How It Works The script starts by creating a new session (or picking up an existing one) with session_start() Then it defines a couple of constants, USERNAME and PASSWORD, to store the predefined login details (In a real Web site you would probably store a separate username and password for each user in a database table or text file.) session_start(); define( “USERNAME”, “john” ); define( “PASSWORD”, “secret” ); Next the script calls various functions depending on user input If the Login button in the login form was clicked, the script attempts to log the user in Similarly, if the Logout link was clicked, the user is 294 Chapter 10: Preserving State With Query Strings logged out If the user is currently logged in, the welcome message is shown; otherwise the login form is displayed: if ( isset( $_POST[“login”] ) ) { login(); } elseif ( isset( $_GET[“action”] ) and $_GET[“action”] == “logout” ) { logout(); } elseif ( isset( $_SESSION[“username”] ) ) { displayPage(); } else { displayLoginForm(); } The login() function validates the username and password and, if correct, sets a session variable, $_SESSION[“username“], to the logged-in user ’s username This serves two purposes: it indicates to the rest of the script that the user is currently logged in, and it also stores the user ’s identity in the form of the username (In a multi-user system this would allow the site to identify which user is logged in.) The function then reloads the page However, if an incorrect username or password was entered, the login form is redisplayed with an error message: function login() { if ( isset( $_POST[“username”] ) and isset( $_POST[“password”] ) ) { if ( $_POST[“username”] == USERNAME and $_POST[“password”] == PASSWORD ) { $_SESSION[“username”] = USERNAME; session_write_close(); header( “Location: login.php” ); } else { displayLoginForm( “Sorry, that username/password could not be found Please try again.” ); } } } The logout() function simply deletes the $_SESSION[“username“] element to log the user out, then reloads the page: function logout() { unset( $_SESSION[“username”] ); session_write_close(); header( “Location: login.php” ); } The final three functions are fairly self-explanatory displayPage() displays the welcome message, along with the Logout link displayLoginForm() displays the login page, optionally displaying an error message Both these functions use a utility function, displayPageHeader(), to display the markup for the page header that is common to both pages 295 Part III: Using PHP in Practice Summary PHP scripts start to become much more useful when they can store data on a semi-permanent basis In this chapter, you learned how to use three different techniques — query strings, cookies, and sessions — to store data related to a particular user between page requests: ❑ Query strings are simple to understand and use, but they are not at all secure so they’re best used for transmitting innocuous information You learned how to build query strings with urlencode() and http_build_query(), as well as how to read data from query strings, and you created a simple example that uses query strings to create a paged display ❑ Cookies are a step up from query strings, because you don’t have to pass data between every single page request Cookies can even persist when the browser is closed and reopened You looked at the anatomy of a cookie, and learned how to create cookies, read cookies via the $_ COOKIE superglobal, and delete cookies You also wrote a script that uses cookies to remember details about a visitor ❑ Sessions have a couple of major advantages over cookies: they’re more secure, and they don’t involve sending potentially large amounts of data to the server each time a page is viewed You explored PHP’s built-in session-handling functionality, including session_start(), the $_ SESSION superglobal, session_write_close(), and session_destroy() You learned that, though not advisable, you can pass session IDs in query strings in situations where the browser doesn’t support cookies, and you looked at some ways to fine-tune PHP’s session behavior Finally, you used sessions to create a simple shopping cart and user login/logout system Now that you know how to save state, you can start to write more powerful, persistent Web applications that can remember session information between page views In the next chapter you look at how to access the Web server ’s file system from within your PHP scripts This means that you can store application data and other information in files on the server ’s hard drive, further expanding the capabilities of your Web applications Before you leave this chapter, take a look at the following two exercises, which test your knowledge of cookie and session handling in PHP You can find the solutions to these exercises in Appendix A Exercises 296 Write a script that uses cookies to remember how long ago a visitor first visited the page Display this value in the page, in minutes and seconds In Chapter you created a three-step registration form using hidden form fields Rewrite this script to use sessions to store the entered form data, so users can come back to the form at another time and continue where they left off Remember to erase the entered data from the session once the registration process has been completed 11 Working with Files and Directories As a server-side programming language, PHP allows you to work with files and directories stored on the Web server This is very useful, because it means your PHP scripts can store information outside the scripts themselves Files are stored in directories on a hard drive, and because they retain their data after the computer is shut down, they are a persistent storage mechanism, instead of temporary storage such as RAM Directories are a special kind of file made for storing other files Directories are created hierarchically inside other directories, starting with the root (top-level) directory and proceeding down from there Files can contain any kind of data, and also can contain quite a bit of information about themselves, such as who owns them and when they were created PHP makes working with the file system easy by including functions that allow you to obtain information about files, as well as open, read from, and write to them This chapter is all about the PHP functions for working with the file system You learn: ❑ More about files and directories, and how to find out more information about them in your scripts ❑ How to open and close files, as well as how to read data from, and write data to, files ❑ The concept of file permissions and how to work with them ❑ How to copy, move, and delete files ❑ All about working with directories, including reading their contents, creating them, and deleting them As well as learning the theory of file and directory handling, you get to write a script that can move through a directory tree, listing all the files and directories it finds as it goes You also create a simple Web-based text editor to illustrate many of the points covered in the chapter Part III: Using PHP in Practice Understanding Files and Directories Everything on your hard drive is stored as a file of one kind or another, although most folks think in terms of files and directories There are ordinary program files, data files, files that are directories, and special files that help the hard drive keep track of the contents of folders and files PHP has functions that can work with any file type, but typically you’ll be working with text files that contain data The terms “directory” and “folder” are used interchangeably in this book (and in the PHP community); they mean exactly the same thing A file is nothing more than an ordered sequence of bytes stored on a hard disk or other storage media A directory is a special type of file that holds the names of the files and directories inside the folder (sometimes denoted as subdirectories or subfolders) and pointers to their storage areas on the media Many differences exist between UNIX-based and Windows operating systems, one of them being the way directory paths are specified UNIX-based systems such as Linux use slashes to delimit elements in a path, like this: /home/matt/data/data.txt Windows uses backslashes: C:\MyDocs\data\data.txt Fortunately, PHP on Windows automatically converts the former to the latter in most situations, so you can safely use slashes in your script, regardless of the operating system that the script is running on Occasionally, though, backslashes are necessary In this situation, you need to use two backslashes in a row, because PHP interprets a backslash as escaping the following character: “C:\\MyDocs\\data\\data.txt” Getting Information on Files PHP provides some functions that enable you to access useful file information For example, you can use file_exists() to discover whether a file exists before attempting to open it: file_exists( “/home/chris/myfile.txt” ) file_exists() returns true if the file at the specified path exists, or false otherwise In a similar fashion, you can use the filesize() function to determine the size of a file on the hard disk Just as with file_exists(), this function takes a filename as an argument: filesize( “/home/chris/myfile.txt” ) This returns the size of the specified file in bytes, or false upon error 298 Chapter 11: Working with Files and Directories Time-Related Properties Besides their contents, files have other properties that can provide useful information The available properties depend on the operating system in which the files are created and modified On UNIX platforms such as Linux, for example, properties include the time the file was last modified, the time it was last accessed, and the user permissions that have been set on the file PHP provides three time-related file functions: ❑ fileatime() — Returns the time at which the file was last accessed as a UNIX timestamp A file is considered accessed if its contents are read ❑ filectime() — Returns the time at which the file was last changed as a UNIX timestamp A file is considered changed if it is created or written, or when its permissions have been changed ❑ filemtime() — Returns the time at which the file was last modified as a UNIX timestamp The file is considered modified if it is created or has its contents changed A UNIX timestamp is an integer value indicating the number of seconds between the UNIX epoch (midnight on January 1, 1970) and the specified time and date The getdate() function is very useful when working with UNIX timestamps It returns an associative array containing the date information present in a timestamp The array includes such values as the year, the month, the day of the month, and so on For example, you can set a variable such as $myDate to the value returned by getdate(), and then access the month component with $myDate[“month“] Find out more about working with dates and times in Chapter 16 Retrieving a Filename from a Path It’s often very useful to be able to separate a filename from its directory path, and the basename() function does exactly that, taking a complete file path and returning just the filename For example, the following code assigns index.html to $filename: $filename = basename( “/home/james/docs/index.html” ); You can specify a directory path instead, in which case the rightmost directory name is returned Here’s an example that assigns the value docs to $dir: $dir = basename( “/home/james/docs” ); Basically, basename() retrieves the last whole string after the rightmost slash If you don’t want the filename extension, or suffix, you can strip that off too by supplying the suffix as a second argument to basename() The following example assigns “myfile” to $filename: $filename = basename( “/home/james/docs/myfile.doc”, “.doc” ); 299 Part III: Using PHP in Practice Opening and Closing Files Usually, to work with a file from within your PHP script, you first need to open the file When you open a file, you create a file handle A file handle is a pointer associated with the open file that you can then use to access the file’s contents When you’ve finished with the file, you close it, which removes the file handle from memory File handles are resource data types Data types were covered in Chapter Some PHP functions let you work directly with a file without needing to open or close it You read about these later in the chapter In the next sections you look at opening files with the fopen() function, and closing files with fclose() Opening a File with fopen() The fopen() function opens a file and returns a file handle associated with the file The first argument passed to fopen() specifies the name of the file you want to open, and the second argument specifies the mode, or how the file is to be used For example: $handle = fopen( “./data.txt”, “r” ); The first argument can be just a filename (“data.txt“), in which case PHP will look for the file in the current directory, or it can be a relative (”./data.txt“) or absolute (“/myfiles/data.txt“) path to a file You can even specify a file on a remote Web or FTP server, as these examples show: $handle = fopen( “http://www.example.com/index.html”, “r” ); $handle = fopen( “ftp://ftp.example.com/pub/index.txt”, “r” ); A remote file can only be opened for reading — you can’t write to the file If you’re not familiar with command-line file operations, you might be a little confused by the concept of a current directory and the relative path notation Usually, the current directory is the same directory as the script, but you can change this by calling chdir() This is covered later in the chapter Within a relative path, a dot (.) refers to the current directory, and two dots ( ) refer to the immediate parent directory For example, /data.txt points to a file called data.txt in the current directory, and /data.txt points to a file called data.txt in the directory above the current directory / / /data.txt backs up the directory tree three levels before looking for the data.txt file Meanwhile, an absolute path is distinguished by the fact that it begins with a / (slash), indicating that the path is relative to the root of the file system, not to the current directory For example, /home/chris/website/index.php is an absolute path 300 Chapter 11: Working with Files and Directories The second argument to fopen() tells PHP how you’re going to use the file It can take one of the following string values: Value Description r Open the file for reading only The file pointer is placed at the beginning of the file r+ Open the file for reading and writing The file pointer is placed at the beginning of the file w Open the file for writing only Any existing content will be lost If the file does not exist, PHP attempts to create it w+ Open the file for reading and writing Any existing file content will be lost If the file does not exist, PHP attempts to create it a Open the file for appending only Data is written to the end of an existing file If the file does not exist, PHP attempts to create it a+ Open the file for reading and appending Data is written to the end of an existing file If the file does not exist, PHP attempts to create it The file pointer is PHP’s internal pointer that specifies the exact character position in a file where the next operation should be performed You can also append the value b to the argument to indicate that the opened file should be treated as a binary file (this is the default setting) Alternatively, you can append t to treat the file like a text file, in which case PHP attempts to translate end-of-line characters from or to the operating system’s standard when the file is read or written For example, to open a file in binary mode use: $handle = fopen( “data.txt”, “rb” ); Although this flag is irrelevant for UNIX-like platforms such as Linux and Mac OS X, which treat text and binary files identically, you may find the text mode useful if you’re dealing with files created on a Windows computer, which uses a carriage return followed by a line feed character to represent the end of a line (Linux and the Mac just use a line feed) That said, binary mode is recommended for portability reasons If you need your application’s data files to be readable by other applications on different platforms, you should use binary mode and write your code to use the appropriate end-of-line characters for the platform on which you are running (The PHP constant PHP_EOL is handy for this; it stores the end-of-line character(s) applicable to the operating system that PHP is running on.) 301 Part III: Using PHP in Practice By default, if you specify a filename that isn’t a relative or absolute path (such as “data.txt“), PHP just looks in the current (script) directory for the file However, you can optionally pass the value true as a third argument to fopen(), in which case PHP will also search the include path for the file Find out more about include paths in Chapter 20 If there was a problem opening the file, fopen() returns false rather than a file handle resource Operations on files and directories are prone to errors, so you should always allow for things to go wrong when using them It’s good practice to use some form of error-checking procedure so that if an error occurs (perhaps you don’t have necessary privileges to access the file, or the file doesn’t exist), your script will handle the error gracefully For example: if ( !( $handle = fopen( “./data.txt”, “r” ) ) ) die( “Cannot open the file” ); Rather than exiting with die(), you might prefer to raise an error or throw an exception Find out more about error handling in Chapter 20 Closing a File with fclose() Once you’ve finished working with a file, it needs to be closed You can this using fclose(), passing in the open file’s handle as a single argument, like this: fclose( $handle ); Although PHP should close all open files automatically when your script terminates, it’s good practice to close files from within your script as soon as you’re finished with them because it frees them up quicker for use by other processes and scripts — or even by other requests to the same script Reading and Writing to Files Now that you know how to open and close files, it’s time to take a look at reading and writing data in a file In the following sections you learn about these functions: ❑ ❑ fwrite() — Writes a string of characters to a file ❑ fgetc() — Reads a single character at a time ❑ feof() — Checks to see if the end of the file has been reached ❑ fgets() — Reads a single line at a time ❑ fgetcsv() — Reads a line of comma-separated values ❑ file() — Reads an entire file into an array ❑ 302 fread() — Reads a string of characters from a file file_get_contents() — Reads an entire file into a string without needing to open it ... from MySQL with PHP Chapter 14: Manipulating MySQL Data with PHP Chapter 15: Making Your Job Easier with PEAR Chapter 16: PHP and the Outside World Chapter 17: Generating Images with PHP Chapter... name< ?php echo $_POST[“firstName”]?> Last name< ?php echo $_POST[“lastName”]?> Password< ?php echo $_POST[“password1”]?> 241 Part III: Using PHP in Practice... value=”superWidget”< ?php setSelected( “favoriteWidget”, “superWidget” ) ?>>The SuperWidget 244 Chapter 9: Handling HTML Forms with PHP