Hello,
I use user_agent to show a specific style for mobile device, also I don't want showing content for search engines, I don't want loading the user_agent every time the page load to reduce memory and CPU usage; So I wrote this library:
Common.php
Code:
<?php
class Common {
private $CI;
function mobile() {
$CI = & get_instance();
if ($CI->session->userdata('mobile')) {
$mobile = $CI->session->userdata('mobile');
//echo 'mobile check loaded from cookie';
}
else {
$CI->load->library('user_agent');
if ($CI->agent->is_mobile()) {
$mobile = 'y';
}
else {
$mobile = 'n';
}
$CI->session->set_userdata('mobile', $mobile);
//echo 'mobile check loaded from user_agent';
}
return $mobile;
}
function bot() {
$CI = & get_instance();
if ($CI->session->userdata('bot')) {
$bot = $CI->session->userdata('bot');
}
else {
$CI->load->library('user_agent');
if ($CI->agent->is_robot()) {
$bot = 'y';
}
else {
$bot = 'n';
}
$CI->session->set_userdata('bot', $bot);
}
return $bot;
}
}
In the controller I have for testing:
Code:
<?php
if (!defined('BASEPATH'))
exit ('No direct script access allowed');
class Test extends CI_Controller {
function __construct() {
parent :: __construct();
}
public function index() {
$this->load->library('common');
echo '<br><br> Mobile: ' . $this->common->mobile();
echo '<br> Bot: ' . $this->common->bot();
}
}
It is working on mobile browser, but any body know if it will work with search engine bots !!!!
Regards,