Loading...

PHP Sessions


In php sessions provides a way to store or keep track the information through with the help of session variables values accross multiple pages on the same website.
these session variables values stores temporary on the server it stays on the server untill user open the browser window.
it is used to store user values or user current session info on the server.
for example session variables are used in ecommerce websites to store user info accross multiple pages to know the user session which helps the website to keep track the user cart info , wishlist info, user login details etc.

PHP Session
in php the session start with the session_start() function. session variables are defined through php super global variable i.e $_SESSION.
for example :
save the below code in a index.php file. here we start a session and set session variable values.
        		     				    	    		
<?php
    session_start();  // to start a session in php.
    $_SESSION["user"] = "John";  // store user value that is now available in many pages on the website.
    $_SESSION["email"] = "john@xyz.com";  // store email value that is now available in many pages on the website.
    echo "User values are all set ..";
?>        		     				    	    			
        		     				    	    		
        		     				    	    	
Output :
User values are all set ..

save the below code in a home.php file. here we will use the session variable values.
        		     				    	    		
<?php
    session_start();  // to start a session in php.
    echo "User Name is : $_SESSION['user']"."<br>";
    echo "User Email is : $_SESSION['email']"."<br>";
?>        		     				    	    			
        		     				    	    		
        		     				    	    	
Output :
User   Name   is   :   John
User   Email   is   :   john@xyz.com