SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast Scripting X's Avatar
    Join Date
    May 2001
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting a cookie (hmmm)

    Hi everyone,

    I am having a slight problem with cookies/php/mysql

    My problem is that, I have a login system, when a user logs in, what it does is redirect to the main page (index.php), on index.php, it calls all my other pages (creates a template), one of the template pages is called title.php, on it I have this script...

    Code:
    $query = "SELECT * FROM sessions WHERE user_id = '$vuser_id'";
    $result = mysql_query($query) or die (mysql_error());
    $row = mysql_fetch_array($result);
    
    $row_user_id = $row['user_id'];
    
    if($row_user_id = "") {
    
    setcookie("time",$PHPSESSID,time()+60,"/",".scriptingx.com",0); 
    
    $time=$HTTP_COOKIE_VARS["time"]; 
    $timesl=strlen($time); 
         if($timesl<1) {
    
    $sql = "DELETE FROM sessions WHERE user_id = '$vuser_id'";
    $result = mysql_query($sql, $link) or die("There has been an error whilst attempting to insert the information into our database...<br><br>Please <A HREF=\"$HTTP_REFERER\">go back</a> and try again. If the problem persists contact us at <a href=\"mailto:support@scriptingx.com?subject=Error on $PHP_SELF&body=I recieved this error whilst on Scripting X!!! $sql .mysql_error();\">support@scriptingx.com</a>.<br><br>
    $sql ".mysql_error());
    
            }
    
    else
    
    {
    
    setcookie("time",$PHPSESSID,time()+60,"/",".scriptingx.com",0); 
    
    }
    
    }
    Any ideas as to why this doesn't work ? what I am trying to do is, check if the user exists in sessions ($vuser_id is a session registered ID btw), and then if the user does exist set a cookie which lasts for 60 seconds, because the user will travel between different pages, but title.php will be called on each page the cookie should reset itself back to 60 seconds, however; if the user does not go onto a different page within 60 seconds then the session is deleted from the DB.

    My problem is basically that when I check my temp internet folder, there is no cookie there, and I have waited several minutes without travelling to a different page and it still shows me as online!?!? when it should have logged me out after 60 secs of inactivity.

    I am wondering whether someone could shed some light onto the current situation, and possibly create a fix for my script.

    Thanks in advance,
    Chris Morley.
    cgi://www.scriptingx.com - Everything CGI

  2. #2
    SitePoint Enthusiast Scripting X's Avatar
    Join Date
    May 2001
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Anyone?

    Does anyone have any ideas ?

    Thanks, Chris.
    cgi://www.scriptingx.com - Everything CGI

  3. #3
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sending the cookie and a header('Location: ...'); thing at the same time? If so there's an "issue" with doing that which can prevent the cookie from setting. I always generate a simple HTML page with a <meta redirect> thing after setting a cookie rather than using the header-location method and it seems to work fine.

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2000
    Location
    Seattle, WA area
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out what time zone settings your server is using. If your server is on EST or something like that, most cookies probably won't be set correctly, as they usually need to be set with GMT time in mind.

    If your server is on a different time than GMT, you may need to offset the time you send out for the cookie expiration date. I don't know if this will apply to you, but after many hours of painful debugging, I've found that timezones do seem to come into effect when setting cookies, especially short ones like that.
    David Baxter
    Freelance PHP/MySQL Developer
    Contact me for more information: [ Email ] [ICQ: 47136518]

  5. #5
    SitePoint Enthusiast Scripting X's Avatar
    Join Date
    May 2001
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hmmm

    I just don't see why it won't work, all i want to know is why it won't set a cookie which lasts for 60 secs then dies...odd...

    cgi://www.scriptingx.com - Everything CGI


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
  •