Professional Documents
Culture Documents
Chap4 Session
Chap4 Session
Chap4 Session
Session:
You want to store important information such as the user id more securely
on the server where malicious users cannot temper with them.
You want to pass values from one page to another.
You want the alternative to cookies on browsers that do not support cookies.
You are developing an application such as a shopping cart that has to
temporary store information with a capacity larger than 4KB.
i. When you work with an application, you open it, do some changes,
and then you close it. This is much like a Session. The computer
knows who you are. It knows when you start the application and
when you end. But on the internet there is one problem: the web
server does not know who you are or what you do, because the
HTTP address doesn't maintain state.
ii. PHP session is used to store and pass information from one page to
another temporarily (until user close the website).
iii. PHP session creates unique user id for each browser to recognize
the user and avoid conflict between multiple browsers.
iv. PHP session technique is widely used in shopping websites where
we need to store and pass cart information e.g. username, product
code, product name, product price etc. from one page to another.
V Difference between Cookie and Session
Sr. Cookies Session
No
1 It is stored on Client Side It is stored on server side
2 Cookie is not all non-secure since data in Sessions are secured
cookie is stored text-format at client side and because it is stored in binary
travel with each HTTP request format/encrypted form and
gets decrypted at server and
they never travel with HTTP
request
Storing and Accessing Session Data: You can store all your session data as key-
value pairs in the $_SESSION[] superglobal array. The stored data can be accessed
during lifetime of a session. PHP $_SESSION is an associative array that contains
all session variables. It is used to set and get session variable values.
1. $_SESSION["user"] = "Sachin";
1. echo $_SESSION["user"];
<?php
session_start();
$_SESSION["user"] = "Sachin";
echo "Session information are set successfully.<br/>";
?>
<html><body>
<a href="session2.php">Visit next page</a>
</body>
</html>
File: session2.php
<?php
session_start();
eho "User is: ".$_SESSION["user"];
?>
2. Write a PHP script to keep track of number of times the web page has
been access. [Use Session]
<?php
session_start();
if (!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 1;
} else {
$_SESSION['counter']++;
}
echo ("Page Views: ".$_SESSION['counter']);
?>
3. Write a PHP script to accept Employee details (Eno, Ename, address)
on first page. On second page accept earning (Basic, Da, HRA). On
third page print Employee information (Eno, Ename, Address, BASIC,
DA, HRA, TOTAL)[Hint: Use Session]
Empsession.html
<html>
<body>
<form action="/php programs/earning.php" method="post">
<center> <h2>Enter Employee Details :</h2> <br>
Emp no :<input type=text name=”eno”></br></br>
Name :<input type=text name=”enm”><br><br>
Address :<input type=text name=”eadd”><br><br>
<br><br> <input type=submit value=Show name=”submit”>
</center>
</form>
</body>
</html>
earning.php
<?php
session_start();
$eno = $_POST['eno'];
$enm = $_POST['enm'];
$eadd = $_POST['eadd'];
$_SESSION[“eno”] = $eno;
$_SESSION[“enm”] = $enm;
$_SESSION[“eadd”] = $eadd;
?>
<html>
<body>
<form action="/php programs/disp.php" method="post">
<center>
<h2>Enter Earnings of Employee:</h2>
Basic : <input type="text" name="e1"></br><br>
DA : <input type="text" name="e2"></br></br>
HRA :<input type="text" name="e3"></br></br>
<input type="submit" value=Next></br></br>
</center>
</form>
</body>
</html>
Disp.php
<?php
session_start();
$e1 = $_POST['e1'];
$e2 = $_POST['e2'];
$e3= $_POST['e3'];