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>
DEMO: Setting data array Cookies
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);
DEMO: Setting data array Cookies



Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











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