Sessions in PHP



A PHP session fixes this problem so that you can store user information on the server for later use (i.e. user name, shopping cart item etc.). However, this session information is temporary and generally it is removed after users leave the website leaving the session.

It is important to consider that if the temporary storage of the session is applicable to your website. If you need more stable storage then you will need to find another solution, such as a MySQL database.

Note:If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug.

Syntax - 

<?php
session_start(); // start up your PHP session! 
?>

Example - 

<?php  
session_start();  
?>  
<html>  
<body>  
<?php  
$_SESSION["user"] = "Sachin";  
echo "Session information are set successfully.<br/>";  
?>  
<a href="session2.php">Visit next page</a>  
</body>  
</html>  


Storing a session variable


Use the $ _SESSION companion array when you want to store user data in the session This is where you store and retrieve session data. In previous versions of PHP, there were other ways to perform this store operation, but it has been updated and this is the right way to do this.

<?php
session_start(); 
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

Output -  Pageviews = 1




Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment