WEBAPPS - Practice Exercise 9 - PHP Session Variable and Page Redirection
WEBAPPS - Practice Exercise 9 - PHP Session Variable and Page Redirection
Instruction: Type the following codes in any text editor that you going to use like Notepad++.
Understand and analyze the codes. Run the program and understand the output of the program.
Sample Program: Session Variable and Page Redirection
FILENAME: S1.PHP
1: <?php
2: //Filename: S1.php
3:
4: //start the session
5: session_start();
6: ?>
7: <form method="post">
8: <input type="text" name="name"><input type="submit" name="submit">
9: </form>
10: <?php
11: $n = "";
12:
13: //if the submit button is set, redirect to file S2.php using header() and
set the session variable
14: if (isset($_POST['submit']))
15: {
16: $n=$_POST['name'];
17: //set the session variable
18: $_SESSION["name"]=$n;
19:
20: //PHP code in line 16 and 18 can be written as
21: //$_SESSION['name'] = $_POST['name']; to set the session variable
22:
23: //redirect to file S2.php
24: header("location: S2.php");
FILENAME: S2.PHP
1: <?php
2: //Filename: S2.php
3:
4: //start the session
5: session_start();
6:
7: // Echo session variables that were set on previous page
8: echo "Hello, <b>" . $_SESSION["name"] . "</b>.<br>Welcome to my
Website.<br><br>";
9:
10: //this will print all session variable from the previous file run using
session variable if not destroyed using session_destroy() function
11: print_r($_SESSION);
12:
13: // to change a session variable, just overwrite it
14: $_SESSION["name"] = "Elijah";
15: echo "<br>Hello, <b>" . $_SESSION["name"] . "</b>.<br>Welcome to my
Website.<br><br>";
16:
17: // to remove all session variables use
18: //session_unset() -->It deletes only the variables from session and
session still exists. Only data is truncated(shorten the duration or
access).
19: //-->if you include this, if will encounter an error jumping to S3.php
since it deletes the data from session variable "name" (it deletes the
session variable)
20: //session_unset();
21:
22: // to destroy the session use
23: //session_destroy(); -->It destroys all of the data associated with the
current session. It does not unset any of the global variables associated
with the session, or unset the session cookie.
Prepared by: Prof. EMILY F. SICAT Page 2
Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
24: //-->if you include this, if will encounter an error jumping to S3.php
since it does not only deletes the data from session variable "name" but
also destroy the session
25: //session_destroy();
26:
27: //if the submit button is set, redirect to file S3.php using header()
28: if (isset($_POST['submit']))
29: header("location: S3.php");
30: ?>
31: <form method="post">
32: <input type="submit" name="submit">
33: </form>
FILENAME: S3.PHP
1: <?php
2: //Filename: S3.php
3:
4: //start the session
5: session_start();
6:
7: // Echo session variables that were set on previous page
8: echo "Hello, <b>".$_SESSION["name"]."</b>!<br>Welcome to my
Website.<br><br>";
9:
10: //start the session variable(s)
11: print_r($_SESSION);
12:
13: //destroy the session
14: session_destroy();
15: ?>
FILENAME: INPUT.PHP
1: <?php
2: ini_set('display_errors',0);
3: error_reporting( E_ALL & ~ E_NOTICE);
4:
5: //start the session
6: session_start();
FILENAME: OUTPUT.PHP
1: <?php
2: ini_set('display_errors',0);
3: error_reporting( E_ALL & ~ E_NOTICE);
4: session_start();
5: ?>
6: <!DOCTYPE html>
7: <html>
8: <head>
9: <title>Sicat On-Lline</title>
10: </<head>
11: <body><center>
12: <?php
13: if (isset($_POST["submit"]))
14: {
15: //remove all session variables
16: session_unset();
17:
18: //destroy the session
19: session_destroy();
20:
21: //redirect page
22: header("location: Input.php");
23: }
24:
25: $productName=$mode="";
26: $tp=$disc_charge=$totprice=0;
27: //$N will hold how many products selected
28: $N = count($_SESSION["p"]);
29: ?>
30: <table>
Prepared by: Prof. EMILY F. SICAT Page 7
Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
31: <tr>
32: <td><h1>ON-LINE SHOPPING STORE</h1></td>
33: </tr>
34: </table>
35:
36: <table width=250 border=1 cellpadding=2 cellspacing=1>
37: <tr>
38: <th width=250>CUSTOMER INFORMATION</th>
39: </tr>
40: </table>
41:
42: <table width=250 border=1 cellpadding=2 cellspacing=1>
43: <tr>
44: <td width=100>Name: </td>
45: <td width=500><?php echo $_SESSION['name'];?></td>
46: </tr>
47: <tr>
48: <td width=100>Address: </td>
49: <td width=500><?php echo $_SESSION['address'];?></td>
50: </tr>
51: <tr>
52: <td width=100>Contact #: </td>
53: <td width=500><?php echo $_SESSION['contact'];?> </td>
54: </tr>
55: </table>
56:
57: <br>
58: <?php
59: echo("<p>You purchased ".$_SESSION["N"]." product(s): ");
60: ?>
61: <table width=270 border=1 cellpadding=2 cellspacing=1>
62: <tr>
63: <th width=270>ORDER INFORMATION</th>
64: </tr>
65: </table>
66:
67: <table border=1 cellspacing=2 cellpadding=2>