0% found this document useful (0 votes)
15 views10 pages

SESSIONS COOKIES in PHP

Uploaded by

imjyoti1511
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)
15 views10 pages

SESSIONS COOKIES in PHP

Uploaded by

imjyoti1511
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/ 10

Sessions in PHP

 A session is a small piece of information which is stored at


server side.
 Session is used to store and pass information from one
page to another temporarily (until user close the website).
 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.
 session_start() function is used to start the session. It
starts a new or resumes existing session.
 Example:
session_start();
 $_SESSION is an associative array that contains all
session variables. It is used to set and get session variable
values.
Example: Store information
$_SESSION["user"] = "Madhu";
Example: Get information
echo $_SESSION["user"];
Session1.php:
<html>
<body>
<?php
session_start();
$_SESSION["user"] = "Madhu";
?>
<a href="Session2.php">Visit next page</a>
</body>
</html>
Session2.php:
<html>
<body>
<?php
session_start();
echo "User is: ".$_SESSION["user"];
?>
</body>
</html>
output:
Destroying Session:
 session_destroy() function is used to destroy all session
variables completely.
Example:Session3.php
<?php
session_start();
session_destroy();
?>

• Unlike a cookie, the information is not stored on the users


computer.
ses1.php
<html>
<head>
<title>Session example</title>
</head>
<body>
<form name="form1" method="post">
Name : <input name="uname" type="text">
<br/><br/>
<input type="submit" name="Submit" value="SUBMIT">
</form>
<?php
if(isset($_POST['Submit']))
{
session_start();
$_SESSION["name"] = $_POST["uname"];
header('Location: ses2.php');
}
?>
</body>
</html>
ses2.php
<html>
<head>
<title>Welcome </title>
</head>
<body>
<br/>
<?php
session_start();
echo "Welcome : ".$_SESSION['name'];
?>
</body>
</html>
Cookies in PHP
• Cookie is a small piece of information which is stored
at client browser.
• Cookie is created at server side and saved to client
browser.
• First time when client sends request to the server, then
server embedded the cookie along with Response.
• Next time onwards client sends request along with
cookie to the server. Such way, cookie can be received
at the server side.
• A cookie is a small file that the server embeds on the
user's computer.
• With PHP, we can both create and retrieve cookie
values.
Note: Cookie must be used before <html> tag.

setcookie() function:
• setcookie() function is used to set/create cookie.
Syntax: setcookie(name, value, expire);
• Only the name parameter is required. All other parameters are
optional.
Example:
 setcookie("CookieName", "CookieValue");
/* defining name and value only*/
 setcookie("CookieName", "CookieValue", time()+1*60*
60);
//using expiry in 1 hour(1*60*60 seconds or 3600 seco
nds)
$_COOKIE Variable:
• $_COOKIE superglobal variable is used to get cookie.
i.e Once cookie is set, we can access cookie by
$_COOKIE superglobal variable.
Example:
$value=$_COOKIE["CookieName"];//returns cookie value
Example:
<?php
setcookie("user", "madhu");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["user"])) {
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
?>
</body>
</html>
Output:
Sorry, cookie is not found!
• Firstly cookie is not set. But, if you refresh the page, you will see
cookie is set now.
Output: Cookie Value: madhu

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy