Chap4 Session

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

I.

Session:

 It is time period between login and logout.


 It is way to store information (in variables) to be used across multiple
pages.
 It is not stored on client’s computer

II. Why and when to use Sessions?

 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.

III. How session works?

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

3 In php, to access the cookie we use In php, to access the session


$_COOKIE variable we use $_SESSION
4 Cookie ends depends on the life time you set Session ends when user
for it. close his browser.
5 It can store only 4kb of data It can store large amount of
data
6 It can be used for future references It cannot be used for future
references
Creation of Session: Session can be created using session_start()
<?php
// Starting session
session_start();
?>
The session_start() function first checks to see if a session already exists by
looking for the presence of a session ID. If it finds one, i.e. if the session is already
started, it sets up the session variables and if doesn't, it starts a new session by
creating a new session ID.

Note: session_Start() must be written before html tag

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.

Example: To Store information

1. $_SESSION["user"] = "Sachin";  

Example: To Get information

1. echo $_SESSION["user"];  

PHP Destroying Session: session_destroy() function is used to destroy all session


variables completely.
PHP Session Examples
1. Write a PHP script to create and access session variable [Hint: Use
$_SESSION]
File: session1.php

<?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'];

echo "<h3>Employee Details</h3> ";


echo "<br>Name : ".$_SESSION['eno'];
echo "<br>Address : ".$_SESSION['enm'];
echo "<br>Class : ".$_SESSION['eadd'];
echo "<br>basic : ".$e1;
echo "<br>DA : ".$e2;
echo "<br>HRA : ".$e3;
$total = $e1+$e2+$e3;
echo "<h2>Total Of Earnings Is : $total.";
?>

You might also like