CodeIgniter : Load different view for mobile devices
Codeigniter(PHP framework) supports the detection of client devices(user agents). In this tutorial, we will see how to use Codeigniter User agent class to detect the devices and present different views.
The following code demonstrate the capability in the simplest form
function different_views () {
$this->load->library('user_agent');
if (!$this->agent->is_mobile()) {
$this->load->view('desktop_view');
}
elseif ($this->agent->is_mobile('ipad')) {
$this->load->view('ipad_view');
}
else {
$this->load->view('mobile_view');
}
}
You can view all the user_agents that CodeIgniter recognizes in the file application/config/user_agents.php
Bear in mind that it is case sensitive. The word iPad
will produce different result from the word ipad
Reference:
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.1k Golang : How to check if input from os.Args is integer?
+15.2k nginx: [emerg] unknown directive "ssl"
+5.1k Golang : Experimental Jawi programming language
+8.2k Golang : Emulate NumPy way of creating matrix example
+32.1k Golang : Convert []string to []byte examples
+6.7k Golang : Output or print out JSON stream/encoded data
+9k Golang : Get curl -I or head data from URL example
+29.3k Golang : Saving(serializing) and reading file with GOB
+11.4k Golang : Generate DSA private, public key and PEM files example
+19.1k Golang : Check whether a network interface is up on your machine
+4.7k Fix Google Analytics Redundant Hostnames problem
+50.9k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate