Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Listing 7.6 Using Sessions


<?
//Start the session.
//This must be called before
//sending any content.
session_start();


//Register a couple of variables
session_register("Name");
session_register("Count");


//Set variable based on form input
if($inputName != "")
{
$Name = $inputName;
}


//Increment counter with each page load
$Count++;
?>




Listing 7.6


//print diagnostic info
print("Diagnostic Information
\n");
print("Session Name: ". session_name(). "
\n");
print("Session ID: ". session_id(). "
\n");
print("Session Module Name: ". session_module_name(). "
\n");
print("Session Save Path: ". session_save_path(). "
\n");
print("Encoded Session:". session_encode(). "
\n");

print("


\n");


if($Name != "")
{
print("Hello, $Name!
\n");
}


print("You have viewed this page $Count times!
\n");


//show form for getting name
print("<FORM ACTION=\"$SCRIPT_NAME?".SID."\" METHOD=\"POST\">");
print("<INPUT TYPE=\"text\" NAME=\"inputName\"
VALUE=\"$Name\">
\n");
print("<INPUT TYPE=\"submit\" VALUE=\"Change Name\">
\n");
print("");


//use a link to reload this page
print("<A HREF=\"$SCRIPT_NAME?".SID."\">Reload
\n");
?>


Free download pdf