SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie questions

    I am trying to get cookies working with a login page.

    I have read a lot of tutorials on the net, but I just doesn't get all of it.

    It seems that a rule is that you can't have any printable characters before setting a cookie. My question is then, what if you use templates for your site? (include()). Also, you have to use a form (html) for users to type their username/password before setting the cookie, so how is this done?

    Also, if I want any scripts on my domain to be able to access the cookie, is it right to use '/' then? Example:

    PHP Code:
    setcookie("testcookie""value"time() + (86400), '/''.domain.com' ); 
    Thanks in advance.

  2. #2
    Almeaty Member
    Join Date
    Jul 2000
    Location
    Nowhere
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Cookie questions

    Originally posted by Dalixam
    I am trying to get cookies working with a login page.

    I have read a lot of tutorials on the net, but I just doesn't get all of it.

    It seems that a rule is that you can't have any printable characters before setting a cookie. My question is then, what if you use templates for your site? (include()). Also, you have to use a form (html) for users to type their username/password before setting the cookie, so how is this done?
    Yep that's right, no headers can come before setcookie() is called. If you want to use include() it depends on what the included file does. If it outputs characters then the script will fail.

    What include() does is almost the same as writing the included content directly into your script.

    To allow user login try the following:

    PHP Code:
    if ($HTTP_POST_VARS['submit_login'])
    {
      
    // your code to check if logins are valid ( else die(); )

      
    setcookie(...); // set the cookie
      
    header("Location: $PHP_SELF"); // redirect to myself
      
    exit;
    }

    <
    html>
    ...
    </
    html
    Note that freshly set cookies will not be available until the next page load.
    Last edited by dekuro; Feb 12, 2002 at 17:00.


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
  •