SitePoint Sponsor

User Tag List

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

    Cookie doesn't work when included

    If I do a simple cookie check on a page it works fine, but if I place the same code in another file and include it, then it doesn't work. It acts as if there is no cookie.

    I have tried running the include file directly and then it also works fine - it finds the cookie. But not when the file is included from another page. Can included files not look for cookies?

  2. #2
    As the name suggests... trickie's Avatar
    Join Date
    Jul 2002
    Location
    Melbourne, Australia
    Posts
    678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try posting some code up. It is easier to spot problems, and work solutions.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, there's not much code

    How I include the file:
    PHP Code:
    INCLUDE($HTTP_SERVER_VARS['DOCUMENT_ROOT'] .'/wmi/cookies.php'); 
    Same way I include all other files, so there should be nothing wrong there.

    Code in included file:
    PHP Code:
    if( isset( $HTTP_COOKIE_VARS['username'] ))
    { echo 
    "Cookie found"; } else { echo "Cookie not found"; } 
    As said, it works when not included and I just run the file directly.

  4. #4
    As the name suggests... trickie's Avatar
    Join Date
    Jul 2002
    Location
    Melbourne, Australia
    Posts
    678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are there any errors?

    This is from the manual:

    Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser. This is the same limitation that header() has.
    have you taken that into consideration?

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are no errors and nothing is sent to the header first. As said, it isn't setcookie() which is the problem, but reading the cookie.

    However, I found out what the problem was. It was because I called the file with the database info from both the cookie page and the page which also called the cookie file. I solved it by removing the include for the database info in the cookie page and then call them both in the page that needs to check for cookies.

    I hope that made sense It might be obvious why this shouldn't be done but I can see why including the database info twice should cause any problems.


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
  •