0% found this document useful (0 votes)
31 views17 pages

PFPDP

The document discusses cookies and sessions in PHP. Cookies are small files stored on the client side that identify users, while sessions are server-side global variables that store temporary user information. The document provides details on creating, accessing, and deleting cookies and sessions in PHP code.

Uploaded by

shyilisnice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views17 pages

PFPDP

The document discusses cookies and sessions in PHP. Cookies are small files stored on the client side that identify users, while sessions are server-side global variables that store temporary user information. The document provides details on creating, accessing, and deleting cookies and sessions in PHP code.

Uploaded by

shyilisnice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Bhavesh R.

Joshi
Dr. V. R. Godhaniya I.T. College, Porbandar
Porbandar – 360 575

PHP
Session & Cookies
Cookie:
 A cookie is a small file with the maximum size of 4KB that
the web server stores on the client computer.
 It is used to recognize the user identity.
 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.
 Once a cookie has been set, all page requests that follow
return the cookie name and value.
 A cookie created by a user can only be visible to them.
Other users cannot see its value.
 There are 2 types of cookie: Temporary and Permanent.
How Cookie Works?
1) A user requests for a page that stores cookies
2) The server sets the cookie on the user’s computer
3) Other page requests from the user will return the cookie
name and value
Create a Cookie:
• In PHP we can create/set a cookie using the setcookie()
function. Below we have the syntax for the function,
• setcookie(name, value, expire, path, domain, secure)
• The first argument which defines the name of the cookie is
mandatory, rest all are optional arguments.
Argument Use
name Used to specify the name of the cookie.
value Used to store any value in the cookie.
expire Used to set the expiration time for a cookie.
path Used to set a web URL in the cookie.
domain The domain of your web application.
secure If you set this to 1, then the cookie will be available
and sent only over HTTPS connection.
<?php
// set the cookie
setcookie("username", “Madhav Joshi", time()+60*60*24*7);
?>

<?php
// check if the cookie exists
if(isset($_COOKIE["username"])) {
echo "Cookie set with value: ".$_COOKIE["username"];
}
else
{
echo "cookie not set!";
}
?>
Delete a Cookie:
<?php
// deleting the cookie
setcookie("username", “Madhav Joshi", time() - 3600);
print_r($_COOKIE);
?>
Session:
 A session is a global variable stored on the server.
 Each session is assigned a unique id which is used to
retrieve stored values.
 The session values are automatically deleted when the
browser is closed. If you want to store the values
permanently, then you should store them in the database.
 Session is not stored on the user browser like Cookies,
hence it is a more secure option.
• PHP session is used to store and pass information from one
page to another temporarily (until user close the website).
• PHP session technique is widely used in shopping websites
where we need to store and pass cart information e.g.
username, product code, product name, product price etc
from one page to another.
Real Example of Session:
 Website which require a user to login, use session to store
user information, so that on every webpage related
information can be displayed to the user.
 In ecommerce websites, shopping cart is generally saved as
part of session.

session_start():
 PHP session_start() function is used to start the session.
 It starts a new or resumes existing session.
 It returns existing session if session is created already.
$_SESSION:
 PHP $_SESSION is an associative array that contains all
session variables.
 It is used to set and get session variable values.
 Store the value like
$_SESSION["username"] = “brjoshi";
 Get the value like
echo $_SESSION["username"]; or
$user = $_SESSION["username"];
echo $user;
Session1.php
<?php
session_start();

$_SESSION["userid"]=“brjoshi”;
$_SESSION[“password"]=“12345678”;

?>

<html>
<body>
<?php
echo “Sessions are set”;
?>
<a href=“session2.php">Go to Second Page</a>
</body>
</html>
Session2.php
<?php
session_start();

$userid = $_SESSION["userid"];
$pass = $_SESSION[“password"];

?>

<html>
<body>
<?php
echo " User id is: ".$userid."<br/>";
echo “Password is: ".$pass;
print_r($_SESSION);
?>
<a href=“session1.php">Go to First Page</a>
</body>
</html>
Counter.php
<?php

session_start(); //start the PHP_session function

if(isset($_SESSION['page_count']))
{
$_SESSION['page_count'] += 1;
}
else
{
$_SESSION['page_count'] = 1;
}
echo 'You are visitor number ' . $_SESSION['page_count'];

?>
Delete Session (session_destroy()):
• The session_destroy() function is used to destroy the
whole Php session variables.
<?php
session_destroy(); //destroy entire session
?>

• If you want to destroy only a session single item, you use


the unset() function.

<?php
unset($_SESSION[“userid”]);
?>
PHP Predefined / Super Global Variable
Variable Description
$GLOBALS It reference to every variable which is currently
available within the global scope of the script.
$_SERVER This is an array containing information such as
headers, paths, and script locations.
$_GET An associative array of variables passed to the
current script via the HTTP GET method.
$_POST An associative array of variables passed to the
current script via the HTTP POST method.
$_FILES An associative array of items uploaded to the
current script via the HTTP POST method.
PHP Predefined / Super Global Variable
Variable Description
$_REQUEST An associative array consisting of the contents
of $_GET, $_POST, and $_COOKIE.
$_COOKIE An associative array of variables passed to the
current script via HTTP cookies.
$_SESSION An associative array containing session variables
available to the current script.
$_PHP_SELF A string containing PHP script file name in
which it is called.
$php_errormsg $php_errormsg is a variable containing the text
of the last error message generated by PHP.
Most Imp. Server Variable - $_SERVER
Variable Description
$_SERVER['PHP_SELF'] It is used to display the filename of
the currently executing script
$_SERVER['SERVER_ADDR'] It is used to display the IP address of
the host server
$_SERVER['SERVER_NAME'] It is used to display the host
server's name
$_SERVER['SERVER_SOFTWARE'] It is used to display the server
identification string.
$_SERVER['SERVER_PROTOCOL'] It is used to display the name and
revision of the information protocol
$_SERVER['REQUEST_METHOD'] It is used to display the request
method used to access the page. i.e.
'GET','POST'
Most Imp. Server Variable - $_SERVER
Variable Description
$_SERVER['HTTP_HOST'] It is used to display the Host header
from the current request
$_SERVER['HTTP_REFERER'] It is used to display the complete URL
of the current page
$_SERVER['SCRIPT_FILENAME'] It is used to display the absolute path
of the currently executing script
$_SERVER['SERVER_ADMIN'] It is used to display the value given to
the SERVER_ADMIN
$_SERVER['SCRIPT_NAME'] It is used to display the current
script's path
$_SERVER['SCRIPT_URI'] It is used to display the current page's
URI

You might also like