1

In my MY_Controller.php, I want to detect user device and the requested domain name. Domain names I am using on same app are: www.seeme.tld and m.seeme.tld, also I am using $this->detect(). So this is what I did:

<?php

if($this->detect->isMobile() || $_SERVER['HTTP_HOST'] === MOBILE_URL){
$this->config->set_item('base_url', MOBILE_URL);
}elseif(!$this->detect->isMobile() || $_SERVER['HTTP_HOST'] != MOBILE_URL){
$this->config->set_item('base_url', WEBSITE_URL);
}

?>

I have 2 folders in application/views folder : PC(for pc users) and Mobile(for mobile users) In order to load views, I used this code in my fetch() function:

public function fetch($view, $data = array, $other_vars = false)
{

if(base_url() === MOBILE_URL || $this->_ci->detect->isMobile()){
$f = 'Mobile/';
}elseif(!$this->_ci->detect->isMobile() || base_url() != MOBILE_URL){
$f = 'PC/';
}
return $this->_ci->load->view($f.'contents/'.$view, $data, true);
}

When I use a mobile device or the visit m.seeme.tld with a mobile device, I get mobile contents. But when I visit visit m.seeme.tld with a PC instead of getting mobile contents, I rather get PC contents. Please help me solve this issue!

Marko Popovic
  • 3,428
  • 2
  • 17
  • 34
Stephane
  • 31
  • 7

2 Answers2

1

Changing config array could be problematic sometimes: How to override config's array within a controller in CodeIgniter?. Also you are doing double check (in controller and in the function).

I archieve similar behaviour with doing this way:

YOURCONTROLLER.PHP __construct():

if ($this->detect->isMobile() || $_SERVER['HTTP_HOST'] === MOBILE_URL){
   define('IS_MOBILE', TRUE);
}else{
   define('IS_MOBILE', FALSE);
}

then you could use to load the view:

if (IS_MOBILE) {
   $view_folder = 'Mobile/';
}else{
   $view_folder = 'PC/';
}
$this->load->view($view_folder.$view, $data, TRUE); 

Also you could add a single checkpoint to view if the if statment is working fine:

if ($this->detect->isMobile() || $_SERVER['HTTP_HOST'] === MOBILE_URL){
   define('IS_MOBILE', TRUE);
   log_message('debug', 'Im mobile browser: '.$this->detect->isMobile().' or the url is mobile:'.$_SERVER["HTTP_HOST"]);
}else{
   define('IS_MOBILE', FALSE);
   log_message('debug', 'Im pc');
}

Hope it helps to you. // This works only replace the '===' with '='.

Community
  • 1
  • 1
JP. Aulet
  • 4,117
  • 3
  • 21
  • 34
  • thanks for the help but still your code doesn't works as it prints 'Im pc' when i visit the mobile url with a pc. – Stephane Mar 26 '16 at 18:23
  • Ok, now you could know what of the 2 conditions are not working with: log_message('debug', 'Im pc - Ismobile should be false: '.$this->detect->isMobile().' and the url mobile true:'.($_SERVER['HTTP_HOST'] === MOBILE_URL); And check which is not correct. How do you set MOBILE_URL? Where is defined? You have .htaccess that affects the URL? – JP. Aulet Mar 26 '16 at 18:27
  • If you print the result of $_SERVER['HTTP_HOST'] its correct? – JP. Aulet Mar 26 '16 at 18:33
  • MOBILE_URL is defined in /application/config/constants.php – Stephane Mar 26 '16 at 18:45
  • The only thing it could be is that the comparision of SERVER === MOBILE_URL is false when it should be true (with PC and URL to m.seeme.tld). Append the code of your constants.php . How you redirect the 2 different URL to the same CI application? Post your .htaccess too (ou routes). – JP. Aulet Mar 26 '16 at 19:24
  • Ok. Well i use no code to redirect the 2 different domain names in a single CI APP. As i use cPanel i just created a subdomain to public_html directory. Here is my constants.php : define('MOBILE_URL', 'http://m.seeme.tld'); define('WEBSITE_URL', 'http://www.seeme.tld'); and my .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] – Stephane Mar 26 '16 at 20:32
  • Fine, i think you almost got it but you have to check witch of the 2 if conditions are not doing as you want. Is isMobile() or $_SERVER? Check the log and check too the value of each if is not working well ($_SERVER value or isMobile() value). – JP. Aulet Mar 26 '16 at 20:42
  • In fact what i want to achieve is: 1) show me mobile contents when i use mobile device or visit mobile url with a pc 2) show me pc contents when i visit with a pc on main domain url. – Stephane Mar 27 '16 at 03:54
  • Yes I know that, but your problem is when you access with PC in the mobile URL, something don't work and goes to 'im pc'. The isMobile() function returns false (that is correct) && the $_SERVER too (incorrect). You have to guess why. Whats the value of $_SERVER? why is NOT equal to m.seeme.tld ? etc. – JP. Aulet Mar 27 '16 at 11:21
  • The isMobile() function returns false (that is correct) && the $_SERVER is true(correct -> m.seeme.tld) – Stephane Mar 27 '16 at 15:00
  • This has no sense, if the condition is: `if(false || true){ ` the if function must go tho the first part and load the 'Mobile/' part. Make sure if the mobile view folders aren't loading but displayed as computer (i.e responsive bootstrap css). You could add a different header foreach folder to make sure this. – JP. Aulet Mar 27 '16 at 15:17
  • how to do that? i'm a noob to PHP – Stephane Mar 27 '16 at 15:28
  • Add i.e a header tag in php like: ` IM IN THE PC TEMPLATE "; ?>` and the same with IM in the MOBILE. You should see a div in top and see if the template is really in the mobile or in the pc. – JP. Aulet Mar 27 '16 at 15:31
  • i do not use responsive design nor a frontend framework – Stephane Mar 27 '16 at 16:02
  • Try updating your code in the question or try tho 'hardwrite' the folder like: `$this->load->view("mobile/".$view, $data, TRUE);` and see if it works. Without more information we are stucked. The if MUST work (true || false), therefore the` $view_folder = 'Mobile/';` must be set either. – JP. Aulet Mar 27 '16 at 16:21
0

thanks for the help but i got it fixed. all i did was to first detect mobile users in MY_Controller.php and redirect them to MOBILE_URL then in my fetch() i did:

if($_SERVER['HTTP_HOST'] = MOBILE_URL){
$view_folder = 'Mobile/; 
}else{ $view_folder = 'Frontend/; 
}

and that's it,Paam it started working.

Stephane
  • 31
  • 7