Welcome Guest, Not a member yet? Register   Sign In
Codeigniter user_agent field in sessions table showing all browsers instead of one
#1

[eluser]sanjitscorps[/eluser]
I'm using codeigniter for storing session data in the ci_sessions table. upon loading the webpage for the first time I get one cookie (which is okay) but at the same time I get two exactly same records (except for the session id) in my ci_session table. This is ridiculous as there should be only one for one user agent and IP combination.

Also when i tried another browser it started showing all of my installed browsers in the user_agent field for 1 record. this is driving me nuts. below is what i see in the user agent after using another browser to access my website. I'm going live very soon, please help!

user_agent

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36


My session table is as follows

# Name Type
1 session_id varchar(40)
2 ip_address varchar(45)
3 user_agent varchar(255)
4 last_activity int(10)
5 user_data text
6 login_time timestamp

And my session configuration is as follows

$config['sess_cookie_name'] = 'cisession';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
#2

[eluser]GIN[/eluser]
Hi,

You can set $config[‘sess_match_useragent’] = FALSE;
Rows will not be duplicated

UPD. also you can change lenght for user_agent field in ci_sessions table - that solves this problem and you can use $config[‘sess_match_useragent’] = TRUE; as well as $config[‘sess_match_ip’] = TRUE;
#3

[eluser]sanjitscorps[/eluser]
I've tried both of them but it is still not working. As you can see the user_agent field is 255 characters long and hence it should not be a problem in my case.

Also when I set the $config[‘sess_match_useragent’] = FALSE

it still duplicates. Sad

#4

[eluser]sanjitscorps[/eluser]
I’ve tried both of them but it is still not working. As you can see the user_agent field is 255 characters long and hence it should not be a problem in my case.

Also when I set the $config[‘sess_match_useragent’] = FALSE

it still duplicates. Sad


[quote author="GIN" date="1379320303"]Hi,

You can set $config[‘sess_match_useragent’] = FALSE;
Rows will not be duplicated

UPD. also you can change lenght for user_agent field in ci_sessions table - that solves this problem and you can use $config[‘sess_match_useragent’] = TRUE; as well as $config[‘sess_match_ip’] = TRUE;[/quote]
#5

[eluser]CroNiX[/eluser]
It doesn't matter if you set the user_agent field to 255 in the DB, internally it's not using it all. Look at the session class code.
#6

[eluser]sanjitscorps[/eluser]
Hi Cronix,

I did not get you, what do you mean by it is not using it internally. Which part of the session code are you referring to?

[quote author="CroNiX" date="1379341596"]It doesn't matter if you set the user_agent field to 255 in the DB, internally it's not using it all. Look at the session class code.[/quote]
#7

[eluser]CroNiX[/eluser]
It's only using the first 120 characters of the UA string.

Code:
if ($this->sess_match_useragent === TRUE &&
   trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))

Here, it's only storing the first 120 chars of the UA string...
Code:
$this->userdata = array(
   'session_id' => $this->_make_sess_id(),
   'ip_address' => $this->CI->input->ip_address(),
   'user_agent' => trim(substr($this->CI->input->user_agent(), 0, 120)),
   'last_activity' => $this->now,
  );

So setting the db field above 120 chars does nothing because CI isn't using it. There are other places in the Session class where it uses the 120 char hard-coded value.
#8

[eluser]sanjitscorps[/eluser]
Ya, I know that part of trimming the user agent to 120 chars

The problem then is why am I getting duplicate records on page refresh and why does the user agent field has all the browser names in one record. The original question still stands as 255 chars or 120 chars doesn't change anything.

[quote author="CroNiX" date="1379347899"]It's only using the first 120 characters of the UA string.

Code:
if ($this->sess_match_useragent === TRUE &&
   trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))

Here, it's only storing the first 120 chars of the UA string...
Code:
$this->userdata = array(
   'session_id' => $this->_make_sess_id(),
   'ip_address' => $this->CI->input->ip_address(),
   'user_agent' => trim(substr($this->CI->input->user_agent(), 0, 120)),
   'last_activity' => $this->now,
  );

So setting the db field above 120 chars does nothing because CI isn't using it. There are other places in the Session class where it uses the 120 char hard-coded value.[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB