PHP108 And A Half

From mi-linux
Jump to navigationJump to search

Main Page >> Web Application Development >> Workbook >> Predefined Variables

What is a predefined variable?

A number of predefined variables are available to ANY PHP script, however some are dependent on the webserver, version of server, etc. Some of the more common are shown here with some examples of how to use them. The following are all array types.

$_SERVER

Variables set by the web server or otherwise directly related to the execution environment of the current script - PHP.net

The following are some useful $_SERVER variables although many more can be found at uk.php.net Manual

$_SERVER["PHP_SELF"]

Used throughout this wiki, this variable returns the directory, filename and extension such as "cp1079/test1.php" and is useful if you want a form to come back to itself when you click submit.

$_SERVER["SERVER_ADDR"]

The IP address of the web server

$_SERVER["HTTP_REFERER"]

Not sent by all browsers, but this should contain the address of the page which linked to your page - might give you an indication of which page was visited before the current one.

$_SERVER["REMOTE_ADDR"]

The IP address of the client


$_GET & $_POST

Get and post are critical concepts to understand that are fundamental to web development in general, not just PHP specific topics.

Essentially, in order to pass parameters to PHP webpages, one or both of these methods will be used.

$_GET

If you want to pass parameters to a script on the URL you would use $_GET to retrieve the variables you send

$_GET Example

Create the following and save it as "getTest.php"

 <html>
   <head><title>Get Example</title></head>
   <body>
    <?php
      if (isset($_GET["name"])) {
        echo "<p>Hello ".$_GET["name"]."</p>";
      } else {
        echo "<p>I don't know who you are!</p>";
      }
   ?>
  </body>
 </html>

Point your browser at getTest.php, then try the following URL

http://[yourserveranddirectory]/getTest.php?name=SuperPHP

$_POST

If you want to use HTML forms to send information to a PHP script

$_POST example

Create the following and name the file "postPart1.html"

 <html>
   <head>
     <title>$_POST - part 1</title>
   </head>
   <body>
     <form method="POST" action="postPart2.php">
       <p>Enter a module code: <input type="text" name="frmModuleCode" value=""></p>
       <p><input type="submit" value="Go"></p>
     </form>
   </body>
 </html>

And create the following and name the file "postPart2.php"

 <html>
   <head>
     <title>$_POST - part 2</title>
   </head>
   <body>
     <?php
       if (isset($_POST["frmModuleCode"]) && $_POST["frmModuleCode"] != "") {
         echo "<p>I love ".$_POST["frmModuleCode"]."</p>";
       } else {
         echo "<p>You haven't entered a Module Code - go back and try again</p>";
       }
     ?>
   </body>
 </html>

Point your browser at "postPart1.html"

EXERCISES: Experiment with GET and POST to send and retrieve information to and from your PHP scripts.

Ready to move on?

PHP109 - Files and PHP