Welcome Guest, Not a member yet? Register   Sign In
$_COOKIE[x] disappears
#1
Wink 
(This post was last modified: 12-24-2018, 07:15 AM by richb201.)

I am trying having some trouble with TCP so I am trying to code my way around it in the app. When my controller gets spawned, I am setting $_COOKIE[$myname]=1. I know this works because I can see it is being set with the debugger. I thought that $_COOKIE[$myname] would still be there the next time my controller gets spawned. But it appears to be gone.  Why is this? Is there some other method where I can set a variable that would remain on the server, globally? I am using Linux.
proof that an old dog can learn new tricks
Reply
#2

I think you need to call setcookie() in order for them to be persistent.
Reply
#3

(This post was last modified: 12-25-2018, 11:53 PM by richb201.)

Dave, I ran these 3 lines in my controller:

$this->load->helper('cookie');


$this->input->set_cookie("test",$value=1);
$iRc=$this->input->get_cookie("test");


I took a look at $iRc in my debugger at the next line. $iRc =null.

I noticed you typed setcookie(). Significance?  If my code checks the cookie right after setting it (on the server, ie not the browser), will it be able to see it?
proof that an old dog can learn new tricks
Reply
#4

(This post was last modified: 12-26-2018, 12:37 PM by dave friend.)

You do not need to load or use the cookie helper in order to use $this->input->set_cookie();

$_COOKIE is for reading values. If you want a cookie to remember something (be persistent) then you must use setcookie().
Reply
#5

You need to pass the set_cookie an expire time.

CodeIgniter User's Guide - set_cookie
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 12-26-2018, 11:18 AM by richb201.)

I tried this:
           if(isset($_COOKIE['TestCookie'])){
                   unset($_COOKIE['TestCookie']);
                   $res = setcookie('TestCookie', '', time() - 3600); //delete
                   $json=json_encode(html_escape("empty"));
                   header('Content-type: application/json');
                   header("Access-Control-Allow-Origin: *");
                   echo ($json);
                   exit;
               }
               else {
                   setcookie("TestCookie");
               }
Never mind the $json stuff. I am setting the cookie but if I stop at the line right after the setcookie(), it is not set. Then at the next iteration (really a completely new instance) I stop the code at the isset(), and I can see that there is no TestCookie in $_COOKIE. 

Seems like my debugger (xdebug under phpStorm) is not accurately showing the value of $_COOKIE. Is there any Linux CL utility to see what cookies are set? It seems that there is some type of delay with cookies?
proof that an old dog can learn new tricks
Reply
#7

I am starting to think that cookies is the wrong way to go with this. Has anyone used shmop? http://php.net/manual/en/book.shmop.php
proof that an old dog can learn new tricks
Reply
#8

This is how I set the cookie for logged_in

PHP Code:
setcookie("logged_in"$identifiertime() + 3600'/'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

I have this:
if(isset($_COOKIE['TestCookie'])){
setcookie('TestCookie', '1', time() - 10,'/'); //remove it
$json=json_encode(html_escape("empty"));
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
echo ($json);
exit;
}
else {

setcookie('TestCookie', '1', time() + 10,'/');
}
It works first time through, the isset doesn't seem to work. I can't really tell if it is being set (realtime) due to the debugger not updating the field right way. There seems to be some type of delay.
proof that an old dog can learn new tricks
Reply
#10

(This post was last modified: 12-27-2018, 08:01 AM by richb201.)

I manged to solved this by using this:

          if(is_null($_SESSION[$email_keye])){
               $this->session->set_tempdata($email_keye, 'value', 1);  //block for 1 sec
           }    //noyes, so write it
           else
               {
               $json=json_encode(html_escape("record written\n"));
               header('Content-type: application/json');
               header("Access-Control-Allow-Origin: *");
               echo ($json);
               exit;
               }

The idea is to block the same userid from sending in two buffers in under one second. Seems to work. Cookie seemed to have some sort of delay. My app has no user interface.
proof that an old dog can learn new tricks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB