Tuesday 25 March 2014

Setting php Sessions

session php
In this tutorials we were going to see session inside php. Cookies and Session are quick similar in sense they store information about the user that currently visiting your website. So, we are going to store a piece of information it may value relative to them or they entered so why we do store sessions and why we do store cookies there will be another article of cookie and in each article. I will explain difference between them and what should be used when mainly security purposes.

Session are stored on server so hidden from the user we don't had easily ability to change sessions

Code for Setting php Session 


set.php (Screenshot1)

php sessions
Set.php Screenshot1




<?php

session_start();



$_SESSION['name']='Alex';

?>

View.php (Screenshot2)

session_start()
view.php Screenshot2


<?php

session_start();



echo $_SESSION['name'];



?>

Output (Screenshot3, Screenshot4)

php session
Output Screenshot3
session php
screenshot4
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

Code for Setting php Session 

set.php (Screenshot5)

set session php
Screenshot5


<?php

session_start();



$_SESSION['username']='alex';

?>

View.php (Screenshot6)

Screenshot6

<?php

session_start();



if (isset($_SESSION['username'])) {

echo 'Welcome. '.$_SESSION['username']';



}else {

echo 'Please log in.';

}



?>

Output screenshot 

session php
Screenshot7
php session
Screenshot8
session php
Screenshot9
If user logged in you want to log them through out the website. Now here our set.php page we can say that its login page when ever user will input correct username and password the session will grap the username and will store in it till user does not log out which will be in view.php. At moment we don't had session variable username set. So, Now here session is empty now As you can see Screenshot7 . In Screenshot8 we had call set.php therefore now session is set variable of username will be set in session. Now you can see in Screenshot that's actually what we want username to display when user logged in. This is minor part of login and register system itself. This types of fundamental will make system of register and login page.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates