Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create/modify/expyire a cookie in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 107
    Comment on it

    To know what is a cookie and about the optional parameters for the cookie see click the link below.
    http://findnerd.com/list/view/What-is-a-cookie/13459/

    Here we will see how to create a cookie and modify the cookie and how the cookie expires.For creating a cookie we us a php function setcookie().

    The syntax for the cookie function is below in which all the parameters are optional other than "name".

    setcookie(name, value, expire, path, domain, secure, httponly);
    

    1-Creating a cookie

    <?php
    $credentialone = "userinfo";
    $credentialtwo = "vijaykant";
    setcookie($credentialone, $ credentialtwo, time() + (86400 * 30), "/");
    ?>
    
    <html>
    <body>
    <?php
    if(!isset($_COOKIE[$credentialone])) {
     echo "Cookie name is not set..!";
    } else {
     echo "Cookie name is set as:".$_COOKIE[$credentialone];
    echo "and the value of cookie name is: " . $_COOKIE[$credentialtwo];
    }
    ?>
    </body>
    </html>
    

    Note: we can also modify the cookie name using setcookie().


    2- Deleting the cookie

    For deleting the cookie we again use the setcookie() function In set cookie function we set the past time to expire the cookie;

    For Example:

    <?php
    setcookie($credentialone, $credentialtwo, time() - 3600);
    ?>
    <html>
    <body>
    
    <?php
    echo "Cookie with name. $_COOKIE[$credentialtwo].  is expired.";
    ?>
    
    </body>
    </html>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: