All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Sessions

PHP Sessions is also like cookies you can store user information to be used across multiple pages you want (e.g. userid, password, etc). By default, session variables last until the user closes the browser.



Starting PHP Session

You can start session by calling session_start() function


You can set the session variable by $_SESSION variable.


Example of starting and storing session variables.


session_start();

$_SESSION["Name"] = "Rahul";
$_SESSION["Course"] = "B-Tech";
$_SESSION["Rollno"] = "34";



Destroy PHP Session

session_start();

// remove all session variables
session_unset(); 

// destroy the session 
session_destroy(); 




Accessing PHP Session Variable Values

session_start();

echo "Name:".$_SESSION["Name"];
echo "Course:".$_SESSION["Course"];
echo "Rollno:".$_SESSION["Rollno"];




Change PHP Session Variable Values

session_start();

$_SESSION["Name"] = "Aman";
$_SESSION["Course"] = "BCA";
$_SESSION["Rollno"] = "2";

❮ PrevNext ❯