Library tutorials & articles

An Introduction to PHP

Cookies in PHP

Setting Cookies

The world wide web is stateless, which means it remembers nothing about you. The usual way of keeping track of user preferences is to use cookies. If you are setting a cookie, you must do it before the headers are written, so must be set at the very start of the document. Cookies are set in PHP using the setcookie function.

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

The cookies are not actually set in the page where they are written, but will be set in any subsequent pages. The following example sets a userName cookie, that expires after one hour.

<?php
    // Set a cookie that expires in one hour
    setcookie("userName", $name, time()+3600);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PHP Cookies</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
A cookie was set on this page, and will be active when
the client has sent the cookie back to the server.
</p>
</body>
</html>

Reading Cookies

When a cookie is set, PHP uses the cookie name as a variable with the appropriate value. Accessing a cookie is just a simple case of referring to the cookie name as a variable. You can use the isset function to determine if a cookie has been set. The following example tests if the userName cookie has been set, and prints an appropriate message.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Accessing a Cookie</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
<?php
    if (isset($userName))
        print "Welcome " . $userName . "<br>";
    else
        print "You are not logged in <br>";
?>
</p>
</body>
</html>

Comments

  1. 28 May 2009 at 22:12

    It is not just talk though: Zend server is flourishing despite the recession - "the Q1 of 2009 has been our strongest quarter ever," says Suraski - and although PHP's enterprise market share is small compared to Java or .NET, it is growing. Read more on ITJOBLOG.

  2. 02 Jan 2007 at 06:39
    Here they show how to send valued in one file to another(both are in same folder)
    but i need to send values in one folder to another


  3. 04 Apr 2006 at 10:20

    Yes, I think you're correct, although you could easily have a line like this:

    $num = $_POST['num'];


    to get around that problem. Which I think is what's missing from this tutorial Wink [;)]





  4. 04 Apr 2006 at 09:47

    In the PHP file should it be "$_POST["num"]" ?

    instead of just $num?

  5. 21 Feb 2004 at 04:09
    hello

    I have in php a header which brings a save as box , but when file is down loaded it contains the 6 blank spaces at the start of of document .
    I have to avoid storing these blank spaces.Please help me to solve this problem.
  6. 28 Jan 2004 at 15:16

    You can also do string concatenation like

    Code:

    $h = "Hello";
    $w = "World";


    $message = "$h $w";


  7. 01 Jan 1999 at 00:00

    This thread is for discussions of An Introduction to PHP.

Leave a comment

Sign in or Join us (it's free).

Gez Lemon I'm available for contract work. Please visit Juicify for details.
AddThis

Related discussion

Related podcasts

  • EarthClassMail.com - Moving from LAMP to .NET 3.5

    Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.

Events coming up

  • Dec 3

    The Auckland PHP December meetup

    Auckland, New Zealand

    Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.

Want to stay in touch with what's going on? Follow us on twitter!