0% found this document useful (0 votes)
26 views

WEBAPPS - Practice Exercise 9 - PHP Session Variable and Page Redirection

The document provides code samples for using PHP session variables and page redirection. It includes code to start a session, set and retrieve session variables, and redirect between pages. Array functions are used to dynamically generate HTML form elements. The output displays customer information and order details stored in session variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

WEBAPPS - Practice Exercise 9 - PHP Session Variable and Page Redirection

The document provides code samples for using PHP session variables and page redirection. It includes code to start a session, set and retrieve session variables, and redirect between pages. Array functions are used to dynamically generate HTML form elements. The output displays customer information and order details stored in session variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

WEBAPPS – PHP SESSION VARIABLES,

PAGE REDIRECTION & USING ARRAY IN


CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
Week 8 Date
Section Score

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");

Prepared by: Prof. EMILY F. SICAT Page 1


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
25: }
26: ?>

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: ?>

Prepared by: Prof. EMILY F. SICAT Page 3


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
SAMPLE OUTPUT
Output upon running S1.php dsfsdf Output upon clicking the submit button in
sdfssdfsdfsdf S1.php

Output upon clicking the submit button from S2.php

Sample Program: Session Variable, Page Redirection and using Array


in creating HTML components

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();

Prepared by: Prof. EMILY F. SICAT Page 4


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
7:
8: if (isset($_POST["submit"]))
9: {
10: if (isset($_POST["p"]) &&
11: isset($_POST["paymode"]) &&
12: isset($_POST["name"]) &&
13: isset($_POST["address"]) &&
14: isset($_POST["contact"]))
15: {
16: $_SESSION["p"] = $_POST["p"];
17: $_SESSION["paymode"]=$_POST["paymode"];
18: $_SESSION["name"]=$_POST["name"];
19: $_SESSION["address"]=$_POST["address"];
20: $_SESSION["contact"]=$_POST["contact"];
21:
22: if(!empty($_SESSION["p"]))
23: $_SESSION["N"]=count($_POST["p"]);
24:
25: header("location: Output.php");
26: }
27:
28: if (empty($_POST["p"]))
29: {
30: echo("<p><h1>You didn't select any products.</h1></p>\n");
31: }
32: }
33: ?>
34: <form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
35: <h1>SICAT ON-LINE SHOPPING STORE</h1>
36:
37: <p><b>Which product/s would you like to buy?</b></p>
38: <?php
39: //declare the array for products and its price
40: $products =
array(2000=>"Bags",1500=>"Shoes",500=>"Accessories",1000=>"Jewelries");
41:

Prepared by: Prof. EMILY F. SICAT Page 5


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
42: //create the HTML checkbox component using array and foreach loop
43: foreach($products as $price => $prod)
44: {
45: ?>
46: <p><input type="checkbox" name="p[]" value="<?php echo $price;?>"><?php
echo $prod." <i>Php ".number_format($price,2)."</i>";?></p>
47: <?php
48: echo "\n";
49: }
50: ?>
51: <br>
52: <b>How will you pay?</b>
53: <?php
54: //declare the array for mode of payment
55: $payment = array(1=>"Cash", 2=>"Card");
56: ?>
57: <select name="paymode">
58: <?php
59: //create the HTML select component using array and foreach loop
60: foreach($payment as $key=>$value)
61: echo "<option value=\"$key\">$value</option>\n";
62: ?>
63: </select>
64: <br><br>
65: <b>Note:</b> Cash - with 10% discount on total purchase<br>
66: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Credit Card
- with 10% additional charge on total purchase
67:
68: <br><br>
69: <p><b>Customer Information:</b></p>
70: <p><label><b>Name: </b></label><input type="text" name="name" value="<?php
echo $_POST['name'];?>"></p>
71: <p><label><b>Address: </b></label><input type="text" name="address"
value="<?php echo $_POST['address'];?>"></p>
72: <p><label><b>Contact Number: </b></label><input type="text" name="contact"
value="<?php echo $_POST['contact'];?>"></p>
73: <br>

Prepared by: Prof. EMILY F. SICAT Page 6


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
74: <input type="submit" name="submit" value="Submit Order">&nbsp;&nbsp;&nbsp;
75: <input type="reset" name="reset" value="Reset Order">
76: </form>

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>

Prepared by: Prof. EMILY F. SICAT Page 8


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
68: <tr>
69: <th width=150><font face=Arial, Helvetica, sans-serif> Product
Sold</font></th>
70: <th width=100><font face=Arial, Helvetica, sans-serif>
Price</font></th>
71: </tr>
72: <?php
73: //this loop determine which product is selected
74: foreach($_SESSION["p"] as $key=>$value)
75: {
76: if ($value==2000)
77: $productName="Bag";
78: else if ($value==1500)
79: $productName="Shoes";
80: else if ($value==500)
81: $productName="Accessories";
82: else if ($value==1000)
83: $productName="Jewelries";
84:
85: //create a table row depending on what is selected on the product
checkbox
86: echo "<tr>";
87: echo "<td>$productName</td>";
88: echo "<td align='right'>Php ".number_format($value,2)."</td>";
89: echo "</tr>";
90: $tp+=$value;
91: }
92: ?>
93: </table>
94: <?php
95: if ($_SESSION["paymode"]==1)
96: {
97: $disc_charge = number_format($tp * 0.10,2);
98: $totprice = number_format($tp - $disc_charge,2);
99: $mode = "Discount: ";
100: }
101: else if ($_SESSION["paymode"]==2)

Prepared by: Prof. EMILY F. SICAT Page 9


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
102: {
103: $disc_charge = number_format($tp * 0.10,2);
104: $totprice = number_format($tp + $disc_charge,2);
105: $mode = "Charge: ";
106: }
107: ?>
108: </table>
109:
110: <table border=1 cellspacing=2 cellpadding=2>
111: <tr>
112: <td width=150>Sub-Total:</td>
113: <td width=100 align=right>Php <?php echo number_format($tp,2);?></td>
114: </tr>
115: <tr>
116: <td width=150><?php echo $mode;?></td>
117: <td width=100 align=right>Php <?php echo $disc_charge;?></td>
118: </tr>
119: <tr>
120: <td width=150>Total Purchase</td>
121: <td width=100 align=right>Php <?php echo $totprice;?></td>
122: </tr>
123: </table>
124: <?php
125: //remove all session variables
126: session_unset();
127:
128: //destroy the session
129: session_destroy();
130: ?>
131: <button type="button" onclick="location.href='Input.php'">Back</button>
132: </center></body>
133: </html>

Prepared by: Prof. EMILY F. SICAT Page 10


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
SAMPLE OUTPUT: INPUT.PHP AND OUTPUT.PHP
Output upon running Input.php Output of Input.php when submit button
is clicked without selecting product(s)

Prepared by: Prof. EMILY F. SICAT Page 11


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
Output upon clicking submit button of Output upon clicking submit button of
Input.php iand at least one product is Input.php and at least one product is
selected and cash is the mode of payment it selected and card is the mode of payment it
redirect to Output.php redirect to Output.php

Prepared by: Prof. EMILY F. SICAT Page 12


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP SESSION VARIABLES,
PAGE REDIRECTION & USING ARRAY IN
CREATING HTML COMPONENTS
PRACTICE EXERCISE #9
Output upon clicking submit button of Output upon clicking submit button of
Input.php and more than one product is Input.php and more than one product is
selected and cash is the mode of payment it selected and card is the mode of payment it
redirect to Output.php redirect to Output.php

Prepared by: Prof. EMILY F. SICAT Page 13


Faculty, College of Computing and Information Sciences

You might also like