PHP I : Web Apps

Follow this lesson in Ullman Chapter 8. The scripts are located in the 08 directory.

 

Checking for Form Submission

if (isset ($_POST[‘submit’])) {
//Handle the form
….
}

 

Using include() and require()

See Ullman page 198.

<?php
include (‘../../../dbcon.php’);
….
?>

Included files are “brought in” to the current file. If include() fails, PHP simply generates a warning (which is visible to visitors on the web).

<?php
require (‘../../../dbcon.php’);
….
?>

Required files are also “brought in” to the current file. But if require() fails, PHP stops execution of the script.

See also include_once() and require_once().

 

Constants

You can’t change it after you set it!

define (‘CONSTANT’, ‘value);

for instance:

define (‘PI’, 3.1416);

Constants DO NOT USE THE $:

print PI;

Constants WON’T PRINT WITHIN ANY QUOTATION MARKS. Use concatenation to print them:

print ‘Pi equals ‘ . PI;

 

defined(‘PI’) //returns TRUE
defined(‘foo’) //returns FALSE

See Ullman page 207 for this usage.

 

PHP_VERSION

PHP_OS

 

Date and Time

date(‘format’)

See Ullman page 209 for format options

time()

mktime()

 

Self-Referral: Presenting a Form and Handling Submission In a Single Page

if(isset($_POST[‘submit’])) {
   //Then handle the form
} else {
   //Display the form here
}

 

Making Forms Sticky

See Ullman’s register.php script for the full text.

// Display the form.
print ‘<form action=”register.php” method=”post”><p>’;

print ‘Username: <input type=”text” name=”username” size=”20″ value=”‘ . $_POST[‘username’] . ‘” /><br />’;

 

Sending email with PHP

mail ( ’email_address’, ‘subject’, ‘message’);

Life will be much easier if you populate a variable with the message, and use that variable as the third parameter.

 

Output Buffering

This function simply sends the output of your page/script to a buffer, so that time-sensitive functions like session_start() won’t run until ready.

Place this at the very top of your code, before everything, including <html> and <head> elements:

<?php
ob_start();
?>

Place this at the very end of your code, after everything, including the </body> and </html> elements:

<?php
ob_end_flush();
?>

<?php
ob_end_clean();
?>

Either of these last two turn off buffering.

See Ullman page 231 for other buffer functions.

 

HTTP Headers

Header functions must be called before anything else is sent to the browser!

The most frequent use is for redirecting the user to a new page:

header(‘Location: some_page.php);
exit();

 

To do out of class:

Review Chapter 8 of Ullman.