PHP Tutorials - Herong's Tutorial Examples - v5.18, by Herong Yang
SessionPage*.php - Session Test Script Pages
This section provides a tutorial example on how to write multiple script pages and use the session concept to make them work together.
To help us testing the session concept, I wrote 3 PHP scripts.
1. SessionPage1.php:
<?php # SessionPage1.php #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. # session_start(); $quantity = 3; $_SESSION['quantity'] = $quantity; if (isset($_SESSION['count'])) { $count = $_SESSION['count']; } else { $count = 0; } $count++; $_SESSION['count'] = $count; # print "<pre>\n"; print "\nI am buying $quantity PHP books.\n"; print "\n<a href=SessionPage2.php>Next</a>\n"; print "\nCounter = $count\n"; print "Session name = ".session_name()."\n"; print "Session id = ".session_id()."\n"; # print "\nContents of \$_GET:\n"; foreach ($_GET as $k => $v) { print " $k = $v\n"; } # print "\nContents of \$_POST:\n"; foreach ($_POST as $k => $v) { print " $k = $v\n"; } # print "\nContents of \$_COOKIE:\n"; foreach ($_COOKIE as $k => $v) { print " $k = $v\n"; } print "</pre>\n"; ?>
2. SessionPage2.php:
<?php # SessionPage2.php #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. # session_start(); $quantity = $_SESSION['quantity']; $price = 9.99; $_SESSION['price'] = $price; $count = $_SESSION['count']; $count++; $_SESSION['count'] = $count; # print "<pre>\n"; print "\nI am buying $quantity PHP books.\n"; print "The unit price is $price per book.\n"; # print "\n<a href=SessionPage3.php>Next</a> "; print " <a href=SessionPage1.php>Prev</a>\n"; print "\nCounter = $count\n"; print "Session name = ".session_name()."\n"; print "Session id = ".session_id()."\n"; # print "\nContents of \$_GET:\n"; foreach ($_GET as $k => $v) { print " $k = $v\n"; } # print "\nContents of \$_POST:\n"; foreach ($_POST as $k => $v) { print " $k = $v\n"; } # print "\nContents of \$_COOKIE:\n"; foreach ($_COOKIE as $k => $v) { print " $k = $v\n"; } print "</pre>\n"; ?>
3. SessionPage3.php:
<?php # SessionPage3.php #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. # session_start(); $quantity = $_SESSION['quantity']; $price = $_SESSION['price']; $total = $quantity * $price; $count = $_SESSION['count']; $count++; $_SESSION['count'] = $count; # print "<pre>\n"; print "\nI am buying $quantity PHP books.\n"; print "The unit price is $price per book.\n"; print "The total price is $total.\n"; # print "\n<a href=SessionPage2.php>Prev</a>\n"; print "\nCounter = $count\n"; print "Session name = ".session_name()."\n"; print "Session id = ".session_id()."\n"; # print "\nContents of \$_GET:\n"; foreach ($_GET as $k => $v) { print " $k = $v\n"; } # print "\nContents of \$_POST:\n"; foreach ($_POST as $k => $v) { print " $k = $v\n"; } # print "\nContents of \$_COOKIE:\n"; foreach ($_COOKIE as $k => $v) { print " $k = $v\n"; } print "</pre>\n"; ?>
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
►Creating and Managing Sessions in PHP Scripts
How Sessions Are Supported in PHP
►SessionPage*.php - Session Test Script Pages
Running Session Test Script Pages
Managing Session IDs without Cookies
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
Functions to Manage Directories, Files and Images
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS