faqts : Computers : Programming : Languages : PHP : Common Problems : Cookies

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

20 of 27 people (74%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

What is the client server sequence for setting a cookie?
How can I check if a browser accepts cookies?

Jul 8th, 1999 21:26
Nathan Wallace,


Here is the cookie setting sequence in PHP:

    Client                      Server

    requests page
                                gets request
                                sets cookie in reply
                                sends reply
    gets reply (with cookie)
    sets cookie on machine

    sends new request (with
        cookie included)
                                gets request
                                finds cookie in request
                                sets variable in script
                                runs script

The server has to interact with the client to be able to set a cookie or
determine if it has been set.

From http://www.php3.com/manual/function.setcookie.php3

    <?php

    $status = 0;
    if (isset($myTstCky) && ($myTstCky == "ChocChip")) $status = 1;
    if (!isset($CCHK)) {
        setcookie("myTstCky", "ChocChip");
        header("Location: $PHP_SELF?CCHK=1");
        exit;
    }
    ?>

    <html>

    <head><title>Cookie Check</title></head>
    <body bgcolor="#FFFFFF" text="#000000">
    Cookie Check Status:

    <?php
        printf ('<font color="#%s">%s</font><br>;',
            $status ? "00FF00" : "FF0000",
            $status ? "PASSED!" : "FAILED!");
    ?>

    </body>
    </html>