<?php
// Do something wrong
echo "opening wrong file<br>";
$file=fopen("NoSuchFile.txt", "r");
echo "done.<br>";
?>
|
We can create our own error handling function, with a custom message.
Run Example - Custom Error Handler
<?php
// Create Custom Error Handler
function myErrHandler ($errNum, $errString)
{
echo "<b>Error Code $errNum:</b> $errString<br>";
}
set_error_handler("myErrHandler");
// Do something wrong
echo "opening wrong file<br>";
$file=fopen("NoSuchFile.txt", "r");
echo "done.<br>";
?>
|
We can even have an error send us an email message.
Run Example - Error w/ Email
<?php
// Create Custom Error Handler
function myErrHandler ($errNum, $errString)
{
echo "Oops! We apologize for the disruption in service.<br>Please try again later.<P>";
error_log ("Error Code $errNum: $errString",1,"dannellys@winthrop.edu","From: dannellys@winthrop.edu");
}
set_error_handler("myErrHandler");
// Do something wrong
echo "opening wrong file<br>";
$file=fopen("NoSuchFile.txt", "r");
echo "done.<br>";
?>
|
Data can be saved during session by using the predefined _SESSION variable. This is an alternative to using a cookie. Just like cookies must be dealt with before any HTML is sent to the browser, we must Start the Session before HTML is sent to the browser.
Run Example - Session Test
<?php
// must start Session first!
session_start();
// we have been here before
if (isset($_SESSION["usrname"]))
echo "Welcome " . $_SESSION['usrname'] . "!<br />";
// user specified name
else if (isset($_POST['usrname']))
{
$_SESSION ["usrname"] = $_POST['usrname'];
echo "Setting the Session<P>";
}
// first time visitor
else
{
echo "Welcome first time visitor<P>";
echo "<form action='sess_test1.php' method='Post'>";
echo "User Name: <input type='text' name='usrname'><br>";
echo "<input type=submit value='Save Name'<P>";
echo "</form>";
}
?>
|
We have used _POST, _COOKIE, and _SESSION. If we sent our form data to our script via GET instead of the POST method, then we would retrieve form data from the _GET system variable. Two other interesting system variables are _ENV and _SERVER.
Run Example - Print_R for _SERVER and _ENV
<?php require_once "Appt_Library"; // Get Info from Form if ( !is_set ($_POST['fName'] )) did not supply a name blah blah $fName = $_POST['fName']; blah blah // Create a New Appointment $myAppt = new studentAppt(); $myAppt->setNames($fName, $lName); $myAppt->setDayTime($day,$time); // Remove Time Slot and Save Appt $myAppt->deleteTimeSlot(); $myAppt->saveRecord(); ?> |
class studentAppt
{
private $fName, $lName;
private $apptDay, $apptTime;
// * * * * * * * * * * * * * * * * * * * * * * * * * *
__construct ()
{
blah blah blah
}
__destruct ()
{
blah blah blah
}
// * * * * * * * * * * * * * * * * * * * * * * * * * *
function setNames ($param1, $param2)
{
$this->fName = $param1;
$this->lName = $param2;
}
function setDayTime ($param1, $param2)
{
$this->apptDay = $param1;
$this->apptTime = $param2;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * *
function getDayTime()
{
SELECT * FROM appointments WHERE ...
...
return array (this->$apptDay, this->$apptTime);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * *
function saveRecord()
{
INSERT INTO appointments VALUES ( $fName, $lName, $apptDay, $apptTime );
}
// * * * * * * * * * * * * * * * * * * * * * * * * * *
function isExistingRecord()
{
SELECT * FROM appointments WHERE ...
if ($record_count == 0)
return false;
else
return true;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * *
function deleteTimeSlot()
{
DELETE FROM timeslots WHERE day=$apptDay && time=$apptTime;
}
|
require_once "Appt_Library";
. . .
process the form
. . .
// create a new appointment instance
$myAppt = new studentAppt();
$myAppt->setNames($fName, $lName);
// is that appt in the system?
if ($myAppt->isExistingRecord())
{
$dayTime = $myAppt->getDayTime();
echo "Your appointment is at $dayTime[1] on $dayTime[0]";
}
else
{
echo "Could NOT find your appointment.";
}
|