Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | How to use $_SESSION variable in PHP
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

How to use $_SESSION variable in PHP

Using $_SESSION instead of Cookies. Tutorial on how create and use $_SESSION in PHP.

 
  Author: TheDude | Version: PHP 5.3 | 2nd December 2011 |  
 
 
1.
 

$_SESSION variable stores data on a web server for each user separately, which makes data accessible from other php pages as well.

First thing you need to do to use $_SESSION variable is put this in the very first row of your code:

<?php
session_start();
?>


 
 
2.
 

Now enter the following code in index.php:

<?php
$_SESSION['x']='John Deer';

echo $_SESSION['x'];
?>

First we store in $_SESSION['x'] value 'John Deer' and function echo displays the variable value.

 
 
3.
 

This is what you get in your web browser if you you open index.php.

 
 
4.
 

Now open a new document in the same folder and save it as index2.php.

Again put the following code in the very first row of the new index2.php document:

<?php
session_start();
?>

 
 
5.
 

Inside the <body> tag enter the following code:

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

In this case we didn't define the variable value, we just displayed the existing value of 
$_SESSION['x'] variable.

 
 
6.
 

Now try and open index2.php in your browser and you should be able to see value John Deer displayed. Meaning the $_SESSION['x'] variable value has been successfully transferred from the index.php to index2.php page.

Variable
$_SESSION gets automatically deleted after about 15 minutes of user inactivity.

In case you want to create another
$_SESSION['x'] variable, just put in some other letter instead of an x.

 
 
 
   
  Please login to post a comment
  Click to open user profile  
activedetaili, 4th Jun 2024, 1:28 PM
Interior car detailing involves deep cleaning of the cabin, including seats, carpets, and dashboard.
 
 
  Click to open user profile  
heru yuda, 30th Aug 2024, 6:22 AM
https://new.petmicrochiplookup.org/domin....
 
 
  Click to open user profile  
heru yuda, 30th Aug 2024, 6:22 AM
https://new.petmicrochiplookup.org/domin....
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO