Sample PHP Scripts

This page lists some sample standalone PHP scripts illustrating core language principles, as well as some PHP scripts that have associated HTML and other files. There are several groupings: core language, system and web info, get and post, forms and their submission and processing, file-handling, cookies, sessions, general database, database table creation, a simple music database, and database-based user authentication. Many of these sample files are modified and updated versions of examples from the various editions of a text on PHP, MySQL and Apache by Julie Meloni. Note that sometimes the first link will not run the script, for various reasons such as the access required or what it might do, and there will be a message that you should make a copy of the script for your own account and run it there.

When clicking on any of the "text" links below, you should see the text of the associated .php file, which will run and show you its output if you click on it.


PHP Includes

  1. include.php [ text ] Illustrates the PHP include() function.
  2. require.php [ text ] Illustrates the PHP require() function.
  3. include_with_error.php [ text ] Illustrates the PHP include() function behavior when an error is encountered.
  4. require_with_error.php [ text ] Illustrates the PHP require() function when an error is encountered.

Core Language Group

  1. first.php [ text ] The PHP version of the classic "Hello, world!" program.
  2. error1.php [ text ] Generates a parse error because of a missing semi-colon.
  3. error2.php [ text ] Generates a parse error because of nested double quotes.
  4. error3.php [ text ] Generates a fatal error because of a call to an undefined function.
  5. error4.php [ text ] This example used to generate a notice error because of an undefined variable but with PHP 8 many "notice errors" have been coverted to "warning errors", including this one See the text of this example for a list.
  6. error5.php [ text ] Generates a warning error because of a non-existent include file.
  7. comments.php [ text ] Illustrates PHP comments.
  8. printvars.php [ text ] Displays values of several PHP variables.
  9. heredoc.php [ text ] Illustrates PHP "heredoc" construct.
  10. assignment.php [ text ] Illustrates PHP assignment operations.
  11. arithmetic.php [ text ] Illustrates PHP arithmetic operations.
  12. comparison.php [ text ] Illustrates PHP comparison operations.
  13. logical.php [ text ] Illustrates PHP logical operations.
  14. arrays.php [ text ] Illustrates PHP arrays.
  15. explode_implode.php [ text ] Illustrates the "exploding" of a string into an array of strings, and the "imploding" of an array of strings into a single string.
  16. regular_expressions.php [ text ] Illustrates the use of the PHP preg_match() function to test a regular expression.

System and Web Info Group

  1. phpinfo.php [ text ] Displays a lot of information about your PHP installation. A good script to run to test your installation, but not necessarily one you want every visitor to be able to run.
  2. remote_address.php [ text ] Finds and displays the IP address of your browser location.
  3. user_agent.php [ text ] Identifies and displays more details than you really wanted to know about the browser you are using.
  4. browser_match.php [ text ] Identifies and displays just the name of the browser you are using.
  5. server_id.php [ text ] Shows the username under which the web server is running.
  6. test_SERVER.php [ text ] Shows the values of a number of global constants.

Get and Post Group

  1. test_get.html A form for entering data to illustrate the GET method for submitting form data. [ text of test_get.php, the PHP script that processes the form data submitted. ]
  2. test_post.html A form for entering data to illustrate the POST method for submitting form data. [ text of test_post.php, the PHP script that processes the form data. ]
  3. simple_get.php [ text ] A simple program to illustrate the GET method of passing values to a PHP script at the end of the URL that acesses the script file. To use this script you need to understand the GET method, how values are sent via a URL, and you also need to know that the two (integer) values you want to send are for the variables $first and $second.

Simple Form Input, Submission and Processing Group

  1. calculate.html A form to enter data for calculation. [ text of calculate.php, the PHP script that performs the calculation. ]
  2. string_manipulations.html [ text ] Permits the user to enter some text and choose what to do with that text from several alternative actions.
  3. redirect.html [ text ] Permits the user to choose a web site from a dropdown list, and then redirects the user to that site when the user clicks the Go button.
  4. email_feedback.html [ text ] Permits the user to fill out a simple feedback form and have the form data e-mailed to a recipient.
  5. email_feedback_allinone.php [ text ] Same functionality as the two files in the previous example, but now encapsulated in a single file.

File-Handling Group

The scripts in this group are all related to file-handling in PHP. You should not try to run them here. Instead, download the files to your own account and run them there. In order to run through this sequence of scripts successfully, you should do the following:

In the same directory where this group of files is located, issue the following Linux commands:

mkdir files
chmod 777 files
  1. list_files.php [ text ] Displays an unordered list of all files in the same directory as the script itself.
  2. create_file.php [ text ] Creates an empty file called my_data.txt in the subdirectory called files, which is located in the same directory as the script itself. This script simply overwrites any file with the same name in the same location.
  3. create_file_check_first.php [ text ] Performs the same action as the previous script, except that it checks first for the existence of a file with the same name in the same location and does not create a new file if such a file already exists.
  4. delete_file.php [ text ] Deletes any file called my_data.txt from the subdirectory called files, if that subdirectory is located in the same directory as the script itself. [Exercise: Try calling this script without altering the file files/my_data.txt in any way, and try deleting a second instance of the file after editing it. In this latter case do a long listing of the file before and after the editing and note carefully what you see.]
  5. write_data_to_file.php [ text ] Writes the following three lines, plus another line containing the date when the file was written, to the file files/my_data.txt in the current directory:
    Check it out!
    I've created a new file and
    stuck all this text into it!
    
  6. read_data_from_file.php [ text ] Reads and displays the contents of files/my_data.txt, with a single-line preamble. For example, if the file contains the contents put there by the previous script, the output of this script would be the following four lines, plus a line showing the date when the file was written:
    The file files/my_data.txt contains:
    Check it out!
    I've created a new file and
    stuck all this text into it!
  7. mail_file_contents.php [ text ] Mails the contents of files/my_data.txt to an e-mail address "hard-wired" into the script.
  8. copy_file.php [ text ] Makes a copy of files/my_data.txt and calls it files/my_data.bak, which could serve as a backup for the original file.
  9. rename_file.php [ text ] Renames the file files/my_data.bak to files/my_data.txt, which could be regarded as restoring the file from backup. [Exercise: Try this script with and without editing the files/my_data.bak file, and observe what happens in each case. Compare this exercise with the one involving the delete.php script above.]
  10. upload_file.html [ text of PHP script to process the form data in the accompanying HTML file ] Permits the user to upload a file from the client machine to a files directory on the server.

General Database Group

The scripts in this group are all database-related. They illustrate connecting to an existing database, trying (unsuccessfullyl) to create a new database, trying (unsuccessfully) to remove (or "drop") a database, as well as displaying all accessible databases and their tables.

  1. db_connect.php [ text ] Connects to a particular database, but doesn't do anything.
  2. db_create.php [ text ] Connects to one database and tries to create another database, but fails because the user does not have permission to do so.
  3. db_drop.php [ text ] Connects to a database and tries to "drop" (or delete) another database, but fails because that database does not exist.
  4. db_listdb.php [ text ] Lists the databases to which a particular MySQL user has access.
  5. db_listtables.php [ text ] Lists all the tables in all the databases to which a particular MySQL user has access.

Database Table Creation Group

The three files in this group allow you to create a table in your database. The HTML file will show you the form and you can fill it out but the script to which it is connected will not run here. You must copy all three files to your own account and set them up with your own username and password and database for them to work properly.

  1. get_table_name_and_size.html Provides a form for the user to enter the name and number of fields in a table to be created.
  2. define_table_fields.php [ text ] Receives the data from the form in the previous file and allows the user to choose the name and data type for each field in the table.
  3. create_table.php [ text ] Actually creates the table developed by the previous two files.

A One-Table Music Database Group

The scripts in this group add records to, and retrieve information from, a one-table database contain information related to a music library. You should use the three files in the Database Table Creation Group above to create the table. Then you can use the files here to put records in the table and retrieve information from the table via a menu that lets you select how you would like to see the data organized. The HTML files here will show you the relevant forms but the associated scripts will not run here. You must copy them to your own account and set them up properly there for them to perform their various tasks.

  1. my_music.txt A brief description of the files in the single table that makes up the my_music database.
  2. add_record.html [ text ] The form and script that add a new record to the music database.
  3. selection_menu.html The menu that allows the user to choose how the music information is to be displayed.
  4. select_by_artist.php [ text ] Shows the musical selections in the database ordered by artist name.
  5. select_by_date_acquired.php [ text ] Shows the musical selections in the database ordered by date acquired, with the most recent first.
  6. select_by_id.php [ text ] Shows the musical selections in the database ordered by id number, which will be the order in which the items were entered into the database.
  7. select_by_title.php [ text ] Shows the musical selections in the database ordered by title.

Cookies Group

The scripts in this group are all cookie-related. A cookie is a small piece of text sent from the server to the browser, to be stored on the browser, and later retrieved and used by that server when that browser comes back to that server.

  1. set_cookie.php [ text ] Sets a cookie called "name" with value "Porter" to last for two minutes.
  2. get_cookie.php [ text ] Gets the cookie called "name" with value "Porter" and uses it to welcome the user "Porter" back to the site.
  3. get_cookie_with_test.php [ text ] Gets the cookie called "name" with value "Porter" and uses it to welcome the user "Porter" back to the site, but also tests to see if there is a cookie, and if not issues a welcome to the arriving "stranger".
  4. unset_cookie.php [ text ] Deletes (or "unsets") the cookie called "name" with value "Porter".
  5. bad_cookie.php [ text ] Illustrates that a cookie must be the first thing sent from a web page.

Sessions Group

The scripts in this group are all session-related. A session is a mechanism used to pass information from one page to another as a user browses from one page to another on your site. This is a way of overcoming the "stateless" nature of web pages that "have no memory".

  1. session_start.php [ text ] Starts a PHP "session".
  2. count_visits.php [ text ] Updates a visit counter in the $_SESSION array and reports the result.
  3. session_remove.php [ text ] First removes all components from the current $_SESSION array, and then removes the session itself.
  4. set_preferences.php [ text ] Permits the user to choose the font family and size to use for displaying the page.
  5. change_preferences.php [ text ] Permits the use to change the font family and size preferences set by the previous script.

Database User Authentication Group

The scripts in this group are all related to user authentication by checking whether a user is "registered" in a database. The scripts must be copied to your own account and set up to work there, since they will not run here.

  1. auth_users.txt A brief description of the files in the single table that makes up the auth_users database.
  2. add_user.html The form for adding a user to the auth_users database.
  3. add_user.php [ text ] The script for adding a user to the auth_users database.
  4. login1.html The login form for authenticating a user in the auth_users database.
  5. login1.php [ text ] The script for authenticating a user in the auth_users database.
  6. login2.html The login form for authenticating a user in the auth_users database, and setting a cookie which is checked when the user tries to access either of the following "secret" scripts.
  7. login2.php [ text ] The login script for authenticating a user in the auth_users database, and setting a cookie which is checked when the user tries to access either of the following "secret" scripts.
  8. secretPage.php [ text ] A script that checks whether a cookie is set before proceeding.
  9. secretA.php [ text ] The first script that tests whether a cookie is set before proceeding.
  10. secretB.php [ text ] The second script that checks whether a cookie is set before proceeding.