X

Rate Us :



Share with Friends :

Menu

PHP-Sessions :

Sessions can store the data in Web server. This data can be access from any web page in the browser. By using $_SESSION, we can create and access the sessions.


By default, we cannot access the sessions of one page from another page. If we want to access the sessions we need to initialize the sessions when the request is started.


By using SESSION.AUTO_START we can initialize sessions. The default value is zero. By changing it's value as '1', we can start the session when the request is started. Otherwise use session_start() function in the application from where we want to access sessions.

Example :

Page1.php

<?php session_start(); $_SESSION['x'] = 100; echo $_SESSION['x']; ?> <a href="Page2.php">Go To Next</a>

Page2.php

<?php session_start(); echo $_SESSION['x']; ?>

Output :

100 Go To Next


session_id :

session_id is an unique value generated by the server when user sends the first request to the server. It is alphanumeric string. When user connected with server without session_id then sever creates new session_id for the user. This session_id stores in client system as inmemory cookie. The name of cookie is PHP SESSID and value is the SESSION_ID. At the same time, in server temporary memory location, a new file will create to store the session data. File name is same as SESSION_ID with prefix word SESS_. This file creates in temporary memory location of server.


When user sending the first request without session_id, new session will create. The same session_id transfer between browser and server which consequent request and response. If user closing the website, the inmemory cookie will destroy. Again if we connect with server, the request will go without session_id, then server creates the new session_id for the user again.

Example :

Page1.php

<?php session_start(); if(isset($_POST['sub'])) { $con = $_POST['d1']; $visit = $_POST['t1']; $_SESSION[$con] = $visit; } ?> <form method = "POST" action = " "> country :<select name = 'd1'> <option>USA</option> <option>Australia</option> </select> <br> visited :<input name = 't1'> <br> <input type = "submit" name = "sub" value = "submit"> </form> <a href="Page2.php">Show Details</a>

Page2.php

<?php session_start(); foreach($_SESSION as $k => $v) { echo $k." ".$v; } ?>

Output :

country : visited :
Show Details

SESSION_UNSET() :

By using this function, we can delete the data of session.


SESSION_DESTROY() :

By using this function, we can destroy session.

Example :

<?php session_start(); $_SESSION['abc'] = "Man"; SESSION_UNSET(); SESSION_DESTROY(); ?>