• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User-Agent

#1
[eluser]WebDevB[/eluser]
Hi Guys,

I've got a problem with the user-agent...

It just ignores the code and show's the first IF statement, here is the code:

Code:
$this->load->library('user_agent');

  if($xmlRefresh==1) {
   $viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
   $this->load->view('layout', $viewData);

   } elseif ($this->agent->is_mobile()) {

      $this->_showPage("newquote/policy_mobile", 'Customise', $viewData);

   } else {

      $this->_showPage("newquote/policy", 'Customise', $viewData);

   }

Cheers guys!

#2
[eluser]TheFuzzy0ne[/eluser]
[quote author="WebDevB" date="1369927430"]
It just ignores the code and show's the first IF statement, here is the code:
[/quote]

Sorry, but what does that even mean? What code does it ignore, and what do you mean when you say it "shows the first IF statement"?

#3
[eluser]WebDevB[/eluser]
Sorry, I was probably rushing...

What seems to happen is if you come from a mobile to the site it ignores the below if statement and loads the first if view...

Code:
if ($this->agent->is_mobile()) {

      $this->_showPage("newquote/policy_mobile", 'Customise', $viewData);

   } else {

      $this->_showPage("newquote/policy", 'Customise', $viewData);

   }

Is that any better?

#4
[eluser]TheFuzzy0ne[/eluser]
CodeIgniter uses the values in ./application/config/user_agents.php to figure out whether or not it's communitcating with a mobile device. If your if-statement is not executing, there's a good chance the mobile device you are using is not in there.

Something like this might help you figure out what you need to know about the device in question:
Code:
foreach (array('agent_string', 'mobile', 'browser', 'version', 'platform') as $val)
{
    echo $val . ' -> ' . $this->agent->$val() . '<br />';
}

It's untested, but should do the trick.

#5
[eluser]TheFuzzy0ne[/eluser]
Also, an observation on the code you posted. It might be wise to extend the Loader, and override the view() method. You can add a little logic in there that will automatically append '_mobile' to the end of the filename string if it's a mobile request, but only if the mobile view file exists. If not, then it can just fallback on the regular view, and if that's not available, CodeIgniter can just chuck a wobbler as normal.

I just thought it might save you from repeatedly having to type:
Code:
if ($this->agent->is_mobile()) {
    $this->load->view('some/view_mobile', $data);
} else {
    $this->load->view('some/view', $data);
}

Instead you'd just type:
Code:
$this->load->view('some/view', $data);
...and the rest is taken care of for you.

#6
[eluser]WebDevB[/eluser]
Thanks for the reply!!

Yea it must be something to do with the user-agent library as this works ok:

Code:
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod'))

I will have another look now...

#7
[eluser]CroNiX[/eluser]
I see you loading the library as "user_agent", which is the default, but then try to use $this->agent instead of $this->user_agent. It's not "ignoring" the user agent library; you aren't referencing the user_agent object correctly. Are you not getting errors on that?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.