I know this has been asked on here many, many times before, but I am trying to optimize our company gateway for mobile. This was contracted out in 2016 but the guy that started building it wasn't building it for mobile. And he didn't finish it. Now, please know I cut my teeth on ASP.Net so PHP is not my forte, but I inherited this project to finish it. It was built with CodeIgniter 2.2 and I can't upgrade it. Everything mobile (that I work on) will be built using Bootstrap (latest version), as I love Bootstrap and how easy it makes building things with "mobile-first" in mind. So I don't want to try changing all the layout and files that he built because everything is working and displaying really good in all the browsers, but not for mobile. Also know that I have looked at EVERY example that I could find on here (SO). I have tried everything. Problem is, I CAN get my "mobile" login view to display, but what I DON'T know is where to go from there. Do I create separate models and controllers just for the mobile pages that I will be building out? I will show example code of what I've tried below and what has worked. Has anyone tried anything like this and have a working example? At least have enough code that you could point me in the right direction? I would HIGHLY appreciate it. Wracking my brains here. Oh yeah, and I did update the user_agent code from the latest version of CodeIgniter (3.1.11) to catch all the latest mobile browsers because I know CI 2.2 is a little outdated. Btw, I tried to supply as much information as possible as it is working if you visit with a regular computer it shows the regular login page and it logs in just fine. If however you visit with a mobile device, it displays the mobile login page, but will not log in. Just refreshes the page. No matter if I go the user_agent route and use the same URL, or if I go the Detect_Mobile.php route and load the mobi.gatewayurl.com page. Ok, so here's the layout that's on the host. Sorry I know this is complicated, but all this layout was already done, all I added was the mobile and mobi directories.
|--public_html
|--gateway
|--gw_application
|--controllers
|--account.php
|--administrator.php
|--login.php
|--core
|--MY_Loader.php
|--helpers
|--hooks
|--models
|--account_model.php
|--administrator_model.php
|--login_model.php
|--other models as well
|--third_party
|--Mobile_Detect.php <== Was also playing around with this as well.
|--views
|--account
|--dashboard.php
|--All the other views for the regular users.
|--administrator <== Depending on what role they play. A few users will be an Administrator.
|--dashboard.php
|--All the other views for the Administrators. Which should only be a few people. Most will fall under "account".
|--mobile <== Do I put another set of models and controllers in here?
|--account <== Copied all views from the account directory for regular users in here as well.
|--templates <== The mobile login page loads by putting in a blank header.php, heading.php, and footer.php in here. Otherwise it throws errors if I don't have these in here.
|--login.php <== Or I tried even naming it mlogin.php for mobile, but it won't login. It loads, but just refreshes the page.
|--templates
|--admin
|--header.php
|--heading.php
|--footer.php
|--header.php
|--heading.php
|--footer.php
|--login.php <== The MAIN login page when visiting through a normal browser.
|--gw-system
|--core
|--blah blah
|--mobi <== Also this works using Detect_Mobile.php redirecting it to here with the URL as https://mobi.ourgatewayurl.com.
And here's my MY_Loader.php file that I got from SO article 13928677.
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Loader extends CI_Loader
{
//overides existing view function
function view($view, $vars = array(), $return = FALSE)
{
$CI =& get_instance();
$CI->load->library("user_agent");
if($CI->agent->is_mobile()){
$view = 'mobile/'.$view;// <== This does diplay my mobile login page without changing the URL.
}
return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
}
//This works good to keep the same URL, such as https://gateway.ourgatewayurl.com, but where to go from here? Otherwise using Mobile_Detect.php, I
//can redirect to https://mobi.ourgatewayurl.com. That would be no problem as well, I can do the subdomain thing, but again, where to go from here?
?>
Here's the login.php controller. Again, I didn't write this, it was already done.
<?php
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('login_model');
$this->load->model('setting_model');
$this->load->model('producer_model');
$this->load->model('company_model');
$this->load->model('report_model');
$this->load->model('administrator_model');
}
public function index()
{
$data['settings'] = $this->setting_model->get_setting();
$data['bad_login'] = 0;
if ($this->input->post('username')) {
$data['login'] = $this->login_model->get_login($this->input->post('username'),$this->input->post('password'));
if ($data['login'] == '0') {
$level = $this->session->userdata('user_level');
if ($level == 'administrator') {
$data['message_list'] = $this->administrator_model->get_message();
$this->load->view('templates/admin/heading', $data);
$this->load->view('templates/admin/header', $data);
$this->load->view('administrator/dashboard', $data);
$this->load->view('templates/admin/footer', $data);
$this->load->view('administrator/leftmenu', $data);
} else {
$data['company'] = $this->company_model->get_company($this->session->userdata('company_id'));
$data['producer'] = $this->producer_model->get_producer($this->session->userdata('producer_id'));
$data['message_list'] = $this->administrator_model->get_message();
$this->load->view('templates/admin/heading', $data);
$this->load->view('templates/admin/header', $data);
$this->load->view('account/dashboard', $data);
$this->load->view('templates/admin/footer', $data);
$this->load->view('account/leftmenu', $data);
}
} else {
$data['bad_login'] = 1;
$data['login'] = '1';
$this->load->view('templates/heading', $data);
$this->load->view('templates/header', $data);
$this->load->view('login', $data);
$this->load->view('templates/footer', $data);
}
} else {
$data['login'] = '1';
$this->load->view('templates/heading', $data);
$this->load->view('templates/header', $data);
$this->load->view('login', $data);
$this->load->view('templates/footer', $data);
}
}
}
?>
Here's part of the account.php controller. I left a lot of it out.
<?php
//Only showing for index and dashboard, but he has a public function for every page he has on the gateway.
class Account extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->library('My_PHPMailer');
$this->load->model('account_model');
$this->load->model('setting_model');
$this->load->model('user_model');
$this->load->model('producer_model');
$this->load->model('company_model');
$this->load->model('email_model');
$this->load->model('report_model');
$this->load->model('pagenate_model');
$this->load->model('staff_model');
$this->load->model('signup_model');
if (!$this->session->userdata('user_id') || ($this->session->userdata('user_level') != 'corporate' && $this->session->userdata('user_level') != 'branch' && $this->session->userdata('user_level') != 'producer')) {
header("Location: " . base_url() . 'index.php?/login/logout/');
die();
}
}
public function index()
{
$data['settings'] = $this->setting_model->get_setting();
$data['company'] = $this->company_model->get_company($this->session->userdata('company_id'));
$data['producer'] = $this->producer_model->get_producer($this->session->userdata('producer_id'));
$data['message_list'] = $this->account_model->get_message();
$this->load->view('templates/admin/heading', $data);
$this->load->view('templates/admin/header', $data);
$this->load->view('account/dashboard', $data);
$this->load->view('templates/admin/footer', $data);
$this->load->view('account/leftmenu', $data);
}
public function dashboard()
{
$data['settings'] = $this->setting_model->get_setting();
$data['company'] = $this->company_model->get_company($this->session->userdata('company_id'));
$data['producer'] = $this->producer_model->get_producer($this->session->userdata('producer_id'));
$data['message_list'] = $this->account_model->get_message();
$this->load->view('templates/admin/heading', $data);
$this->load->view('templates/admin/header', $data);
$this->load->view('account/dashboard', $data);
$this->load->view('templates/admin/footer', $data);
$this->load->view('account/leftmenu', $data);
}
}
?>
And here's the login model.
<?php
class Login_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_login($username, $password)
{
$this -> db -> select('*');
$this -> db -> from('user');
$this -> db -> where('user_email = ' . "'" . $this->db->escape_str($username) . "'");
$this -> db -> where('user_password = ' . "'" . $this->db->escape_str(do_hash($password, 'md5')) . "'");
$this -> db -> where('user_is_deleted = 0');
$this -> db -> limit(1);
$query = $this -> db -> get();
$user = $query->row_array();
if($query -> num_rows() == 1) {
$this -> db -> select('*');
$this -> db -> from('user_level');
$this -> db -> where('user_level_id = ' . $user['user_level_id']);
$this -> db -> where('user_level_is_deleted = 0');
$this -> db -> limit(1);
$query2 = $this -> db -> get();
$user_level = $query2->row_array();
$pid = '';
$coid = '';
$ctype = '';
if ($user_level['user_level_name'] != 'administrator') {
$this -> db -> select('*');
$this -> db -> from('producer');
$this -> db -> where('user_id = ' . $user['user_id']);
$this -> db -> limit(1);
$query2 = $this -> db -> get();
$producer = $query2->row_array();
$this -> db -> select('*');
$this -> db -> from('company');
$this -> db -> where('company_id = ' . $producer['company_id']);
$this -> db -> limit(1);
$query3 = $this -> db -> get();
$company = $query3->row_array();
$pid = $producer['producer_id'];
$coid = $producer['company_id'];
$ctype = $company['company_type_id'];
}
$newdata = array(
'user_id' => $user['user_id'],
'producer_id' => $pid,
'company_id' => $coid,
'company_type_id' => $ctype,
'user_level_id' => $user['user_level_id'],
'user_level' => $user_level['user_level_name'],
'user_name' => $user['user_first_name'] . ' ' . $user['user_last_name']
);
$this->session->set_userdata($newdata);
return '0';
} else {
$_SESSION['user_id'] = '';
$_SESSION['producer_id'] = '';
$_SESSION['company_id'] = '';
$_SESSION['company_type_id'] = '';
$_SESSION['user_level_id'] = '';
$_SESSION['user_level'] = '';
$_SESSION['user_name'] = '';
return '1';
}
}
}
?>
Sorry guys this is SO LONG. I just wanted everyone to see how this was laid out and how he had the code written. ANY help will be HIGHLY appreciated. Thanks so much.