Friday, October 20, 2017

PHP Session Management With Some Tips


Long time back, we have discussed about the PHP Session for state management. Now I will share some new concepts about session that you might not heard about it. Check out our blog archive on the topic if you’re looking to learn about PHP Session For State Management.

Why do we need session ?
Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests.

Lets see the some more examples about PHP Session, Which may help you to build more understanding.


Generate Session ID using session_id() function. 

Syntax :
string session_id ([ string $id ] )

session_id() is used to get or set the session id for the current session.

Example : 1 [ Get the session ID]

Below code help you to generate the unique session ID for the current session and it will be the same throughout your browsing session.

<?php
session_start();
$getID = session_id(); // generate unique id
echo $getID;
?>

OUTPUT:
----------------------------------
sltak2mjb1qkp2vrtlfak07jjq

Example : 2 [ Set the session ID]

Sometime you require to set the unique id from database for state management . If that's the case lets see the below code, in which we are manually setting the session id as per the need.

NOTE : When you are setting the session id manually, it should be used before the session_start() method, Otherwise it will not work.

<?php
session_id("uniqueID:83249389"); // setting the session ID manually
session_start();
$getID = session_id(); // get the session id after you set
echo $getID;
?>

OUTPUT:
uniqueID:83249389

Example : 3 [ Update the existing session ID value to new value]

Update the current session id value with a new one can be possible by using session_regenerate_id() function.

First Run the below line code and Refresh the page it will generate unique session id every time.

<?php

session_start();
$getID = session_id(); // generate unique id
echo $getID;

session_regenerate_id(); // update the new session id to exisitng sesion ID

?>

session_regenerate_id() : Update the current session id with a newly generated one.
Syntax: 
bool session_regenerate_id ([ bool $delete_old_session = false ] )


Get/Set session name using session_name() function:

This function help you Set and Get the current session name like Session_id() function.
<?php
$a = session_name("ghjgjh");
session_start();
echo $a;
?>

NOTE :  it should be used before the session_start() method, Otherwise it will not work.


Create Session using session_create_id() function

session_create_id() is used to create new session id for the current session. It returns collision free session id. Every time it is generating the new unique ID for the state management.

NOTE : Refresh the page each time it will generate the unique code .
<?php
session_start();
echo session_create_id();
?>

Destroys all data registered to a session using session_destroy()

session_destroy() use to destroy all data registered to a session
<?php
session_start();
// destroy the session 
session_destroy(); 

?>



1 comment:

  1. You can also use Redis server for PHP session management. For this you must first install Redis server and configure for session management. You can also use MySQLi for session handling PHP.

    ReplyDelete