Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

PHP Sessions

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

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH