PHP Programming/Integration Methods (HTML Forms, etc.)

Integrating PHP

edit

There are quite a few ways that PHP is used. Following are a few methods that PHP can be called.

Forms

edit

Forms are, by far, the most common way of interacting with PHP. As we mentioned before, it is recommended that you have knowledge of HTML, and here is where we start using it. If you don't, just head to the HTML Wikibook for a refresher.

Form Setup

edit

To create a form and point it to a PHP document, the HTML tag <form> is used and an action is specified as follows:

  <form method="post" action="action.php">
      <!-- Your form here -->
  </form>

Once the user clicks "Submit", the form body is sent to the PHP script for processing. All fields in the form are stored in the variables $_GET or $_POST, depending on the method used to submit the form.

The difference between the GET and POST methods is that GET submits all the values in the URL, while POST submits values transparently through HTTP headers. A general rule of thumb is if you are submitting sensitive data, use POST. POST forms usually provide more security.

Remember $_GET and $_POST are superglobal arrays, meaning you can reference them anywhere in a PHP script. For example, you don't need to call global $_POST or global $_GET to use them inside functions.

Example

edit

Let's look at an example of how you might do this.

 <!-- Inside enterlogin.html -->
 <html>
 <head>
  <title>Login</title>
 </head>
 <body>
  <form method="post" action="login.php">
   Please log in.<br/>
   Username: <input name="username" type="text" /><br />
   Password: <input name="password" type="password" /><br/>
   <input name="submit" type="submit" />
 </form>
 </body>
 </html>

This form would look like the following:

Please log in.
Username:
Password:
submit

And here's the script you'd use to process it (login.php):

 <?php
  // Inside enterlogin.html
  if($_POST['username'] == "Spoom" && $_POST['password'] == "idontneednostinkingpassword")
  {
   echo("Welcome, Spoom.");
  }
  else
  {
   echo("You're not Spoom!");
  }
 ?>

Let's take a look at that script a little closer.

 if($_POST['username'] == "Spoom" && $_POST['password'] == "idontneednostinkingpassword")

As you can see, $_POST is an array, with keys matching the names of each field in the form. For backward compatibility, you can also refer to them numerically, but you generally shouldn't as this method is much clearer. And that's basically it for how you use forms to submit data to PHP documents. It's that easy.

For More Information

edit

PHP Manual: Dealing with Forms

PHP from the Command Line

edit

Although PHP was originally created with the intent of being a web language, it can also be used for commandline scripting (although this is not common, because simpler tools such as the bash scripting are available).

Output

edit

You can output to the console the same way you would output to a webpage, except that you have to remember that HTML isn't parsed (a surprisingly common error), and that you have to manually output newlines. The typical hello world program would look like this:

 <?php
   print "Hello World!\n";
 ?>

Notice the newline character at the end-of-line - newlines are useful for making your output look neat and readable.

Input

edit

PHP has a few special files for you to read and write to the command line. These files include the stdin, stdout and stderr files. To access these files, you would open these files as if they were actual files using fopen, with the exception that you open them with the special php:// "protocol", like this:

$fp = fopen("php://stdin","r");

To read from the console, you can just read from stdin. No special redirection is needed to write to the console, but if you want to write to stderr, you can open it and write to it:

$fp = fopen("php://stderr","w");

Bearing how to read input in mind, we can now construct a simple commandline script that asks the user for a username and a password to authenticate himself.

 <?php
   $fp = fopen("php://stdin","r"); 
   
   print "Please authenticate yourself\n";
   print "Username: ";
   // rtrim to cut off the \n from the shell
   $user = rtrim(fgets($fp, 1024));
   print "Password: ";
   // rtrim to cut off the \n from the shell
   $pass = rtrim(fgets($fp, 1024));
   if (($user=="someuser") && ($pass=="somepass")) {
    print "Good user\n";
    // ... do stuff ...
  } else die("Bad user\n");
  fclose($fp);
 ?>

Note that this script just serves as an example as to how to utilise PHP for commandline programming - the code contained here demonstrates a very poor way to authenticate a user or to store a password. Remember that your PHP scripts are readable by others!

For More Information

edit