|
After a request from script or a server user's browser stores small amount of data, This data is called cookie.
A D V E R T I S E M E N T
|
Cookie can be accessed in three ways by a PHP script:
|
In a cookie there are information about a name, value, expiry date, host and path.
As cookies are sent from server through HTTP header, they end up to the user.
Here are the 3 ways to access cookies:
- using �$HTTP-COOKIE� which is the environmental variable, all cookie names
and values are present in this variable.
-
using global variable �$cookie_name�, here the name should be replaced
- using �HTTP_COOKIE_VARS [�cookie_name�]� which is a global array variable.
(here replace the �cookie_name� by actual name of the cookie).
|
print $HTTP_COOKIE; //outputs �visits=23�
print getenv(�HTTP_COOKIER�); //outputs �visits=23�
print $visits; //outputs �23�
print $HTTP_COOKIE_VARS[visits]; //outputs �23�
|
|
How to Set a Cookie with PHP
|
�header()� function, or �setcookie()� function can be used to set cookie with PHP.
The main purpose of �header()� function is not to set a cookie, and works
just like �setcookie()�. Cookie header is written by ourself by using �header()� funtion, but �setcookie()� is much automated.
|
//don�t output anything before this...
header(�visits=23; expires=Friday, 15-Nov-06 03:27:21 GMT; path=/;
domain=softwareprojects.org�);
setcookie(�hits�, 23, time() + 3600, �/�, �softwareprojects.org�, 0);
//notice this last extra argument
|
|
Weather the cookies will be send over a secure connection or not is denoted
by the last argument to "setcookie()" function. Here "0" means no and "1" means yes.
|
How to Retrieve a Cookie Value
|
To retrieve a cookie value, PHP $_COOKIE variable is used.
In the following example the value of cookie named "user" is retrieved and displayed on the page.
|
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
|
|
How to Delete a cookies
|
To delete a cookie we should SET the cookie we want to delete with the date that has
already expired. While doing so we should include the same path, secure parameters and domain
which was originally used to set the cookie.
|
setcookie(�visits�, 23, time() - 60, �/�,�vyom.co.in�, 0)
|
|
Weather the cookies will be send over a secure connection or not is denoted
by the last argument to "setcookie()" function. Here "0" means no and "1" means yes.
|
Limitations of cookies
|
Apart from the advantage of passing information from one page to another page
or visit to visit, Cookies do have some limitations. The maximum number of cookies that
can be stored by the browser is 20.And maximum size of the cookie is 4KB.
The user's privacy is maintained since only the originating host can read the data that has been stored.
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:php tutorial, php scripts, php nuke, php download, php editor, php mysql, php forum,
php add link, learn php, php code
|
|
HTML Quizzes |
|
XML Quizzes |
|
Browser Scripting Quizzes |
|
Server Scripting Quizzes |
|
.NET (dotnet) Quizzes |
|
Multimedia Quizzes |
|
Web Building Quizzes |
|
Java Quizzes |
|
Programming Langauges Quizzes |
|
Soft Skills Quizzes |
|
Database Quizzes |
|
Operating System Quizzes |
|
Software Testing Quizzes |
|
SAP Module Quizzes |
|
Networking Programming Quizzes |
|
Microsoft Office Quizzes |
|
Accounting Quizzes |
|
|