SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP cookies array adding and deleting

We can add a set of data by array as cookies. Here is a sample code on how to use array as cookies. We will use one sample form to collect user entered details.

<form method=post action=cookies_redirect.php><font face='Verdana' size='2'>
Enter Your Name<input type=text name=name > <br>
Enter Your City Name<input type=text name=city><br>
Enter Your State Name<input type=text name=state><br>
Enter Your Zip Code<input type=text name=zip>
<input type=submit value=Next>
</font>
</form>

We will collect the details and then set the cookies and add them to user computer.

$name=$_POST['name'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];


if(isset($name) and strlen($name) > 1){
setcookie ("Plus2netCookie[name]", $name);
setcookie ("Plus2netCookie[city]", $city);
setcookie ("Plus2netCookie[state]", $state);
setcookie ("Plus2netCookie[zip]", $zip);
header ("Location: cookies_t2.php");


}else{
echo " Error in redirection ";

}

To read the array cookie we will use print_r command of PHP array

if(isset($_COOKIE['Plus2netCookie'])){

print_r($_COOKIE[Plus2netCookie]);

echo "<br><br><font face='Verdana' size='2'>Your name should appear after the welcome message above if cookie is available. </font>";
}else{
echo " No Cookie is set ";
}

We can delete the cookies like this.

setcookie ("Plus2netCookie", "", time() - 3600);
setcookie ("Plus2netCookie[zip]", "", time() - 3600);
setcookie ("Plus2netCookie[city]", "", time() - 3600);
setcookie ("Plus2netCookie[state]", "", time() - 3600);
setcookie ("Plus2netCookie[name]", "", time() - 3600);



Download the ZIP file here  cookies.zip



Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Join Our Email List
Email:  
For Email Newsletters you can trust
Cookies
PHP Sections