Welcome Guest, Not a member yet? Register   Sign In
SOLVED: Cookie not being created on remote server, but does get created locally (dx_auth)
#1

[eluser]dallen33[/eluser]
I'm building a website using dx_auth. In MAMP (Mac), it works flawlessly. dx_auth creates an autologin cookie when the user clicks "remember me". Everything just works.

But when I put it on my remote server (IIS, not Apache), the autologin cookie doesn't get created.

Now I've spent some time going through the dx_auth files to see what the problem could be, but I can't figure it out. Here's what I have:

Step 1
User logs in. If Remember is selected, _create_autologin is called.
Quote:function login($login, $password, $remember = TRUE)
{
if ($remember)
{
// Create auto login if user want to be remembered
$this->_create_autologin($row->id);
}
}
I know 'remember' is being sent by the browser by using LiveHTTP in FireFox:
Quote:username=dallen&password=somepass&remember=1&login=Login

Step 2
Here is the _create_autologin function. Looks okay to me.
Quote:function _create_autologin($user_id)
{
$result = FALSE;

// User wants to be remembered
$user = array(
'key_id' => substr(md5(uniqid(rand().$this->ci->input->cookie($this->ci->config->item('sess_cookie_name')))), 0, 16),
'user_id' => $user_id
);

// Load Models
$this->ci->load->model('dx_auth/user_autologin', 'user_autologin');

// Prune keys
$this->ci->user_autologin->prune_keys($user['user_id']);

if ($this->ci->user_autologin->store_key($user['key_id'], $user['user_id']))
{
// Set Users AutoLogin cookie
$this->_auto_cookie($user);

$result = TRUE;
}

return $result;
}

Step 3
And this step creates the cookie.
Quote: function _auto_cookie($data)
{
// Load Cookie Helper
$this->ci->load->helper('cookie');

$cookie = array(
'name' => $this->ci->config->item('DX_autologin_cookie_name'),
'value' => serialize($data),
'expire' => $this->ci->config->item('DX_autologin_cookie_life')
);

set_cookie($cookie);
}

But the problem is that the cookie doesn't get created. What could be the problem? I really can't figure it out.
#2

[eluser]dallen33[/eluser]
Found the problem:
Quote: if ($val->run() AND $this->dx_auth->login($val->set_value('username'), $val->set_value('password'), $val->set_value('remember')))
{
// Redirect to homepage
redirect('', 'location');
}

This is in the login function. If I change 'location' to 'refresh', the cookie gets made. HOWEVER, the resulting page doesn't show. I have to hit refresh in order to view it. Any way around this?
#3

[eluser]dallen33[/eluser]
Figured this out as well. So, for anyone with IIS and using dx_auth, this may come in handy.

In auth.php function login()
Quote:if ($val->run() AND $this->dx_auth->login($val->set_value('username'), $val->set_value('password'), $val->set_value('remember')))
{
// Redirect to homepage
redirect('', 'location');
}

I am now doing this:
Quote:if ($this->dx_auth->is_role('admin'))
{
redirect('/backend', 'refresh');
}
elseif ($this->dx_auth->is_role('user'))
{
redirect('/app/user', 'refresh');
}

Another thing, I had to remove my cookie_prefix. If I had a prefix, autologin was created, but when I came back to the page, it's like the script couldn't find it. I'm thinking it's because "autologin" was given a prefix "test_autologin" but dx_auth only looks for the name without prefix.

There you go! Hope it helps someone.




Theme © iAndrew 2016 - Forum software by © MyBB