Create a Cookie in PHP

Create the cookie before sending HTML

Cookie headers must be sent before page output.

<?php
setcookie('Plus2netCookie', 'plus2net.com', [
    'expires' => time() + 3600,
    'path' => '/',
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Lax',
]);
?>
Cookie note: Secure cookies require HTTPS. Choose SameSite and HttpOnly settings according to how the cookie is actually used.

Current example first; the detailed tutorial examples below are retained for additional learning and comparison.

Enter Your Name
HTML code
<form method=post action=cookies_redirect.php>
Enter Your Name <input type=text name=name class='form-control'> 
<input type=submit value=Next class='btn btn-danger'>
</form>
Code for page cookies_redirect.php
<?php
$name=$_POST['name'];
if(isset($name) and strlen($name) > 1){
setcookie ("Plus2netCookie", $name,time()+3600);
header ("Location: cookies_t2.php"); 

}else{
echo " Input your name first and then click NEXT button 
<input type='button' value='Back' onclick='window.history.back()' />";
}
?>
Tutorial on PHP Cookies
DEMO of Data Array as Cookies Reading Cookies Deleting Cookies


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-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer