php sessions & cookies
TRANSCRIPT
![Page 1: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/1.jpg)
WELCOME
![Page 2: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/2.jpg)
INTRODUCTION TO PHPSESSIONS AND COOKIES
![Page 3: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/3.jpg)
What you Benefit ???
By the end of this session you will learn
● How to use Sessions and Cookies to maintain the state among
multiple requests.
![Page 4: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/4.jpg)
TASK OF THE DAY
Create a session when a user log in to his account. When user logout from his account the session should expire
LOGIN PAGE
![Page 5: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/5.jpg)
INTRODUCTION TO PHP SESSIONS AND COOKIES
![Page 6: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/6.jpg)
Introduction To PHP Sessions And Cookies
We had already tried passing data to a server . But..how the server knows the user from which the requests are received…?
![Page 7: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/7.jpg)
COOKIES
![Page 8: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/8.jpg)
Cookies
•HTTP is a stateless protocol; this means that the web server does not know (or care) whether two requests comes from the same user or not; it just handles each request without regard to the context in which it happens.
•Cookies are used to maintain the state in between requests—even when they occur at large time intervals from each other.
•Cookies allow your applications to store a small amount of textual data (typically,4-6kB) on a Web client browser.
•There are a number of possible uses for cookies, although their most common one is maintaining state of a user
![Page 9: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/9.jpg)
Creating A Cookie
• setcookie(“userid", "100", time() + 86400);
• This simply sets a cookie variable named “userid” with value “100” and this variable value will be available till next 86400 seconds from current time
Cookie variable name
variable value
Expiration time.
![Page 10: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/10.jpg)
Accessing a Cookie
• echo $_COOKIE[’userid’]; // prints 100
• Cookie as array– setcookie("test_cookie[0]", "foo");– setcookie("test_cookie[1]", "bar");– setcookie("test_cookie[2]", "bar");
– var_dump($_COOKIE[‘test_cookie’]);
![Page 11: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/11.jpg)
Destroying A Cookie
•There is no special methods to destroy a cookie, We achieve it by setting the cookie time into a past time so that it destroys it
–Eg : setcookie(‘userid’,100,time()-100);
![Page 12: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/12.jpg)
SESSIONS
![Page 13: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/13.jpg)
Sessions
•Session serve the same purpose of cookies that is sessions are used to maintain the state in between requests
•Session can be started in two ways in PHP–By changing the session.auto_start configuration setting in php.ini–Calling session_start() on the beginning of each pages wherever
you use session(Most common way)Note: session_start() must be called before any output is sent to the
browser
![Page 14: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/14.jpg)
Creating and accessing session
• Once session is started you can create and access session variables like any other arrays in PHP – $_SESSION[‘userid’] = 100;
– echo $_SESSION[‘userid’]; //prints 100
Session variable name variable value
![Page 15: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/15.jpg)
Destroying A Session
•There are two methods to destroy a session variable1. Using unset() function
• Eg unset($_SESSION[‘userid’])
2. Calling session_destroy() method. This will effectively destroy all the session variables. So for deleting only one variable you should go for the previous method• Session_destroy()
![Page 16: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/16.jpg)
Let’s try implementing with our task
![Page 17: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/17.jpg)
Step 1
Goto Login_baabtra.php page and set form action to Profile.php page
<form name=”login” action=”login_action.php” method=”post”>
![Page 18: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/18.jpg)
Step 2
Login_action.php Page
Create database connection heremysql_connect('localhost','root','');mysql_select_db("Baabtra");$result=mysql_query("select * from tbl_user where vchr_user_name='$username'and vchr_password='$password'");
![Page 19: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/19.jpg)
Step 3
Login_action.php Page
Check whether id is valid or not.if valid user then create session
if(mysql_num_rows($result)){while($row=mysql_fetch_array($result)){
session_start();$_SESSION['user_id']=$row['pk_int_user_id'];
header(‘Location: profile.php’);
}}
checks whether there is any resultant
![Page 20: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/20.jpg)
Step 3
Login_action.php Page
Check whether id is valid or not.if valid user then create session
if(mysql_num_rows($result)){while($row=mysql_fetch_array($result)){
session_start();$_SESSION['user_id']=$row['pk_int_user_id'];
header(‘Location: profile.php’);
}}
starts a session
![Page 21: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/21.jpg)
Step 3
Login_action.php Page
Check whether id is valid or not.if valid user then create session
if(mysql_num_rows($result)){while($row=mysql_fetch_array($result)){
session_start();$_SESSION['user_id']=$row['pk_int_user_id'];
header(‘Location: profile.php’);
}} sets a session variable
userid with value of
pk_int_user_id field of the resultant set
![Page 22: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/22.jpg)
Step 3
Login_action.php Page
Check whether id is valid or not.if valid user then create session
if(mysql_num_rows($result)){while($row=mysql_fetch_array($result)){
session_start();$_SESSION['user_id']=$row['pk_int_user_id'];
header(‘Location: profile.php’);
}} sets a session variable
userid with value of
pk_int_user_id field of the resultant set
![Page 23: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/23.jpg)
Step 3
Login_action.php Page
Check whether id is valid or not.if valid user then create session
if(mysql_num_rows($result)){while($row=mysql_fetch_array($result)){
session_start();$_SESSION['user_id']=$row['pk_int_user_id'];header(‘Location: profile.php’);
}}
sets a session variable userid
with value of pk_int_user_id field of
the resultant set
![Page 24: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/24.jpg)
Step 3
Login_action.php Page
Check whether id is valid or not.if valid user then create session
if(mysql_num_rows($result)){while($row=mysql_fetch_array($result)){
session_start();$_SESSION['user_id']=$row['pk_int_user_id'];header(‘Location: profile.php’);
}}
header function is used for page redirection
![Page 25: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/25.jpg)
Step 4
Design a profile Page and Create a link for Logout
![Page 26: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/26.jpg)
Step 5
Go to profile page and display Qualification details of that particular user using session variable.
![Page 27: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/27.jpg)
Step 5Profile.php
session_start();$user_id=$_SESSION['user_id'];mysql_connect('localhost','root','');mysql_select_db("Baabtra");$result=mysql_query("select * from tbl_academic_qualificaion where fk_int_user_id='$user_id'");echo “ qualification name-----college--------percentage--------passout”;
while($data=mysql_fetch_assoc($result)){echo $data['vchr_qualification_name'];echo $data['vchr_qualification_name'];echo $data['int_percentage'];echo $data['dat_passout_date'];}
![Page 28: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/28.jpg)
Step 5Profile.php
session_start();$user_id=$_SESSION['user_id'];mysql_connect('localhost','root','');mysql_select_db("Baabtra");$result=mysql_query("select * from tbl_academic_qualificaion where fk_int_user_id='$user_id'");echo “ qualification name-----college--------percentage--------passout”;
while($data=mysql_fetch_assoc($result)){echo $data['vchr_qualification_name'];echo $data['vchr_qualification_name'];echo $data['int_percentage'];echo $data['dat_passout_date'];}
fetches the session variable user_id and
stores to variable $userid
![Page 29: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/29.jpg)
Step 5Profile.php
session_start();$user_id=$_SESSION['user_id'];mysql_connect('localhost','root','');mysql_select_db("Baabtra");$result=mysql_query("select * from tbl_academic_qualificaion where fk_int_user_id='$user_id'");echo “ qualification name-----college--------percentage--------passout”;
while($data=mysql_fetch_assoc($result)){echo $data['vchr_qualification_name'];echo $data['vchr_qualification_name'];echo $data['int_percentage'];echo $data['dat_passout_date'];}
selects the qualification details of the user that matches with session
value
![Page 30: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/30.jpg)
Step 6
Destroy session on Logout
![Page 31: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/31.jpg)
Step 6
Logout.php
unset($_SESSION[‘user_id’]);header(‘Location: Login_baabtra.php’);
![Page 32: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/32.jpg)
Comparison
Cookies are stored in the user's browser
A cookie can keep information in the user's browser until deleted by user or set as per the timer. It will not be destroyed even if you close the browser.
Cookies can only store string
We can save cookie for future reference
Sessions are stored in server
A session is available as long as the browser is opened. User cant disable the session. It will be destroyed if you close the browser
Can store not only strings but also objects
session cant be.
Cookies Session
![Page 33: Php sessions & cookies](https://reader036.vdocument.in/reader036/viewer/2022062711/55bfd670bb61ebf6198b466b/html5/thumbnails/33.jpg)
END OF THE SESSION