Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 3 of 3

This is a discussion on setcookie isn't working in the Shared & Semi-Dedicated forum
I'm running into a problem when I try and implement cookie based logins (so people can stay logged in after they close their browser blah ...

  1. #1
    JPC Member
    Join Date
    Oct 2002
    Posts
    4

    setcookie isn't working

    I'm running into a problem when I try and implement cookie based logins (so people can stay logged in after they close their browser blah blah blah). I'm probably just making a newb problem here, but I have chopped down my code to the following, but it keeps giving me an error when i do "setcookie". Any suggestions on how to fix this? I really appreciate it guys, thanks!

    <?
    //check to see if the cookie is set
    $cookiename = $_COOKIE['duser'];
    $cookiepassword = $COOKIE['dpass'];
    if(isset($cookiename)){
    echo "Welcome $cookiename";
    exit();
    }
    //do this after the login is pressed
    if (isset($login)){
    echo "You put in $formUser for your username <br>";
    echo "You put in $formPass for your password <br>";
    setcookie ('duser', $formUser,time()+3600, '/','drewlink.com') or exit("could not set cookie");
    setcookie ('dpass', $formPass,time()+3600, '/','drewlink.com') or exit("could not set cookie");
    echo "Cookies have been set";
    }

    ?><form action="cookie1.php?login=1" method="post">
    Username: <input type="text" name="formUser">
    <p>Password: <input type="password" name="formPass"></p>
    <p><input type="submit" name="submit" value="login"></p>
    </form>

  2. #2
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    Any setcookie() calls have to to be made before anything is written to the screen. This is because the cookies are sent in the HTTP headers, which as their name implies, are sent before the portion of the page that your user actually sees. Just make sure your setcookies() all come before any echo() or print() calls, any text that is outside of the <? ... ?>'s, or any thing else that writes directly to the browser, such as fpassthru() or readfile().

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  3. #3
    JPC Member
    Join Date
    Oct 2002
    Posts
    4
    Perfect...Thanks Jason!

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
  •