SlideShare a Scribd company logo
sessions and cookies in PHP
Sessions in php
 Session variables used to storing user information to be used across multiple
pages
 e.g. username, password, ..etc
 By default, session variables last until the user closes the browser.
Example for Session
Php session_start() function
 A session is started with the session_start() function
 Session variables are set with the PHP global variable: $_SESSION
 PHP $_SESSION is an associative array that contains all session variables.
 session_start(); // for creating session
 $_SESSION["user"] = “abc"; //setting of session values
 echo $_SESSION["user"]; //get or printing session values
session_destroy() in php
 PHP session_destroy() function is used to destroy all session
variables completely.
//Example c.php
<?php
session_start();
session_destroy();
?>
Example for Session
// Abc.php
<?php
session_start(); //creation of session
?>
<html>
<body>
<?php
$_SESSION["user"] = “pavan"; //setting session
value
echo "Session information are set successfully.<br/>
";
?>
<a href=“cs.php">Visit next page</a>
</body>
</html>
// cs.php
<?php
session_start();
?>
<html>
<body>
<?php
echo "User is: ".$_SESSION["user"];
?>
</body>
</html>
Cookies in php
 PHP cookie is a small piece of information which is stored at client browser.
 It is used to recognize the user.
 Cookie is created at server side and saved to client browser.
 Each time when client sends request to the server, cookie is embedded with
request.
 In that way cookie can be received at the server side.
Example for cookies
setcookie() function
 setcookie() function is used to set cookie with HTTP response.
 Once cookie is set, you can access it by $_COOKIE superglobal
variable.
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
Example For COOKIES
<?php
setcookie("user", “aaa");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["user"])) {
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
?>
</body>
Difference b/w session and cookies

More Related Content

What's hot

Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
Hitesh Patel
 

What's hot (20)

Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
 
Php
PhpPhp
Php
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
 
Javascript event handler
Javascript event handlerJavascript event handler
Javascript event handler
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
Cookies & Session
Cookies & SessionCookies & Session
Cookies & Session
 
File system node js
File system node jsFile system node js
File system node js
 
Php cookies
Php cookiesPhp cookies
Php cookies
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Node.js File system & Streams
Node.js File system & StreamsNode.js File system & Streams
Node.js File system & Streams
 
Le Wagon - UI components design
Le Wagon - UI components designLe Wagon - UI components design
Le Wagon - UI components design
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP
PHPPHP
PHP
 
php
phpphp
php
 
jQuery
jQueryjQuery
jQuery
 

Similar to Sessions and cookies in php

Similar to Sessions and cookies in php (20)

Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with session
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erick
 
17 sessions
17 sessions17 sessions
17 sessions
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.pptLecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
 
session.ppt
session.pptsession.ppt
session.ppt
 
Php session
Php sessionPhp session
Php session
 
season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
Php session 3 Important topics
Php session 3 Important topicsPhp session 3 Important topics
Php session 3 Important topics
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
18.register login
18.register login18.register login
18.register login
 
PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdf
 
Manish
ManishManish
Manish
 
Parameter Passing & Session Tracking in PHP
Parameter Passing & Session Tracking in PHPParameter Passing & Session Tracking in PHP
Parameter Passing & Session Tracking in PHP
 
Php mysql
Php mysqlPhp mysql
Php mysql
 

Recently uploaded

plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
parmarsneha2
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 

Recently uploaded (20)

Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 

Sessions and cookies in php

  • 2. Sessions in php  Session variables used to storing user information to be used across multiple pages  e.g. username, password, ..etc  By default, session variables last until the user closes the browser.
  • 4. Php session_start() function  A session is started with the session_start() function  Session variables are set with the PHP global variable: $_SESSION  PHP $_SESSION is an associative array that contains all session variables.  session_start(); // for creating session  $_SESSION["user"] = “abc"; //setting of session values  echo $_SESSION["user"]; //get or printing session values
  • 5. session_destroy() in php  PHP session_destroy() function is used to destroy all session variables completely. //Example c.php <?php session_start(); session_destroy(); ?>
  • 6. Example for Session // Abc.php <?php session_start(); //creation of session ?> <html> <body> <?php $_SESSION["user"] = “pavan"; //setting session value echo "Session information are set successfully.<br/> "; ?> <a href=“cs.php">Visit next page</a> </body> </html> // cs.php <?php session_start(); ?> <html> <body> <?php echo "User is: ".$_SESSION["user"]; ?> </body> </html>
  • 7. Cookies in php  PHP cookie is a small piece of information which is stored at client browser.  It is used to recognize the user.  Cookie is created at server side and saved to client browser.  Each time when client sends request to the server, cookie is embedded with request.  In that way cookie can be received at the server side.
  • 9. setcookie() function  setcookie() function is used to set cookie with HTTP response.  Once cookie is set, you can access it by $_COOKIE superglobal variable. <?php $cookie_name = "user"; $cookie_value = "John Doe"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day ?>
  • 10. Example For COOKIES <?php setcookie("user", “aaa"); ?> <html> <body> <?php if(!isset($_COOKIE["user"])) { echo "Sorry, cookie is not found!"; } else { echo "<br/>Cookie Value: " . $_COOKIE["user"]; } ?> </body>
  • 11. Difference b/w session and cookies