0% found this document useful (0 votes)
41 views6 pages

Cookies

The document explains how to set cookies in PHP using the setcookie() function, which requires up to six arguments and must be called before the <html> tag. It also covers starting a PHP session with session_start(), accessing session variables through the $_SESSION array, and destroying sessions with session_destroy() or individual session variables using unset(). Key details include cookie attributes like name, value, expiry, path, domain, and security settings.

Uploaded by

neelesh1222
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)
41 views6 pages

Cookies

The document explains how to set cookies in PHP using the setcookie() function, which requires up to six arguments and must be called before the <html> tag. It also covers starting a PHP session with session_start(), accessing session variables through the $_SESSION array, and destroying sessions with session_destroy() or individual session variables using unset(). Key details include cookie attributes like name, value, expiry, path, domain, and security settings.

Uploaded by

neelesh1222
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/ 6

Cookies




Setting Cookies
 PHP provide setcookie() function to set a cookie. This function requires upto six
arguments and should be called before <html> tag. For each cookie this function has to be
called separately.
Setcookie(name, value, expire, path, domain, security)
 Name – this sets the name of the cookie and is stored in an environment variable called
HTTP_COOKIE_VARS. This variable is used while accessing cookies.
 Value – this sets the value of the named variable and is the content that you actually want
to store.
 expiry – this specify a future time in seconds since 00:00:00 GMT on 1st Jan 1970. after
this time cookie will become inaccessible. If this parameter is not set the cookie will
automatically expire when the web browser is closed.
Setting Cookies
Path – this specifies the directories for which the cookie is valid. A single
forward slash character permits the cookie to be valid for all directories.
Domain – This can be used to specify the domain name in very large domains
and must contain at least two periods to be valid. All cookies are only valid for
the host and domain which created them.
Security – this can be set to 1 to specify that the cookie should only be sent
by secure transmission using HTTPS otherwise set to 0 which mean cookie
can be sent by regular HTTP.

Setcookie (“name”, “Raj”, time()+3600, “/”, “”, 0);


Starting a PHP session
A PHP session is easily started by making a call to the session_start()
function.
This function first checks if a session is already started and if none is
started then it starts one.
It is recommended to put the call to session_start() at the beginning
of the page.
Session variables are stored in associative array called $_SESSION[].
These variables can be accessed during lifetime of a session.
Make use of isset() function to check if session variable is already set
or not.
Destroying a php session
A PHP session can be destroyed by session_destroy() function. This
function does not need any argument and a single call can destroy all the
session variables.
<?php
php session_destroy();
?>
If you want to destroy a single session variable then you can use unset()
<?php
unset($_SESSION[‘counter’]);
?>

You might also like