Setting PHP Session
The session and cookies store the user information currently visiting your website. So why session and cookies? Session information stored on server so it will hide from the user. We don’t have easy ability to change the session. Cookies value can be changed.
We can use the session variable for login, shopping cart and many more website purpose. Let’s see session concept through example. One thing is very important, when you used the session always write session_start() at the top of your PHP page. Create two PHP page and in the index.php page write the code as specify below.
First run the welcome.php page. Nothing is going to happen. Because our session u_fname and u_lname variable value is not set. Now run the index.php page. So what is that mean u_fname has a value “Harshida ” and u_lname has a value is “Parmar”. Again run the welcome.php page our variable will display the output.
The session variable is very useful for login purpose. Let’s check it out through example. So we need to change some of the code in our welcome.php Page.
In the above code say that if our session variable is null than go to index.php page.
Restart your browser and first run welcome.php page. You can see that you are automatically redirected to index.php page, because both the session variable value is not set. Now run index.php page and then again try to run the welcome.php page. Our page will not be redirect to index.php page. Because at the second time our session variable value has been set.
Unsetting the Session Variable.
If we want to unset the session variable, there is couple of way to unset the session variable value. First create one new PHP page unsetting.php.






No comments:
Post a Comment