SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My cookies are expiring at the end of the session

    I'm using the following code to set a cookie:

    PHP Code:
       $expire time()+60*60*24*90;
       
    $cookiestring "mycookievalue";
       
    setcookie("cookiname"$cookiestring$expire"/"".mydomain.com"00); 
    The thing is, that the cookie expires at the close of the browser--in every browser. If I go see the cookie in Firefox, it even says that it expires at the end of the session.

    I really don't understand what I'm doing wrong.

  2. #2
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    Works perfectly for me.

    I put that code in a file called setcookie.php on my testing virtual machine, and changed the domain value to match my testing domain.

    I put <?PHP print_r($_COOKIE) in another file called viewcookie.php.

    I visited setcookie.php in my browser, closed my browser and opened it again - then visited viewcookie.php. It showed me the cookie name and value as expected. It also had an expiry date next February (90 days).

    Make sure you domain matches your testing environment.

  3. #3
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried putting it in a file by itself like you say you did. Still won't work.
    Could it be a problem with the server or some php.ini settings?

    The domain matches, I even tried it without all those extra parameters and it still won't go. I'm totally confused.

  4. #4
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    Is there a chance something else is removing it, or that you're running the function after there has been something output to the browser?

  5. #5
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do outputs to the browser also include <html> and <head> tags too?
    They're not outputted by PHP but are at the top of the page.
    I'll have to try changing that and see what happens.

  6. #6
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    Yup, anything outside of php tags is considered output, even something as simple as a space!

  7. #7
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sigh... still doesn't work.
    Just don't understand how the cookie is set fine, and all the other values are passed along correctly besides the expiration date.

  8. #8
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    Can you post your full code here?

  9. #9
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm basically trying to create a rating system for my site.

    This is what I've got so far. It's a bit convoluted and probably has a bunch of vulnerabilities, but it's got a long way to go.

    The first part calculates the rating, then there's an if. If $_GET[rating] is not set display the stars (with a check to see if it has been rated already), then if the rating is set, add or update the cookie and put it in the db.

    PHP Code:
    <?php

    $id 
    strip_tags($_GET[id]);

    if(
    is_numeric($id)) {

    $type strip_tags($_GET[type]);

    function 
    round_to_half($num)
    {
     if(
    $num >= ($half = ($ceil ceil($num))- 0.5) + 0.25) return $ceil;
     else if(
    $num $half 0.25) return floor($num);
     else return 
    $half;
    }

    if (
    $_GET[rating] == "") {

    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
     <title></title>
     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
     <link rel="stylesheet" href="rating.css" type="text/css">
     <script type="text/javascript" src="script.js"></script>
    </head>
    <body style="background-color:#202020;">

    <?php

     
    //get variables
     
    $type $_GET[type];
     
    $id $_GET[id];

     
    //connect to db
     
    mysql_connect("[...]") or die(mysql_error());
     
    mysql_select_db("[...]") or die();

     
    //get row
     
    $result mysql_query("[...]") or die(mysql_error());
     
    $row mysql_fetch_array($result);

     
    //calculate rating
     
    $rating substr(($row[total] / $row[votes]) / 2,0,4);

     
    //round to nearest half
     
    $rating round_to_half($rating);

     
    /*
     while($rating > 0) {
      if($rating >= 1) {
       echo "1, ";
       $rating--;
      } else {
       echo "half";
       $rating = $rating - 0.5;
      }
     }
     */

     
    $width $rating 20;

     if (isset(
    $_COOKIE["cc-rating"])) {

     
    $pos strpos($_COOKIE["cc-rating"],$type.$id);
      if(
    $pos === false) {

      
    //if not voted, display with links
      
    echo "
      <ul class='star-rating'>
       <li class='current-rating' style='width:"
    .$width."px;'></li>
       <li><a href=\"rating.php?rating=1&type="
    .$type."&id=".$id."\" title='1 star' class='one-star'>1</a></li>
       <li><a href=\"rating.php?rating=2&type="
    .$type."&id=".$id."\" title='2 stars' class='two-stars'>2</a></li>
       <li><a href=\"rating.php?rating=3&type="
    .$type."&id=".$id."\" title='3 stars' class='three-stars'>3</a></li>
       <li><a href=\"rating.php?rating=4&type="
    .$type."&id=".$id."\" title='4 stars' class='four-stars'>4</a></li>
       <li><a href=\"rating.php?rating=5&type="
    .$type."&id=".$id."\" title='5 stars' class='five-stars'>5</a></li>
      </ul>"
    .$row[votes]." votes";

      } else {

      
    //if voted, display without links
      
    echo "
      <ul class='star-rating'>
       <li class='current-rating' style='width:"
    .$width."px;'></li>
       <li><a href=\"#\" title='Already voted' class='one-star'>1</a></li>
       <li><a href=\"#\" title='Already voted' class='two-stars'>2</a></li>
       <li><a href=\"#\" title='Already voted' class='three-stars'>3</a></li>
       <li><a href=\"#\" title='Already voted' class='four-stars'>4</a></li>
       <li><a href=\"#\" title='Already voted' class='five-stars'>5</a></li>
      </ul>"
    .$row[votes]." votes<br><span style=\"color:#5F5F5F;\">(including you)</span>";

      }

     } else {

     
    //if no cookie, display with links
     
    echo "
      <ul class='star-rating'>
      <li class='current-rating' style='width:"
    .$width."px;'></li>
      <li><a href=\"rating.php?rating=1&type="
    .$type."&id=".$id."\" title='1 star' class='one-star'>1</a></li>
      <li><a href=\"rating.php?rating=2&type="
    .$type."&id=".$id."\" title='2 stars' class='two-stars'>2</a></li>
      <li><a href=\"rating.php?rating=3&type="
    .$type."&id=".$id."\" title='3 stars' class='three-stars'>3</a></li>
      <li><a href=\"rating.php?rating=4&type="
    .$type."&id=".$id."\" title='4 stars' class='four-stars'>4</a></li>
      <li><a href=\"rating.php?rating=5&type="
    .$type."&id=".$id."\" title='5 stars' class='five-stars'>5</a></li>
      </ul>"
    .$row[votes]." votes";
     }

    } else {

     
    //get cookie
     
    if (isset($_COOKIE["cc-rating"])) {

      
    $pos strpos($_COOKIE["cc-rating"],$type.$id);
      if(
    $pos === false) {

       
    //add new id to cookie
       
    $oldcookiestring $_COOKIE["cc-rating"];

       
    //set updated cookie
       
    $expire time()+60*60*24*60;
       
    $cookiestring $oldcookiestring.$type.$id.",";
       
    setcookie("cc-rating"$cookiestringtime()+60*60*24*60"/"".mydomain.com"00);

       
    //echo cookie content for testing
       
    echo "previous cookie present, <br> old cookie string: ".$oldcookiestring."<br>new cookie string: ".$cookiestring;

      }

      } else {


       
    //create a new cookie, with id (60 days)
       
    $expire time()+60*60*24*60;
       
    $cookiestring $type.$id.",";
       
    setcookie("cc-rating"$cookiestringtime()+60*60*24*60"/"".mydomain.com"00);

       
    //echo cookie content for testing
       
    echo "no cookie present, new cookie set with string: ".$cookiestring;

      }


    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
     <title></title>
     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
     <link rel="stylesheet" href="rating.css" type="text/css">
     <script type="text/javascript" src="script.js"></script>
    </head>
    <body style="background-color:#202020;">

    <?php

     
    //connect to db
     
    mysql_connect("[...]") or die(mysql_error());
     
    mysql_select_db("[...]") or die();

     
    $rating strip_tags($_GET[rating]) * 2;

     if(
    is_numeric($id)) {

     
    //update row with new vote
     
    mysql_query("[...]") or die(mysql_error());

     
    //set cookie

     //get row
     
    $result mysql_query("[...]") or die(mysql_error());
     
    $row mysql_fetch_array($result);

     
    //calculate rating
     
    $rating round_to_half(substr(($row[total] / $row[votes]) / 2,0,4));

     
    $width $rating 20;

     echo 
    "
      <ul class='star-rating'>
      <li class='current-rating' style='width:"
    .$width."px;'></li>
       <li><a href=\"#\" title='Already voted' class='one-star'>1</a></li>
       <li><a href=\"#\" title='Already voted' class='two-stars'>2</a></li>
       <li><a href=\"#\" title='Already voted' class='three-stars'>3</a></li>
       <li><a href=\"#\" title='Already voted' class='four-stars'>4</a></li>
       <li><a href=\"#\" title='Already voted' class='five-stars'>5</a></li>
      </ul> Thanks!"
    ;

     }
     else
     {
     echo 
    "No.";
     }


    }

    } else {

    echo 
    "No.";

    }

    ?>
    </body>
    </html>

  10. #10
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is messing you up.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title></title><meta http-equiv="content-type" content="text/html; charset=UTF-8"><link rel="stylesheet" href="rating.css" type="text/css"><script type="text/javascript" src="script.js"></script></head><body style="background-color:#202020;">
    You cannot output anything to the browser before setting the cookie. This includes even a space or linefeed.

    Try the code without "output" and do a print_r($_COOKIE) after the cookie is and is not set section of your code. You should see it working.
    What I lack in acuracy I make up for in misteaks

  11. #11
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The doctype was put in a section of the code that would only be executed if no cookie was being set, or after it was.

    Regardless, I made a php file that had only the following contents in it:

    PHP Code:
    <?php
       $expire 
    time()+60*60*24*60;
       
    $cookiestring "my cookie value";
       
    setcookie("cc-rating"$cookiestring$expire"/"".mydomain.com"00);  //obviously i put my actual domain

       
    print_r($_COOKIE["cc-rating"]);
    ?>
    The cookie was set fine, and upon a reload, the cookie contents showed up as I had put them. But still, the expire date, when I checked it in Firefox, was set to the end of the session.

    Tried it IE and Chrome too. Neither had the cookie set after restarting them.

    I'm seriously lost here.

  12. #12
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I've got a more perplexing problem now.

    I went and tried the above script on a different computer... and the expiration date was set correctly. I had not previously tried to go on another computer because I assumed three different browsers on my own computer would be enough to test it.

    I have had problems with Firefox not keeping me logged in to many sites (like Facebook or Google), but I assumed it was just a bug in my installation of FF--which I had gotten used to. But apparently it's a computer-wide thing.

    Does anyone know what could affect the computer's ability among three totally different browsers to not accept cookies correctly?

  13. #13
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    firewall
    caching proxy

  14. #14
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    At least the code's working correctly!

  15. #15
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, it's the firewall damn it. I would expect a firewall to block cookies entirely, not just their expiration date.

    Sorry for wasting everyone's time, and thanks for all your help!


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
  •