PHP and working with Cookies
Working with cookies in php is rather simple, as is working with php in general.Cookie Basics:
Cookies are text files used to store data on a client's computer, allowing the ability to store visitor data during their visit, or across multiple visits. They are most commonly used for personalizing a site for the visitor, by storing user information so they don't need to log in every time they visit and have many other uses since you store almost any data in a cookie.
Creating, reading and deleting cookies is very simple to do with PHP.
Creating a cookie:
Before you start, there is one important rule: The cookie code must be the first line of your php document unlike with javascript. It must be before the head of your page, and before any other code on the page. If not, you will get a "Headers already sent" error. So to set a cookie, your page should look like this:
<?php
setcookie ("cookieName", "This will be the data stored in the cookie");
?>
<html>
<head>
<title>Page Title</title>
</head>
<body>
This stores a cookie name or variable name in a txt file on the user's computer with the name "cookieName" which contains the value "This will be the data store in the cookie". Of course this is just a string of text which is actually useless. A cookie would usually look something more like a query string of varying data, like this.
<?php
setcookie ("userCookie", "username=name|pswd=password|data=something");
?>
This cookie data is constructed with a few variables which would need to parsed through to obtain each value's name and assigned value once you have read in the string.
The cookies created above we created as session cookies, because there was not an expiration date or time set to cookie. So when a client leaves your site the cookie will no longer be active.
To set the expiration of cookie. You add to end of the cookie a date() or time() function as follows.
<?php
setcookie ("userCookie", "username=name|pswd=password|data=something", time() + 900);
?>
This set's the cookie to expire in 15 minutes. 60 seconds * 15 = 900.
<?php
setcookie ("userCookie", "username=name|pswd=password|data=something", time() + (7 * 24 * 60 * 60));
?>
Here we set the cookie to expire in one week's time from now. We could also write that like using the date() function.
<?php
setcookie ("userCookie", "username=name|pswd=password|data=something", date('Y,m,d, strtotime('+ 1 week') );
?>
Now that we have cleared up on how to create a cookie. Let's read the cookie's. That's rather simple.
<?php
echo $_COOKIE["cookieName"]; // Display "This will be the data stored in the cookie"
echo $_COOKIE["userCookie"]; // Display "username=name|pswd=password|data=something"
$ckData = $_COOKIE["userCookie"]; // Store the userCookie data in a string called $ckData which now allows you to parse the string for all content of the cookie string.
?>
That's pretty much wraps up php and cookie's.
Before I go let me show you how to delete cookie using php.
<?php
setcookie ("userCookie", "", time( ) - 1);
?>
Just set the cookies time to a negative value and the cookies crumbles.
setcookie ("cookieName", "This will be the data stored in the cookie");
?>
<html>
<head>
<title>Page Title</title>
</head>
<body>
This stores a cookie name or variable name in a txt file on the user's computer with the name "cookieName" which contains the value "This will be the data store in the cookie". Of course this is just a string of text which is actually useless. A cookie would usually look something more like a query string of varying data, like this.
<?php
setcookie ("userCookie", "username=name|pswd=password|data=something");
?>
This cookie data is constructed with a few variables which would need to parsed through to obtain each value's name and assigned value once you have read in the string.
The cookies created above we created as session cookies, because there was not an expiration date or time set to cookie. So when a client leaves your site the cookie will no longer be active.
To set the expiration of cookie. You add to end of the cookie a date() or time() function as follows.
<?php
setcookie ("userCookie", "username=name|pswd=password|data=something", time() + 900);
?>
This set's the cookie to expire in 15 minutes. 60 seconds * 15 = 900.
<?php
setcookie ("userCookie", "username=name|pswd=password|data=something", time() + (7 * 24 * 60 * 60));
?>
Here we set the cookie to expire in one week's time from now. We could also write that like using the date() function.
<?php
setcookie ("userCookie", "username=name|pswd=password|data=something", date('Y,m,d, strtotime('+ 1 week') );
?>
Now that we have cleared up on how to create a cookie. Let's read the cookie's. That's rather simple.
<?php
echo $_COOKIE["cookieName"]; // Display "This will be the data stored in the cookie"
echo $_COOKIE["userCookie"]; // Display "username=name|pswd=password|data=something"
$ckData = $_COOKIE["userCookie"]; // Store the userCookie data in a string called $ckData which now allows you to parse the string for all content of the cookie string.
?>
That's pretty much wraps up php and cookie's.
Before I go let me show you how to delete cookie using php.
<?php
setcookie ("userCookie", "", time( ) - 1);
?>
Just set the cookies time to a negative value and the cookies crumbles.
2whoa.com - NEW!!
Home
jQuery
Javascript
Archives
MLB Wallpapers
NFL Wallpapers
NBA Wallpapers
NHL Wallpapers
Flames Wallpaper
Watch Live Games!
NHL
NBA
Soccer
NFL
and more..
BMW, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Home
jQuery
Javascript
Archives
MLB Wallpapers
NFL Wallpapers
NBA Wallpapers
NHL Wallpapers
Flames Wallpaper
Watch Live Games!
NHL
NBA
Soccer
NFL
and more..
BMW, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota



Leave a comment
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota