Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] detecting the user browser
#1

[eluser]xeroblast[/eluser]
im trying to detect the current browser using this code.

Code:
if ($this->session->userdata('browser')) {
  $this->session->set_userdata('browser','< script >document . write (navigator.appName);</ script >');
}

when i print/echo the session variable, it perfectly display the variable value but when i use it in a conditional statements, it wont work like this:

Code:
if ($this->session->userdata('browser') == 'Microsoft Internet Explorer') {
  echo 'IE';
} else {
  echo 'Netscape';
}

it always display in "Netscape" even im using Internet Explorer... unless i print/echo the $this->session->userdata('browser');
#2

[eluser]xeroblast[/eluser]
i tried to var_dump the variable ($this->session->userdata('browser')) and the string ('Microsoft Internet Explorer')
and result is like this

in IE
$this->session->userdata('browser') = string(51) "Microsoft Internet Explorer"
'Microsoft Internet Explorer' = string(27) "Microsoft Internet Explorer"

in FF
$this->session->userdata('browser') = string(51) "Netscape"
'Microsoft Internet Explorer' = string(27) "Microsoft Internet Explorer"

so they are not equal...
what is the difference in strings?
#3

[eluser]xeroblast[/eluser]
i finally solved this problem. the problem was my implementation of the code.

Code:
$this->session->set_userdata('browser','< script >document . write (navigator.appName);</ script >');

is not the proper way to compare string coz the session reads the whole string as the script. that is why it has 51 characters. so i change the implementation to a different style.

Code:
function index() {
  echo '< script >window . location = "'.site_url('controller_name/browser').'/"+navigator.appName;</ script >';
}

function browser($browser) {
  if ($this->session->userdata('browser')) {
    $this->session->set_userdata('browser',$browser);
  }
  redirect('controller_name/function_name');
}




Theme © iAndrew 2016 - Forum software by © MyBB