New Users: Quick PHP CGI

Revision as of 16:25, 2 July 2010 by Ric (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Quick PHP (cgi) 3.5-Apollo

So you have installed UniServer, performed all tests and it’s working. Where is the on off switch for PHP? The answer is straightforward, it’s already running, any page with a “.php” extension is scanned for PHP code and executed. I have written this page purely for orientation with reference to Uniform Server, it is not intended in any way to teach you PHP you can find better material on the Internet. Its sole purpose is to get you up and running with PHP on Uniform Server it is a very basic introduction to PHP.

Introduction

PHP 5.2.3 (as used on 3.5-Apollo) is the latest release, and few issues have been found with it. Indeed, PHP 5 has proved to be a much more stable release than PHP 4.

This section provides a few examples that you can run on Uniform Server. It is not intended to teach you PHP you can find better material on the net.

Note: There are two variants of PHP that can be run on Apache, CGI and CLI. Uniform Server uses the CLI module for processing and serving PHP web pages, think of this as CGI mode. On the next page I cover CLI, it is this dual role that can be confusion basically don’t worry about it, you get the best of both worlds from one module.

PHP Syntax

The following very basic HTML page shows how to insert PHP, save this page as test1.php and place it in folder *\Uniform Server\udrive\www

<html>
<title>Test 1</title>
 <body>
   <P>Welcome, <?php print "Hello World"; ?></P>
 </body>
</html>

Display the page by typing http://localhost/test1.php into your browser address bar.

It displays a paragraph; "Welcome, Hello World", note 'Hello World' is the result produced by the PHP code. There are two things of importance:

  • The file extention must be .php
  • Your PHP code must be enclosed between <?php and ?> open and close tags respectively.


Note 1: PHP on Uniform Server has been set for short_open_tag on hence you can use <? ?> instead of <?php ?>

However it is recommended you do not use short tags. All Uniform Server versions that include PHP 5.3.* has short tags disabled.

Note 2: You can set Apache to use other file extension other than php see PHP file extensions for details.

Top

PHP information function

PHP information function, well I have included this just to show a little of PHP's power. True you can use apanel to obtain this information but how complex is the code that produces it. Create a new page named test2.php and copy the following code:

<html>
<title>Test 2</title>
<body>
   <?php phpinfo(); ?>
</body>
</html>

Display the page by typing http://localhost/test2.php into your browser address bar.

The complete script uses a single function phpinfo() this is built into PHP.

Note: The above is refereed to as embedding code into your HTML document.
The next example shows you are not restricted to placing code just inside the body tags.

Top

Embedded Code

You can embed PHP code anywhere in an HTML page and do just about anything you like. The following page demonstrates this Create a new page and name it test3.php copy the following code into it:

<html>
<?php print "<title>PHP Info page test 3</title>\n"; ?>
<body>
<?php
phpinfo();
?>
</body>
<?php print "</html>" ?>

Display the page by typing http://localhost/test3.php into your browser address bar.

The code adds a title for the page it even adds the closing HTML tag. OK not practical but servers to demonstrate my point. One last example along these lines PHP pure code. Top

Pure Code

You normally embed PHP code in HTML pages however you are not restricted to this. The following code creates an entire page using just the print command: Again create a new page and name it test4.php copy the following code into it:

<?php
print "<html>\n";
print "<title>Test 4</title>\n";
print "<body>\n";
print "<p>Welcome to a complete HTML page</p>\n";
print "</body>\n";
print "</html>\n";
?>

Display the page by typing http://localhost/test4.php into your browser address bar.

It works however look at all those print statements.
If you like typing fine! However take a look at the next section PHP Heredoc to save you some work.

Top

PHP Heredoc

This example is a little more advanced however I introduce it here because it will save you a lot of effort in the future. The previous example PHP pure uses a number of print commands with additional formating markup.

The need for all that typing really is painful however ther is a better way to implement it, to the rescue Heredoc.
Create a new page and name it test5.php copy the following code into it:

<?php
print <<<eot
<html>
<title>Test 5</title>
<body>
<p>This just reduces the effort</p>
<p>Welcome to a complete HTML page</p>
</body>
</html>
eot;
?>

Display the page by typing http://localhost/test5.php into your browser address bar.

Heredoc allows you to define your own string limiter. In this example I am using eot instead of quotes. I like to think of the three chevrons as piping the string into the print function. You can replace print with a variable this would then hold the string as a value.

  • Note: eot you can use name you like
  • In reality the three chevrons <<< tell PHP you want to enter heredoc mode.
  • The delimiter (eot;)at the end of the string must have no spaces around it and must be followed by a semi-colon.
  • You need to escape dollar symbols if you want them printed, otherwise what follows the $ symbol will be treated as a variable and replaced accordingly. Use a backslash like this \$

I am only scratching the surface but the next concept is important; including external files.

Top

Include External Files

An important concept in PHP is that of including files, this allows sharing of functionality. Including a file is extremely easy use the include keyword, and specify the filename you want to include. Most web sites you see have a common banner and footer this example uses only one page but gives the general idea it requires the following three files:

Create a new page and name it test6.php copy the following code into it:

<html>
<title>Test 6</title>
<body>
<?php include 'banner.php'; ?>
<p>This is my main page content</p>
</body>
<?php include 'footer.php'; ?>
</html>

Create a new page and name it banner.php copy the following code into it:

<div style="border:3px solid #ff0000;background:#ffff00; padding:10px;text-align:center;">
<h1>Uniform Server</h1>

Create a new page and name it footer.php copy the following code into it:

<div style="background:#000000; color:#ffffff;padding:3px;text-align:center;fint-size:10px;">
My footer Uniform Server
</div>

Save all three files to folder www
Display the page by typing http://localhost/test6.php into your browser address bar.

Looking at the code for test6.php, the include command inserts a banner from the banner.php file. This is followed by your main page content. Finally a footer is inserted from the file footer.php Use this format for every page on your site and you see how easy it would be to change all page banners just by changing one file.


Note 1: Include files can be nested (for example the included file may itself include another file and so on)
Note 2: All include files must have the file extension .php they get processed preventing their content being directely viewable over the Internet.

Unlocking the real power of PHP requires the use of Superglobals these are pre-defined for you.

Top

Superglobals

Superglobals can be considered a big bucket of data that is destined for PHP. Data from the internet is poured into this bucket along with your current system environment and a mix of other things. Inside the bucket you will find data is neatly packed into a set of nine arrays. These arrays are accessible from anywhere within your scripts hence the name superglobals. You can ignore the last two.

  • $_GET Contains all variables sent using HTTP GET request. The one attached to the URL.
  • $_POST Contains all variables sent using HTTP POST request.
  • $_FILES Contains all variables sent using HTTP POST file upload.
  • $_COOKIE Contains all variables sent using HTTP cookies.
  • $_SESSION Contains all variables stored in a user's session.
  • $_SERVER Contains all variables set by Apache server.
  • $_ENV Contains all environment variables set by your system.
  • $GLOBALS
  • $_REQUEST

OK thats all fine but how do you use them. The first example HTTP_REFERER I have had my little rant about in the quick install guide problems hence could not resist showing you this.

Top

HTTP_REFERER

HTTP_REFERER is one of PHP's pre-set variables that get set before your script runs. As seen by the server it contains the current URL of a users page if a link was clicked on that page, or it will be empty if the user entered the URL directly into their browser. To see this in action run these two files:

Create a new page and name it test8.php copy the following code into it:

<html>
<title>Test 8</title>
<body>
<a href="test8a.php">Click link to demostrate HTTP_REFERER</a>
</body>
</html>

Create a new page and name it test8a.php copy the following code into it:

<html>
<title>Test 8a</title>
<body>
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
print "You came from page {$_SERVER['HTTP_REFERER']}<br />";
} else {
print "You type the page in a new browser window<br />";
}
?>
</body>
</html>

Save the two files to folder www

Display the first page by typing http://localhost/test8.php into your browser address bar. Click the link on this page read the message.

Now open a new browser window (or tab) and type http://localhost/test8a.php into your browser address bar.
Note the message displayed.

Results

When a page is loaded HTTP_REFERER is set; clicking a link requests a new page, HTTP_REFERER is sent with the request. Hence the message "You came from page.."

However when you type the page into a new browser window HTTP_REFERER is not set hence it will not be sent with the page request.

Top

Quick PHP CLI


  Ric