SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Cookie Question

    I'm very new to using cookies in php and I was trying out some stuff on my site. What I've done is when an admin logs in, it automatically sets a cookie storing the persons username. When this then goes to the index page where the admin link is, it checks for the cookie's value. But I'm getting the following error when I try to write the cookie:

    Warning: Cannot add header information - headers already sent by (output started at c:\apache\htdocs\login.php:10) in c:\apache\htdocs\login.php on line 96
    Here is my code for the login page.

    PHP Code:
    // Checks passwords for validity.
          if ($uPassword != $password) {
            echo ("<h5>Error: the specified password does not match the username.  <a href='login.php' style='font-size: 10px'>Retry</a>.</h5>");
        } else {
            echo("<h5>Thank you for logging in: " . $username . ".</h5>");

            setcookie("username", "$username", time()+315360000);
    ?>

    <center>
        <form action="index.php" method="post">
        <input type="hidden" readonly="true" name="username" value="<?=$username ?>">
        <input type="submit" value="Continue" style="font-family: verdana" />
        </form>

    </center>

    <?php     
        
    }
        endif;    
    ?>
    And here is the code for the index page.

    PHP Code:
    <?php

    /***************************************************************************
     *              Cookie Begins.            
     ***************************************************************************/

            
    echo $_COOKIE["username"];
            
    $_COOKIE['username'] = $username;
            
        
    /***************************************************************************
     *              Cookie Ends.            
     ***************************************************************************/

    ?>
    Note that the echo $_COOKIE was for debugging purposes.

    Any help would be much appreciated.

  2. #2
    SitePoint Enthusiast scriptsmiths's Avatar
    Join Date
    Aug 2003
    Location
    South Africa
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    You must send your cookies before outputting any content.

    Cookies are part of the HTTP headers, and PHP ends these as soon as the first bit of content s printed to the browser.

    Try:

    } else {
    setcookie("username", "$username", time()+315360000);
    echo("<h5>Thank you for logging in: " . $username . ".</h5>" );
    ?>

    Just swap the setcookie and echo calls.

    Make sure theres no other output before this tho.
    Also watch out for things like blank lines before the first php tag in your file, this could also cause this, as php reads this as content to send. The <?php must be on the very first line in the very first column.

    Thanks
    Mark


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •