Answers for "how to setup cookies php"

PHP
2

set cookies function in php

//Parameter of Cookie 
//only first line is usable other lines is for descrption
setcookie($cookiename ,$cookievalue , time() + (86400 * 10) , "/" , domain.com ,True , False);
  //Explanation
setcookie(name , value, time, path, domain , secure, httponly)
  1.name is the name of cookie
  2.value is the value that you want to save in cookie
  3.time is expire time of cookie and it is set in sec so 86400 sec is
  equal to 1 day time() function get the current time and 86400 * 10 means
  after 10 days cookie will be expire
  4.path is path of  website to access coookie if we use "/" it means we can 
    access cookie from every page
  5.domain is the domain from which you want to access the cookie if we use 
    domain then we only access cookie from that specific domain
  6.secure means HTTPs protocol if its True it means cookie only set if its 
    HTTPs otherwise cookie cannot set
  7.HTTPonly means if its false we can access cookie from localsite(javascript)
    and serversite but if its Ture other wise from only serversite (php)
Posted by: Guest on August-16-2021
6

php cookies

//Cookies
//Cookies are stored on the client side. cookies are not as secure as sessions
//and it is recommended that you use sessions as much as possible.

<?php
if(isset($_POST['submit'])){
    $username = htmlentities($_POST['username']);

    setcookie('nameofcookie', $username, time()+3600); 
    //1hour time limit

    header('Location: page2.php');
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>PHP Cookies</title>
</head>
<body>
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
                <input type="text" name="username" placeholder="Enter Username">
                <br>
                <input type="submit" name="submit" value="Submit">
            </form>
        </div>
</body>
</html>
Posted by: Guest on May-13-2020

Browse Popular Code Answers by Language