Welcome Guest, Not a member yet? Register   Sign In
aren't cookies global?
#1
Shocked 

I am having an issue with my app. I am using two different Codeigniter addons, codexworld's "user registration and login" and Grocery Crud. My app starts out allowing a user to login with "user reg and login". This happens in controller A. Once they login I store their userid in a cookie using the CI cookie helper. Then I redirect to controller B which contains the Grocery Crud. I try to read the userid cookie that I set with controller A, in a function in controller B,  but it is blank. 

I am loading the cookie helper in each controller with 

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

Does anyone know how I can get the userid from controller A to be used by functions in controller B? I am out of ideas of how to save the USERID in a global session variable for this user. 
proof that an old dog can learn new tricks
Reply
#2

(11-09-2017, 02:24 PM)as richb201 Wrote: I am having an issue with my app. I am using two different Codeigniter addons, codexworld's "user registration and login" and Grocery Crud. My app starts out allowing a user to login with "user reg and login". This happens in controller A. Once they login I store their userid in a cookie using the CI cookie helper. Then I redirect to controller B which contains the Grocery Crud. I try to read the userid cookie that I set with controller A, in a function in controller B,  but it is blank. 

I am loading the cookie helper in each controller with 

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

Does anyone know how I can get the userid from controller A to be used by functions in controller B? I am out of ideas of how to save the USERID in a global session variable for this user. 

I guess you mean this https://www.codexworld.com/codeigniter-u...in-system/ article. In this article author used session to keep userid. Nevertheless if you want to use cookies I suggest you to read this https://stackoverflow.com/questions/6253...s-sessions article.
Reply
#3

Thx for the response, but I think you are misunderstanding the real question here. I have tried both sessions and cookies but they both seems to fail the same way. I'll give an example with $_SESSION.

users.php

session_start();
$_SESSION['user']="me";
redirect('configure');

This works fine and I can see "me" in the $_SESSION array before I run the redirect line. Now I continue excution. Now I run another function which is in a different controller module (configure.php). It seems that the redirect blanked out the session variable.  _ci_last_regenerate= xxxxx123


configure.php

echo $_SESSION['user']

It seems that in the second module the $_SESSION array is blank except for the _ci_last_regenerate=xxxxx129

So the real problem is that I can get the userid from code in user.php, but then I can't communicate that userid to my functions in configure.php.  Bottom line: I need to share that userid with both modules.
proof that an old dog can learn new tricks
Reply
#4

Cookies do not update until a web page refresh!

Also make sure that your cookie path is set to / in the ./application/config/config.php file
What did you Try? What did you Get? What did you Expect?

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

Well, I managed to get the userid into a session in the other controller. And when I look at the $_SESSION variable (I am stopped in the 2nd controller) in phpStorm, my $_SESSION['userid'] appears to be there. So here is my callback before insert

$crud->callback_before_insert(array($this,'add_email'));

and the call back

  function add_email($post_array){
       $post_array['email']=$_SESSION['userid]'];
       $post_array['email'].=$_SESSION['userid]'];
       return $post_array;
   }

But I can't seem to get the $_SESSION into my post array. When I breakpoint at the return statement, $_SESSION['userid'] is null. Anyone know why?
proof that an old dog can learn new tricks
Reply
#6

It might be a copy error but you have

PHP Code:
$post_array['email']=$_SESSION['userid]']; 

Do you actually have something like (guessed your variable names):
PHP Code:
$post_array['userid']=$_SESSION['userid'];
$post_array['email']=$_SESSION['useremail']; 
Reply
#7

Hey Paul. Yeah the naming is a little freaky but I call it userid in the session, but it is called email in the database. Strange thing is that I just ran it again under the debugger and now $_SESSION only has one element in it (when I stop it at a breakpoint in the 2nd controller). This is crazy! I have two controllers. I thought that the $_SESSION['userid'] would remain the same (and in both). Apparently not.
proof that an old dog can learn new tricks
Reply
#8

I was actually referring to the code you wrote has apostrophes out of place and additional brackets.

But something is up because yes, your session values will be available in all your models, libraries and controllers, and a single session has only one session array.

When you say you store the userid in the cookie using the cookie helper, that cookie will not update until the page is loaded at the browser end. So you cannot then read it again in the same server session. What you would have to do is have an intermediate thank you page of some sort with a click here to continue button or similar. Then your new value of the cookie will be available to be read. Loading the cookie library twice is not important here as I believe CI will know it has already been loaded and ignore that.

If you load a value into the session
PHP Code:
$_SESSION['user_id'] = $userid
Then that session value will be available after a redirect.

You can check the profiler to see what is in the session variable.
PHP Code:
$this->output->enable_profiler(TRUE); 

Hope that helps, you seem to have a complicated issue here that is hard for us to replicate easily.

Paul.
Reply
#9

(11-10-2017, 01:51 AM)richb201 Wrote: Thx for the response, but I think you are misunderstanding the real question here. I have tried both sessions and cookies but they both seems to fail the same way. I'll give an example with $_SESSION.

users.php

session_start();
$_SESSION['user']="me";
redirect('configure');

This works fine and I can see "me" in the $_SESSION array before I run the redirect line. Now I continue excution. Now I run another function which is in a different controller module (configure.php). It seems that the redirect blanked out the session variable.  _ci_last_regenerate= xxxxx123


configure.php

echo $_SESSION['user']

It seems that in the second module the $_SESSION array is blank except for the _ci_last_regenerate=xxxxx129

So the real problem is that I can get the userid from code in user.php, but then I can't communicate that userid to my functions in configure.php.  Bottom line: I need to share that userid with both modules.

In the code above you are calling session_start() which, if you are using the CI session library, you should not do. Loading the session library makes the call to session_start() for you.

Otherwise, your problem has the feel of incorrect session and/or cookie $config settings.
Reply
#10

(This post was last modified: 11-13-2017, 01:30 PM by richb201.)

Well, I have traced through to see where the redirect is "breaking the session vars". I have narrowed it down to the bottom of redirect. The following CI code appears in url_helper.php

switch ($method)
{
case 'refresh':
header('Refresh:0;url='.$uri);
break;
default:
header('Location: '.$uri, TRUE, $code);
break;
}
exit;
In my case the $method is 'auto'. So the execution pointer skips the 'refresh' case, sets the header, and falls out to 'exit;'. This causes the session data to be blanked. Either way (refresh or default) the code will reach exit;. Is it possible that my url_helper.php got corrupted? What is the affect of calling exit in php? Shouldn't it be exit() anyway?

This is the code that is calling the redirect.
$this->session->set_userdata('isUserLoggedIn',TRUE);
$this->session->set_userdata('userId',$checkLogin['id']);
//rnb added
//added rnb
$this->session->set_userdata('userid','[email protected]');
redirect('configure/index'); //success

This works fine and starts up the function index() in configure.php. The only issue is that the exit() up above reinitializes the session data.
proof that an old dog can learn new tricks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB