Jump to content

Strange cookie problem


chibineku
 Share

Recommended Posts

I have a map which when you click on it sets a cookie called region. I can verify in FF that the cookie is set and has the appropriate value. But when I try to read the cookie, I get nothing - no recognition that the cookie is set. I tried

 foreach($_COOKIE as $k => $v) {  echo $k." ".$v."<br />"; }

And it simply didn't show up, though my session cookies did. I'm at a loss. If you need more info I will provide, obviously, but I don't know where to start. Anyone had the same experience?

Link to comment
Share on other sites

Same thing, Tino03: it only shows the other 4 cookies.

Link to comment
Share on other sites

Nope, on the next page. I set the cookie in an intermediate script that redirects to the referring page (before the user visits the map to select a region).Here is the most pertinent code:

if(isset($_COOKIE["userid"]) || isset($_SESSION["userid"])) { $get_region_sql = "SELECT region FROM aromaAddress WHERE userid = '$ident'"; $get_region_res = mysqli_query($mysqli, $get_region_sql) or error_log(mysqli_error($mysqli)."\r\n"); $get_region_info = mysqli_fetch_array($get_region_res); $region = $get_region_info["region"];} else if(!isset($_COOKIE["userid"]) && !isset($_SESSION["userid"])) { if(isset($_COOKIE["region"]) || isset($_SESSION["region"])) {  $region = isset($_COOKIE["region"])?$_COOKIE["region"]:$_SESSION["region"]; } else if(!isset($_COOKIE["region"]) && !isset($_SESSION["region"])) {   $region = "uk";}}

Edited by chibineku
Link to comment
Share on other sites

It's all on the same domain, no subdomains. The cookie setting code:

$expire=time()+60*60*24*7;	  if(isset($_COOKIE["region"])) { setcookie("region", "", time()-3600); }	  setcookie("region", $_GET["region"], $expire);	     header("Location: $ref2/$orderid2$cat2$id2$search2");   exit();

Link to comment
Share on other sites

Since I'd never done quite this thing before, I wondered if sending a location header would different browser behavior than a normal page request by the browser. Nope. I stripped down your code to the least possible, and it works the way you want:cookietest.php

<?php   $expire=time()+60*60*24*7;   setcookie("Chibineku", "HELLO", $expire);   header("Location: location2.php");   exit();?>

location2.php

<?php   var_dump($_COOKIE);?>

This produced the expected:array(1) { ["Chibineku"]=> string(5) "HELLO" }So I figure something else is going on?

Edited by Deirdre's Dad
Link to comment
Share on other sites

The problem lies somewhere in the piece of code that tries to assign a value to the variable $region. When I add some echos, like so:

if(isset($_COOKIE["userid"]) || isset($_SESSION["userid"])) { $get_region_sql = "SELECT region FROM aromaAddress WHERE userid = '$ident'"; $get_region_res = mysqli_query($mysqli, $get_region_sql) or error_log(mysqli_error($mysqli)."\r\n"); $get_region_info = mysqli_fetch_array($get_region_res); $region = $get_region_info["region"];} else  if(isset($_COOKIE["region"]) || isset($_SESSION["region"])) {  echo "1";  $region = isset($_COOKIE["region"])?$_COOKIE["region"]:$_SESSION["region"]; } else if(!isset($_COOKIE["region"]) && !isset($_SESSION["region"])) {   echo "2";   $region = "uk";}

It always echos '2'. Je ne comprend pas.

Link to comment
Share on other sites

I said the above code was the problem, and it's true that the logic seems to fail, but I preceded that code with:

foreach($_COOKIE as $k => $v) { echo $k.": ".$v."<br />";}

And it STILL didn't find the cookie I was looking for. It's most unusual.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...