Page 1 of 1

[SOLVED] how to tell if cookie exists

Posted: Thu Jan 27, 2011 8:23 pm
by stevestark5000
*** I have found another way to fix this problem ***

i have read that the following code will not work:

Code: Select all

setcookie("Testcookie", $value);

// CHECK cookie
if(isset($_cookie['Testcookie'])){;
// do something
}
cookie is browser side thingy, so it will not be available in the $_cookie array until sent by the browser. So in example above cookie will become available only after page is accessed second time.

is the above paragraph true. if so, can i have a working code?

Re: how to tell if cookie exists

Posted: Thu Jan 27, 2011 8:38 pm
by fishown
stevestark5000 wrote:i have read that the following code will not work:

Code: Select all

setcookie("Testcookie", $value);

// CHECK cookie
if(isset([color=#FF0000]$_cookie[/color]['Testcookie'])){;  [color=#FF0000]<- what is this? ;?// you sould type $_COOKIE instad of  $_cookie (yes it matters! wont work either way)[/color]
// do something
}
cookie is browser side thingy, so it will not be available in the $_cookie array until sent by the browser. So in example above cookie will become available only after page is accessed second time.

is the above paragraph true. if so, can i have a working code?
Hello,
First of all you have few mistakes in your code.(note the marking above).
And if im understand right the function i wrote below sould work perfect.
If not, it would be helpful to explain what are you tring to achive.

Code: Select all

if (isset($_COOKIE["test"])){                 //checks if the cookie "test" exists, If so execute you code

// your code
    }else{
            if (isset($_REQUEST["testing"]))  //else > check if ther "testing" exist by "GET"
	     {
                     //msg if cookies disabled    //if so , say that you cookies option on your browser is deisabled
	      } 
	     else 
	     {
	                       setcookie("test", "1", 0, "/");     //else set the cookie
	                 header("Location: $_SERVER[PHP_SELF]?testing=1");  //and reload the page with the "testing" var    
	     }
}

Re: how to tell if cookie exists

Posted: Thu Jan 27, 2011 9:12 pm
by stevestark5000
when a user logs in and back out, the cookie is destroyed. when a user logs in and then goes to root site, the user is logged out but the cookie is not destroyed. i am not able to find the code when the user is not logged out when going to root site so i am writing code to determine is cookie exists but it is not working. below is my code.

Code: Select all

if ($_SERVER['REQUEST_URI'] == "/" && (isset($_COOKIE[$myCookie])))  {
echo $_SERVER['REQUEST_URI'];
}

//   $GLOBALS['core']->killAllSessions();     
if (!$_SESSION['OBJ_user']->isUser()) {
  $settings = $_SESSION["OBJ_user"]->getSettings();
  
  if($settings["show_remember_me"])
    $myCookie = PHPWS_User_Cookie::cookie_read("mod_users", "rememberme");

  if(isset($myCookie)) {
    $myCookie = preg_replace('/\W/', '', $myCookie);
    $id = $GLOBALS["core"]->getOne("select user_id from mod_users where cookie='$myCookie'", TRUE);
    if($id)
      $_SESSION['OBJ_user']->loadUser($id, 1);
    else
      PHPWS_User::login_box();
  } else {
    PHPWS_User::login_box();
  }
}
this code at the beginning should echo "/" when cookie exists at hard drive and user is at root site but for some reason this code is not working. when user logs in and then goes to root, the "/" is not displayed. when user logs back in the second time, user goes to a different page then what user should go to. this bug is because another cookie was created over the other one.

I found the problem. SOLVED