PHP cookies array adding and deleting

Basics of PHP Cookies
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);




plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer