0

Hi I'm setting cookie in codeigniter and my code is

if(isset($inputData["rememberMe"])) {
     $cookieData = array(
          "name"   => "rememberMe",
          "value"  => 1,
          "expire" => 25000,
          "domain" => SITE_URL,
          "path"   => "/",
          "secure" => TRUE,
          "prefix" => "asi_"
     );
     $this->input->set_cookie($cookieData);
     echo '<pre>';print_r(get_cookie("asi_rememberMe"));echo '</pre>';
     echo '<pre>';print_r($this->input->cookie("asi_rememberMe"));echo '</pre>';
     die('Call');
}

and cookie helper is initialised in autoload.php file like this $autoload['helper'] = array("url", "form", "cookie"); so no need to call helper in my controller and I've tried both ways to get cookie first is get_cookie("asi_rememberMe") and second is $this->input->cookie("asi_rememberMe") but both are not working and my codeigniter version is 3.1.5 don't know what is wrong with my code and I've also search different forums but didn't work for me.

  • 3
    You can't get info out of cookies until the next request when they're sent. Unsure if Codeigniter has stuff to account for this. – Jonnix Aug 09 '17 at 10:30
  • but the cookie is set now so it should retrieve the cookie? – User 8405526 Aug 09 '17 at 10:32
  • No it's not. The cookie is on the client machine and they don't get the data until you've sent the current request's response. Then on the next request, the updated cookie data is sent. – Jonnix Aug 09 '17 at 10:33
  • The cookie can only be read after the next request. If you need the data before that, save it in some other way. – M. Eriksson Aug 09 '17 at 10:33
  • setcookie sends a cookie to the browser. You cannot access that cookie again until the browser sends it back to the server as part of the next page submission – RiggsFolly Aug 09 '17 at 10:33
  • @RiggsFolly can you show me the example code? – User 8405526 Aug 09 '17 at 10:35
  • First off, you need to let us know who you're asking (write a @ directly followed by the users name to tag them in the comment). You also need to let us know what you want an example code of? – M. Eriksson Aug 09 '17 at 10:36
  • @RiggsFolly It's more a duplicate of https://stackoverflow.com/questions/8201570/codeigniter-cookie-wont-set-but-session-is-working?rq=1 rather than the one linked. –  Aug 09 '17 at 10:43
  • @63d26a1c Thanks, Added that to the list of answers – RiggsFolly Aug 09 '17 at 10:45
  • @RiggsFolly It means cookie is set now I can't get it until a new request have been made??? – User 8405526 Aug 09 '17 at 10:53
  • That is correct, when you submit the page it will be available in the script – RiggsFolly Aug 09 '17 at 10:54
  • @RiggsFolly Thank you for reply... – User 8405526 Aug 09 '17 at 11:01
  • @User8405526: This is the way HTTP works. *Setting a cookie* is just adding a header (`Set-Cookie`) in the response sent to the client. Then, the client will transmit it (and the other cookies, if applicable) in the header `Cookie` of any request matching the same domain (and path). The variable `$_COOKIES` reflects the cookies sent by the client in the request you are currently processing, that's why you need to wait for the next one to see the cookie you set. –  Aug 09 '17 at 11:16

0 Answers0