Results 1 to 8 of 8

cookies in PHP?

This is a discussion on cookies in PHP? within the Programming forums, part of the Web Designing & Development category; How to set cookies in PHP?...

  1. #1
    Junior Member
    Join Date
    Mar 2016
    Posts
    20

    Default cookies in PHP?

    How to set cookies in PHP?

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Posts
    129

    Default Re: cookies in PHP?

    Cookies are bits of data that a web browser stores on your visitor's computer. They can be very useful if you need to store things like your visitor's preferences or login data (if your site has a membership facility) or other things that are specific to a particular visitor. This tutorial teaches you how you can set cookies, retrieve them and delete them using PHP.

    Setting a cookie with PHP could not be easier, since PHP provides a function for you to do precisely that. The following code sets a cookie called "userlogin" with a value of "anonymous":
    $date_of_expiry = time() + 60 ;
    setcookie( "userlogin", "anonymous", $date_of_expiry );
    The code starts by calculating the expiry date of the cookie. Cookies have a limited lifespan. If you do not set an expiry date, the cookie will expire automatically when the user closes his/her web browser. The expiry date has to be in a special format, so it's actually simplest to just use the time() function and work from there. This function returns the current date and time in the required format. My code adds 60 seconds to the existing time, effectively making the cookie last for only 1 minute.
    The second line calls the setcookie() function, which does the actual work of setting the cookie in PHP. This is a built-in function in PHP. The first parameter (or argument) to setcookie() is the name that you want to give the cookie. It can be any name you like. In the example above, I gave the cookie the name "userlogin".
    The second parameter to the setcookie() function contains the actual data that you want saved. Again, this can be any data you like, although the maximum size of any cookie is 4 KB. This 4 KB includes things like the date of expiry, the name, and other cookie overheads, so you don't really have all 4,096 bytes to work with. Note that cookies are not encrypted by default, so unless you encrypt your data yourself, you should not store any sensitive information in them.
    The third argument is the date of expiry that was calculated earlier. As noted earlier, my code gives the cookie a very short lifespan. If you want your cookie to last longer, and you surely will, you will have to add the lifespan you want, converted to seconds, to the value returned by time().
    Here's an example of how to do that using a new variable, $number_of_days. Set the $number_of_days variable to the number of days you want your cookie to last, and the code below will calculate the actual date of expiry for you in a format suitable for passing to the setcookie() function.
    $number_of_days = 30 ;
    $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days ;
    You will of course have to pass $date_of_expiry to setcookie() as its third parameter.

  3. #3
    Junior Member
    Join Date
    Apr 2017
    Posts
    10

    Default Re: cookies in PHP?

    I'm new and I'm interested in this topic. Thank you!

  4. #4
    Member
    Join Date
    Sep 2016
    Posts
    46

    Default Re: cookies in PHP?

    A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer.

  5. #5
    Senior Member
    Join Date
    Nov 2016
    Posts
    148

    Default Re: cookies in PHP?

    A good one.

    Quote Originally Posted by mspcarservice View Post
    A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer.

  6. #6
    Junior Member
    Join Date
    Oct 2017
    Posts
    7

    Default Re: cookies in PHP?

    Here is the example an format for setting cookies in php :

    <!DOCTYPE html>
    <?php
    $cookie_name = "user";
    $cookie_value = "John Doe";
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
    ?>
    <html>
    <body>

    <?php
    if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
    } else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
    }
    ?>

    </body>
    </html>

  7. #7
    Newbie
    Join Date
    Oct 2017
    Posts
    3

    Default Re: cookies in PHP?

    Cookies are text files stored on the client computer and they are kept on use tracking purpose. PHP transparently supports HTTP cookies.


    Setting a cookie requires a key, a value, and the amount of time to allow the cookie to exist.


    $first_name = 'jessica';
    setcookie('first_name',$first_name,time() + (86400 * 7)); // 86400 = 1 day

  8. #8
    Member
    Join Date
    Jul 2017
    Posts
    85

    Default Re: cookies in PHP?

    A cookie is often used to recognize a user. A cookie is a small file that the server embeds on the user's computer.