SlideShare a Scribd company logo
1 of 14
PHP COOKIES 
PHP SESSIONS 
PHP include and require Files 
2
What is a Cookie? 
A cookie is often used to identify a user. A cookie is a small 
file that the server embeds on the user's computer. Each time 
the same computer requests a page with a browser, it will 
send the cookie too. With PHP, you can both create and 
retrieve cookie values. 
How to Create a Cookie? 
 The setcookie() function is used to set a cookie. 
 Note: The setcookie() function must appear BEFORE the <html> tag. 
Syntax 
setcookie(name, value, expire); 3
Example 1 
In the example below, we will create a cookie named 
"user" and assign the value “John” to it. We also specify 
that the cookie should expire after one hour: 
<?php 
setcookie("user", " John ", time()+3600); 
?> 
<html> 
..... 
 Hint… 60*60 4
Example 2 
You can also set the expiration time of the cookie in another way. It may 
be easier than using seconds. 
<?php 
$expire=time()+60*60*24*30; 
setcookie("user", " 12BSCS ", $expire); 
?> 
<html> 
..... 
In the example above the expiration time is set to a month (60 sec * 60 
min * 24 hours * 30 days). 
5
In the example below, we retrieve the value of the 
cookie named "user" and display it on a page: 
<?php 
// Print a cookie 
echo $_COOKIE["user"]; 
// A way to view all cookies 
print_r($_COOKIE); 
?> 
6
In the following example we use the isset() function to find out if a 
cookie has been set: 
<html> 
<body> 
<?php 
if (isset($_COOKIE["user"])) 
echo "Welcome " . $_COOKIE["user"] . "!<br>"; 
else 
echo "Welcome guest!<br>"; 
?> 
</body> 
</html> 7
 A PHP session variable is used to store information about, or change settings for 
a user session. Session variables hold information about one single user, and are 
available to all pages in one application. 
Starting a PHP Session 
 Before you can store user information in your PHP session, you must first start 
up the session. 
 Note: The session_start() function must appear BEFORE the <html> tag: 
 <?php session_start(); ?> 
<html> 
<body> 
</body> 
</html> 
 The code above will register the user's session with the server, allow you to start 
saving user information, and assign a UID for that user's session. 
8
The correct way to store and retrieve session variables is to use the PHP 
$_SESSION variable: 
<?php 
session_start(); 
// store session data 
$_SESSION['views']=1; 
?> 
<html> 
<body> 
<?php 
//retrieve session data 
echo "Pageviews=". $_SESSION['views']; 
?> 
</body> 
</html>Output: Pageviews=1 
9
 In the example below, we create a simple page-views counter. The isset() function 
checks if the "views" variable has already been set. If "views" has been set, we can 
increment our counter. If "views" doesn't exist, we create a "views" variable, and 
set it to 1: 
<?php 
session_start(); 
if(isset($_SESSION['views'])) 
$_SESSION['views']=$_SESSION['views']+1; 
else 
$_SESSION['views']=1; 
echo "Views=". $_SESSION['views']; 
?> 
10
If you wish to delete some session data, you can use the unset() or the session_destroy() 
function. 
 The unset() function is used to free the specified session variable: 
<?php 
session_start(); 
if(isset($_SESSION['views'])) 
unset($_SESSION['views']); 
?> 
 You can also completely destroy the session by calling the session_destroy() function: 
<?php 
session_destroy(); 
?> 
 Note: session_destroy() will reset your session and you will lose all your stored session data. 
11
 In PHP, you can insert the content of one PHP file into another PHP file before the 
server executes it. 
 The include and require statements are used to insert useful codes written in 
other files, in the flow of execution. 
Syntax 
include 'filename'; 
or 
require 'filename'; 
12
 Assume we have an include file with 
some variables defined ("vars.php"): 
 <?php 
$color='red'; 
$car='BMW'; 
?> 
 Then the variables can be used in 
the calling file: 
 <html> 
<body> 
<h1>Welcome to my home 
page.</h1> 
<?php include 'vars.php'; 
echo "I have a $color $car"; // I have a 
red BMW 
?> 
</body> 
</html> 
13
www.w3school.com 
14

More Related Content

What's hot

Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
Arjun Shanka
 

What's hot (20)

PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Php array
Php arrayPhp array
Php array
 
Php forms
Php formsPhp forms
Php forms
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Php
PhpPhp
Php
 
Php functions
Php functionsPhp functions
Php functions
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONSPHP COOKIES AND SESSIONS
PHP COOKIES AND SESSIONS
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 

Viewers also liked

Deliver Files With PHP
Deliver Files With PHPDeliver Files With PHP
Deliver Files With PHP
Thomas Weinert
 
Php file upload, cookies & session
Php file upload, cookies & sessionPhp file upload, cookies & session
Php file upload, cookies & session
Jamshid Hashimi
 
Cookie & Session In ASP.NET
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NET
ShingalaKrupa
 
Php Form
Php FormPhp Form
Php Form
lotlot
 
Session Management & Cookies In Php
Session Management & Cookies In PhpSession Management & Cookies In Php
Session Management & Cookies In Php
Harit Kothari
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
Harit Kothari
 

Viewers also liked (20)

Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
PHP - Getting good with cookies
PHP - Getting good with cookiesPHP - Getting good with cookies
PHP - Getting good with cookies
 
PHP Cookies, Sessions and Authentication
PHP Cookies, Sessions and AuthenticationPHP Cookies, Sessions and Authentication
PHP Cookies, Sessions and Authentication
 
Session & Cookies
Session & CookiesSession & Cookies
Session & Cookies
 
Cookies in PHP
Cookies in PHPCookies in PHP
Cookies in PHP
 
Deliver Files With PHP
Deliver Files With PHPDeliver Files With PHP
Deliver Files With PHP
 
Php file upload, cookies & session
Php file upload, cookies & sessionPhp file upload, cookies & session
Php file upload, cookies & session
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with session
 
Cookie & Session In ASP.NET
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NET
 
Session and Cookie
Session and CookieSession and Cookie
Session and Cookie
 
Php Form
Php FormPhp Form
Php Form
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handling
 
PHP Files: An Introduction
PHP Files: An IntroductionPHP Files: An Introduction
PHP Files: An Introduction
 
3 php forms
3 php forms3 php forms
3 php forms
 
Session Management & Cookies In Php
Session Management & Cookies In PhpSession Management & Cookies In Php
Session Management & Cookies In Php
 
Chapter 08 php advance
Chapter 08   php advanceChapter 08   php advance
Chapter 08 php advance
 
Php Ppt
Php PptPhp Ppt
Php Ppt
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 

Similar to PHP Cookies and Sessions

Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
salissal
 

Similar to PHP Cookies and Sessions (20)

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
 
lecture 12.pptx
lecture 12.pptxlecture 12.pptx
lecture 12.pptx
 
FP512 Cookies sessions
FP512 Cookies sessionsFP512 Cookies sessions
FP512 Cookies sessions
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptxPHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
 
Manish
ManishManish
Manish
 
Php session
Php sessionPhp session
Php session
 
PHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdfPHP-Cookies-Sessions.pdf
PHP-Cookies-Sessions.pdf
 
cookies.ppt
cookies.pptcookies.ppt
cookies.ppt
 
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
 
Sessions n cookies
Sessions n cookiesSessions n cookies
Sessions n cookies
 
PHP 2
PHP 2PHP 2
PHP 2
 
lecture 13.pptx
lecture 13.pptxlecture 13.pptx
lecture 13.pptx
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
 
Php
PhpPhp
Php
 
season management in php (WT)
season management in php (WT)season management in php (WT)
season management in php (WT)
 
murach12.pptx
murach12.pptxmurach12.pptx
murach12.pptx
 
javaScriptCookies.pptx
javaScriptCookies.pptxjavaScriptCookies.pptx
javaScriptCookies.pptx
 
Introduction to php web programming - sessions and cookies
Introduction to php   web programming - sessions and cookiesIntroduction to php   web programming - sessions and cookies
Introduction to php web programming - sessions and cookies
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Ph
PhPh
Ph
 

More from Nisa Soomro (16)

Connecting to my sql using PHP
Connecting to my sql using PHPConnecting to my sql using PHP
Connecting to my sql using PHP
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
 
PHP Filing
PHP Filing PHP Filing
PHP Filing
 
Html5
Html5Html5
Html5
 
HTML Basic Tags
HTML Basic Tags HTML Basic Tags
HTML Basic Tags
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
HTML Frameset & Inline Frame
HTML Frameset & Inline FrameHTML Frameset & Inline Frame
HTML Frameset & Inline Frame
 
HTML Images
HTML Images HTML Images
HTML Images
 
HTML Lists & Llinks
HTML Lists & LlinksHTML Lists & Llinks
HTML Lists & Llinks
 
HTML Tables
HTML TablesHTML Tables
HTML Tables
 
Html5 SVG
Html5 SVGHtml5 SVG
Html5 SVG
 
Html5 Canvas Detail
Html5 Canvas DetailHtml5 Canvas Detail
Html5 Canvas Detail
 
Html5 canvas
Html5 canvasHtml5 canvas
Html5 canvas
 
Html5
Html5Html5
Html5
 
Html
HtmlHtml
Html
 
Web programming lec#3
Web programming lec#3Web programming lec#3
Web programming lec#3
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 

PHP Cookies and Sessions

  • 1.
  • 2. PHP COOKIES PHP SESSIONS PHP include and require Files 2
  • 3. What is a Cookie? A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values. How to Create a Cookie?  The setcookie() function is used to set a cookie.  Note: The setcookie() function must appear BEFORE the <html> tag. Syntax setcookie(name, value, expire); 3
  • 4. Example 1 In the example below, we will create a cookie named "user" and assign the value “John” to it. We also specify that the cookie should expire after one hour: <?php setcookie("user", " John ", time()+3600); ?> <html> .....  Hint… 60*60 4
  • 5. Example 2 You can also set the expiration time of the cookie in another way. It may be easier than using seconds. <?php $expire=time()+60*60*24*30; setcookie("user", " 12BSCS ", $expire); ?> <html> ..... In the example above the expiration time is set to a month (60 sec * 60 min * 24 hours * 30 days). 5
  • 6. In the example below, we retrieve the value of the cookie named "user" and display it on a page: <?php // Print a cookie echo $_COOKIE["user"]; // A way to view all cookies print_r($_COOKIE); ?> 6
  • 7. In the following example we use the isset() function to find out if a cookie has been set: <html> <body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br>"; else echo "Welcome guest!<br>"; ?> </body> </html> 7
  • 8.  A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application. Starting a PHP Session  Before you can store user information in your PHP session, you must first start up the session.  Note: The session_start() function must appear BEFORE the <html> tag:  <?php session_start(); ?> <html> <body> </body> </html>  The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session. 8
  • 9. The correct way to store and retrieve session variables is to use the PHP $_SESSION variable: <?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html>Output: Pageviews=1 9
  • 10.  In the example below, we create a simple page-views counter. The isset() function checks if the "views" variable has already been set. If "views" has been set, we can increment our counter. If "views" doesn't exist, we create a "views" variable, and set it to 1: <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?> 10
  • 11. If you wish to delete some session data, you can use the unset() or the session_destroy() function.  The unset() function is used to free the specified session variable: <?php session_start(); if(isset($_SESSION['views'])) unset($_SESSION['views']); ?>  You can also completely destroy the session by calling the session_destroy() function: <?php session_destroy(); ?>  Note: session_destroy() will reset your session and you will lose all your stored session data. 11
  • 12.  In PHP, you can insert the content of one PHP file into another PHP file before the server executes it.  The include and require statements are used to insert useful codes written in other files, in the flow of execution. Syntax include 'filename'; or require 'filename'; 12
  • 13.  Assume we have an include file with some variables defined ("vars.php"):  <?php $color='red'; $car='BMW'; ?>  Then the variables can be used in the calling file:  <html> <body> <h1>Welcome to my home page.</h1> <?php include 'vars.php'; echo "I have a $color $car"; // I have a red BMW ?> </body> </html> 13