This is a discussion on Firefox cookie oddities within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Not sure what's going on here or even if it /is/ a PHP problem but... Using PHP 5.1....
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Not sure what's going on here or even if it /is/ a PHP problem but...
Using PHP 5.1.6 on a remote server, accessing the page locally and I have certain preconditions for setting cookies, not setting cookies, updating cookies, etc. When accessed from Konqueror this all behaves as expected, when accessed from Firefox however it all starts looking a bit weird. The problem is that when the browser sends a cookie value that isn't stored in the database (and, so far, when $_SESSION has /a/ cookie_id value), while the desired result of a new db row and a reset cookie and session value are achieved, the variables I'm using to trace what's happening report values as though the cookie was always there. Now, this _doesn't_ happen when I use Konqueror (I haven't yet tested anything else) and everything happens as I expect it to. As it goes, if you don't know how it's works when it's working, you won't know how to fix it when it's broke so as much as it may all come to the same conclusion I'd still like to know what's happening. Output under FireFox with a new session after wiping the db clean is: <!-- Initial cookieID = 118503664987 Initial Serv.cookieID = 118503664987 --> <!-- COOKIE['ID'] 1 118503664987 --> <!-- SESSION['cookieID'] 2 0 COOKIE['ID'] 3 0 --> <!-- SESSION['cookieID'] 4 0 --> <!-- SESSION['cookieID'] 5 118503664987 COOKIE['ID'] 6 118503664987 --> <!-- Stored Cookie is 1 --> <!-- SESSION['cookieID'] 7 0 --> <!-- cookieTime --> <!-- cookieRand --> Output under Konqueror (hoped for output) is: <!-- Initial cookieID = 118488413231 Initial Serv.cookieID = --> <!-- COOKIE['ID'] 1 118488413231 --> <!-- SESSION['cookieID'] 2 0 COOKIE['ID'] 3 0 --> <!-- SESSION['cookieID'] 4 118503675993 --> <!-- SESSION['cookieID'] 5 0 COOKIE['ID'] 6 0 --> <!-- Stored Cookie is 0 --> <!-- SESSION['cookieID'] 7 0 --> <!-- cookieTime 1185036759 --> <!-- cookieRand 93 --> As can be seen below, output 4 should occur if !$_SESSION["cookieID"] and if !$_StoredCookie, 5 and 6 should only happen if the inverse is true. CODE (not closed because this is a partial): <?php $one = 0; $two = 0; $three = 0; $four = 0; $five = 0; $six = 0; $seven = 0; session_start(); if (!$dbHandle) { $connectDB = "host=***** hostaddr=***** dbname=***** user=***** password=*****"; $dbHandle = pg_connect( $connectDB ); } $uno = $_COOKIE["ID"]; $duos = $_SESSION["cookieID"]; if (is_numeric($_COOKIE["ID"])) { $sCookie = pg_query( $dbHandle, "SELECT cookie_id FROM cookies WHERE cookie_id = ".$_COOKIE["ID"].";"); $storedCookie = pg_num_rows($sCookie); $one = $_COOKIE["ID"]; } if (!$_SESSION["cookieID"]) { if ($storedCookie) { $_SESSION["cookieID"] = $_COOKIE["ID"]; $two = $_SESSION["cookieID"]; $three = $_COOKIE["ID"]; $themeID = pg_query( $dbHandle, "SELECT theme_id FROM cookies WHERE cookie_id = ".$_COOKIE["ID"].";"); $_SESSION["themeID"] = pg_fetch_result($themeID, 0, 0); setcookie("ID", $_SESSION["cookieID"], time()+604800, '/', '.odubtaig.net'); } else { $cookieTime = time(); $cookieRand = rand(0, 100); $_SESSION["cookieID"] = $cookieTime.$cookieRand; pg_query( $dbHandle, "INSERT INTO cookies (cookie_id, theme_id) VALUES (".$_SESSION["cookieID"].", 1);"); $four = $_SESSION["cookieID"]; setcookie("ID", $_SESSION["cookieID"], time()+604800, '/', '.odubtaig.net'); $_SESSION["themeID"] = "1"; } } else { if($storedCookie) { if ($_SESSION["cookieID"] != $_COOKIE["ID"]) { $_SESSION["cookieID"] = $_COOKIE["ID"]; } $thisTheme = pg_query( $dbHandle, "SELECT theme_id FROM cookies WHERE cookie_id = ".$_COOKIE["ID"].";"); $_SESSION["themeID"] = pg_fetch_result($thisTheme, 0, 0); $five = $_SESSION["cookieID"]; $six = $_COOKIE["ID"]; } else { $cookieTime = time(); $cookieRand = rand(0, 100); $_SESSION["cookieID"] = $cookieTime.$cookieRand; pg_query( $dbHandle, "INSERT INTO cookies (cookie_id, theme_id) VALUES (".$_SESSION["cookieID"].", 1);"); $seven = $_SESSION["cookieID"]; setcookie("ID", $_SESSION["cookieID"], time()+604800, '/', '.odubtaig.net'); $_SESSION["themeID"] = "1"; } } $thisAddy = $_SERVER["HTTP_HOST"]; if ($_POST["theme"]) { $_SESSION["themeID"] = $_POST["theme"]; pg_query( $dbHandle, "UPDATE cookies SET theme_id = ".$_SESSION["themeID"]." WHERE cookie_id = ".$_SESSION["cookieID"].";"); } echo "<!-- Initial cookieID = ".$uno." Initial Serv.cookieID = ".$duos." --> "; echo "<!-- COOKIE['ID'] 1 ".$one." --> "; echo "<!-- SESSION['cookieID'] 2 ".$two." COOKIE['ID'] 3 ".$three." --> "; echo "<!-- SESSION['cookieID'] 4 ".$four." --> "; echo "<!-- SESSION['cookieID'] 5 ".$five." COOKIE['ID'] 6 ".$six." --> "; echo "<!-- Stored Cookie is ".$storedCookie." --> "; echo "<!-- SESSION['cookieID'] 7 ".$seven." --> "; echo "<!-- cookieTime ".$cookieTime." --> "; echo "<!-- cookieRand ".$cookieRand." --> "; |
|
|||
|
On 21 Jul, 17:57, Paul Duffy <odubt...@yahoo.dot.coh.dot.yoo.kay>
wrote: > Not sure what's going on here or even if it /is/ a PHP problem but... > > Using PHP 5.1.6 on a remote server, accessing the page locally and I have > certain preconditions for setting cookies, not setting cookies, updating > cookies, etc. > > When accessed from Konqueror this all behaves as expected, when accessed > from Firefox however it all starts looking a bit weird. > > The problem is that when the browser sends a cookie value that isn't stored > in the database (and, so far, when $_SESSION has /a/ cookie_id value), > while the desired result of a new db row and a reset cookie and session > value are achieved, the variables I'm using to trace what's happening > report values as though the cookie was always there. > > Now, this _doesn't_ happen when I use Konqueror (I haven't yet tested > anything else) and everything happens as I expect it to. As it goes, if > you don't know how it's works when it's working, you won't know how to fix > it when it's broke so as much as it may all come to the same conclusion > I'd still like to know what's happening. > You're more likely to get a reply if you can reduce your code down to something which demonstrates the problem *and* *nothing* *else* I would suggest you look at the paths on the cookies. C. |
|
|||
|
>
> You're more likely to get a reply if you can reduce your code down to > something which demonstrates the problem *and* *nothing* *else* > > I would suggest you look at the paths on the cookies. > > C. OK, I've tried to shorten it by stripping out the specifics of the code. This is a test case for when a browser sends a bad cookie ID. At a new session outputs should only be for $uno, $one, $four, $cookieTime and $cookieRand (no session cookieID and not stored). Whether it's a new session or not, Firefox gets $uno, $duos, $one, $five and $six (has session cookieID and is stored) but not $four, $cookieTime or $cookieRand. Also, $storedCookie comes back as 1 instead of 0 which should not happen if the psql query was done before the cookieID was stored. What's extra confusing is that, otherwise, it behaves as it should, a new value is inserted into the database, the cookie value held by FF changes to this new value and all seems well. The only thing I can think of that could be causing this would be FF calling the page again when a new cookie is set and reloading it in which case the values would be valid, but this seems an unorthodox way for a browser to behave and could make debugging an issue in future. > CODE (not closed because this is a partial): > > <?php > > if (is_numeric($_COOKIE["ID"])) > { Check cookie exists > $storedCookie = TRUE/FALSE(1/0);} > > if (!$_SESSION["cookieID"]) > { > if ($storedCookie) set session cookieID > else set cookie and session cookieID > } > else > { > if($storedCookie) > { > if ($_SESSION["cookieID"] != $_COOKIE["ID"]) > { $_SESSION["cookieID"] = $_COOKIE["ID"]; } > Set theme > } > else > { set new cookie and reset session ID > } > } > > echo "<!-- Initial cookieID = ".$uno." Initial Serv.cookieID = > ".$duos." --> > "; > echo "<!-- COOKIE['ID'] 1 ".$one." --> > "; > echo "<!-- SESSION['cookieID'] 2 ".$two." COOKIE['ID'] 3 ".$three." --> > "; > echo "<!-- SESSION['cookieID'] 4 ".$four." --> > "; > echo "<!-- SESSION['cookieID'] 5 ".$five." COOKIE['ID'] 6 ".$six." --> > "; > echo "<!-- Stored Cookie is ".$storedCookie." --> > "; > echo "<!-- SESSION['cookieID'] 7 ".$seven." --> > "; > echo "<!-- cookieTime ".$cookieTime." --> > "; > echo "<!-- cookieRand ".$cookieRand." --> > "; |