Sessions are useful in shopping cart, user login etc. Session is lot more secure Session are lot easier and quicker. Now what we can do for create session in this example i will show you how to set a session and then how we can view session that already been set. We can also check if session as set and then we can view it . Before viewing the session we must set the session by open set session page into the browser. Now Firstly we have to start the session for that we write
session_start();
This code must be at top before we can do anything with session data. If you create session before session start that will does not work because for session data working we must stat the session. Now You had seen my article
working with data form , $_POST variable and $_GET variable we had seen $_POST and $_GET and session works exactly same way $_SESSION we have to square brackets we have variable name as you can see our session variable name is name
$_SESSION['name']='Alex';
Now we set this relative to piece of information. SO, name = 'Alex';. This is our session name and this variable exit only this page now however its stored now as a session. So, Therefore this should be accessible by anypage of our website anytime even we don't declare it. Now by opening page set.php we set the session and execute the code. This particular session is set Alex. Now at view.php will return data to user. Now when we had to include variables to another file of php we use include function but session data stored at server we don't need to include the file to another file of php.
session_start();
Even you reading back variable you need to start the session then we had simply echo out the session name
echo $_SESSION['name'];
Your may be thinking why it is useful session variable ? where may make useful to session ? the answer is when we want the user logged in data we create session for the user which may useful when you want to say welcome username or hello username when the user is logged in
After execution of file set.php will set the variable in session then only we can use session variable to view.php before that will not output us session variable let see example what i am trying to tell you by example