Session variables are just like normal variables except the fact that session variables are used to access some particular data across multiple pages. For example if you want to build a login system then in order to check whether the user is logged in or not you have to use session variables. You may store the user id in the session variable for each user at the time of signin/signup and then use the value of this session variable across different pages of members area.
How to use session variables in php?
In php if you want to use session variables then first of all you should start the session. This can be done by adding the following code at the start of each page where you want to use session variables:
<?php session_start(); ?>After adding the above mentioned code at the start of each page you are all set for using session variables. Session variables in php can be easily declared using the following code:
<?php $_SESSION['user_id']=1234; ?>After the declaration of session variables you can access the value of these variables on any page using the following code:
<?php echo $_SESSION['user_id']; ?>Now that you know how to declare and use session variables you should know how to remove values from session variables. Removing a value from any session variable is very simple, you can use following code:
<?php unset($_SESSION['user_id']); ?>But the above code will only remove the value of one session variable, now if you have declared many session variables and you want to delete them all at once then you can do this by completely destroying the session. This can be done by using the following code:
<?php session_destroy(); ?>Ok guyz I guess that is all about session variables in php. If you have any queries or suggestions then please feel free to comment.
Note: Dont forget to start the session at the beginning of each page.
Previous Lesson --- Next Lesson
2 comments:
how to store a div id in a session and call on other page usign session..
Just like a preview!
I want to preview one of the div content on other page..
@Jagir
You can store anything in session variables. If you only need to store div id then just store it in any session variable. If you mean something else please do lemme know.
Post a Comment