How to used User agent in Codeigniter3.0.6? - php

I am testing with Codeigniter3.0.6 on User agent as below function then I try to testing IPhone, and Chrome inspect device mode but I got only number 1.
I want to check if I view this website in Mobile phone it will echo number 2
if in PC browser echo number1 if can't detect browser show 0.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Main_Controller extends MY_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
var_dump($this->CheckDevices());
exit();
$this->data['deviceType'] = $this->CheckDevices();
if ($this->data['deviceType'] == 1) {
$this->set_navigation();
}
elseif ($this->data['deviceType'] == 2) {
return false;
}elseif($this->data['deviceType'] == 0){
return false;
}
}
private function CheckDevices()
{
$this->load->library('user_agent');
$agent = '';
if ($this->agent->is_browser()) {
$agent = 1;
} elseif ($this->agent->is_mobile()) {
$agent = 2;
} else {
$agent = 0;
}
return $agent;
}
private function set_navigation()
{
$this->load->library('session');
$this->load->library("nav_libs");
return $this->data['menus'] = $this->nav_libs->navigation();
}
}
?>

you can try this
application/core/My_Core.php
class My_Core extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function detect_dvice(){
$this->load->library('user_agent');
if( $this->agent->is_mobile()){
$_is_mobile = 1;
}
else{
$_is_mobile = 2;
}
return $_is_mobile
}
}
normal controller
application/controllers/Test_controller.php
class Test_controller extends My_Core{
public function __construct(){
parent::__construct();
}
public function index(){
echo $this->detect_dvice();
}
}

Related

Unable to update or delete rows in database using codeigniter

I am new to code-igniter, and I am facing unknown issues in updation and deletion of rows in my database. My code for Controller is :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Nhome extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->Model('N_model');
$data['r'] = $this->N_model->getdata();
$this->load->view('Homeview',$data);
}
public function edit()
{
$id = $this->input->get('id');
$this->load->Model('N_model');
$data['s'] = $this->N_model->editdata();
$this->load->view('Neditview',$data);
}
public function loadEdit()
{
$id = $this->input->get('id');
$this->load->view('Neditview');
}
public function insertdata()
{
$eID = isset($_POST['Id'])?$_POST['Id']:'';
$arr['Name'] = $_POST['Name'];
$arr['Gender'] = $_POST['Gender'];
$arr['Email'] = $_POST['Email'];
$this->load->Model('N_model');
$res = $this->N_model->updatedata($arr , $eID);
if($res){
header('location:'.base_url()."index.php/Nhome/".$this->index());
}
}
public function delete(){
$this->load->Model('N_model');
$id = $this->input->get('Id');
$this->N_model->deletedata($id);
$this->index();
}
}
and my code for model is :
<?php
class N_Model extends CI_Model{
public $Id;
public $Name;
public $Gender;
public $Email;
public function __construct()
{
parent::__construct();
}
public function getdata()
{
$va = $this->db->get('newprac');
$res = $va->result();
return $res;
}
public function editdata($id)
{
$vr = $this->db->where('Id',$id);
return $vr;
}
public function updatedata($data , $id){
$this->db->where('newprac.Id',$id);
$res = $this->db->update('newprac', $data);
return $res;
}
public function deletedata($id)
{
$this->db->where('newprac.id',$id);
$this->db->delete('newprac');
if($this->db->affected_rows()>0)
{
return true;
}
else { return false; }
}
}
Change
$this->db->where('newprac.id',$id)
to
$this->db->where('id',$id);
Simplify your code to:
$this->db->where('id',$id)->update('newprac', $data);
Replace your controller by this code. Becuase I think you passing data as a post method so you should use post when you retrieve data.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Nhome extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->Model('N_model');
$data['r'] = $this->N_model->getdata();
$this->load->view('Homeview',$data);
}
public function edit()
{
$id = $this->input->post('id');
$this->load->Model('N_model');
$data['s'] = $this->N_model->editdata();
$this->load->view('Neditview',$data);
}
public function loadEdit()
{
$id = $this->input->post('id');
$this->load->view('Neditview');
}
public function insertdata()
{
$eID = isset($_POST['Id'])?$_POST['Id']:'';
$arr['Name'] = $_POST['Name'];
$arr['Gender'] = $_POST['Gender'];
$arr['Email'] = $_POST['Email'];
$this->load->Model('N_model');
$res = $this->N_model->updatedata($arr , $eID);
if($res){
header('location:'.base_url()."index.php/Nhome/".$this->index());
}
}
public function delete(){
$this->load->Model('N_model');
$id = $this->input->post('Id');
$this->N_model->deletedata($id);
$this->index();
}
}
Did you try to debug using Chrome debugger. You might get the exact error in debugger. I suggest you to try once let me know the error name.

Error when calling global variables in codeigniter

I want to declare a global variable with a value and access it in the functions.
I'm done below code but showing Undefined variable: msg1 in the login function.
Please help me to do this.
class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
parent::__construct();
}
public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->$msg1;
$this->index($msg) ;
} else {
//something here
}
}
}
}
Try this,
class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
parent::__construct();
}
public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->msg1;
$this->index($msg) ;
} else {
//something here
}
}
}
}
Remove $ from $msg1 in login function
$msg = $this->msg1;

PHP - What is wrong? Learning MVC - Beginner

Hy,
i started learning PHP and i created a simple MVC Style Codebase.
The Script just generates a random number and displays this numer. I also write a function to display the number shown before but it does not work. The value is empty. Can you help me out, i have no clue whats wrong and there is no php error thrown.
view.php
<?php
class View
{
private $model;
private $view;
public function __construct()
{
$this->model = new Model();
}
public function output()
{
echo 'Current Entry: ';
echo $this->model->getData();
echo '<br />';
echo 'Update';
echo '<br />';
echo 'Last';
}
public function getModel()
{
return $this->model;
}
}
controller.php
<?php
class Controller
{
private $model;
private $view;
public function __construct($view)
{
$this->view = $view;
$this->model = $this->view->getModel();
}
public function get($request)
{
if (isset($request['action']))
{
if ($request['action'] === 'update')
{
for ($i = 0; $i<6; $i++)
{
$a .= mt_rand(0,9);
}
$this->model->setData($a);
}
elseif ($request['action'] === 'preview')
{
$this->model->setLast();
}
else
{
$this->model->setData('Wrong Action');
}
}
else
{
$this->model->setData('Bad Request');
}
}
}
model.php
<?php
class Model
{
private $data;
private $last;
public function __construct()
{
$this->data = 'Default';
}
public function setData($set)
{
if ( ! (($set == 'Wrong Action') && ($set == 'Bad Request')))
{
$this->last = $this->data;
}
$this->data = $set;
}
public function getData()
{
return $this->data;
}
public function setLast()
{
$this->data = $this->last;
}
public function getLast()
{
return $this->last;
}
}
index.php
<?php
require_once 'controller.php';
require_once 'view.php';
require_once 'model.php';
$view = new View();
$controller = new Controller($view);
if (isset($_GET) && !empty($_GET)) {
$controller->get($_GET);
}
$view->output();
Are there any other, bad mistakes in the Script?
Any input very welcome! :)
The problem with your code is that PHP does not preserve variable values between requests, therefore, when you set your $model->last value here:
$this->last = $this->data;
It gets reset on your next request.
You may want to store $last value in a session or a cookie instead. Something like:
$_SESSION['last'] = $this->data;
And then when you are instantiating your model you could initialize it with a value stored in a session if available:
index.php - add session_start() at the beginning
model.php:
public function __construct()
{
$this->data = isset($_SESSION['last']) ? $_SESSION['last'] : 'Default';
}
public function setData($set)
{
$this->data = $set;
if ( ! (($set == 'Wrong Action') && ($set == 'Bad Request')))
{
$_SESSION['last'] = $this->data;
}
}
controller.php
elseif ($request['action'] === 'preview')
{
//Remove this
//$this->model->setLast();
}

Auth Component's user function not working in case multiple routing

I have routed my site as admin moderator. Means When I use Auth component my sessions are created as Auth->Admin->id & Auth->Moderator->id And Auth->User->id this way.
Now I tried function $this->Auth->user its returning null why??
As I know $this->Auth->user returns session.
I know the traditional way of $this->Session->reads('Auth.Admin') and else but I want to use Auth->user function why its not working
Please use this to get user session data.
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function index() {
pr($this->Auth->User());
}
auth_helper:<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! function_exists('auth'))
{
function auth()
{
$CI =& get_instance();
$CI->load->model('Auth_model');
return $CI->Auth_model;
}
function Adminauth($admin_id)
{
if($admin_id == false)
{
redirect('login');
}
}
}
auth_model:
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Auth_model extends CI_Model {
private $user = null;
private $ci = null;
public function __construct() {
parent::__construct();
$CI =& get_instance();
$this->ci = $CI;
}
public function get($name, $default = null){
if ($this->ci->session->has_userdata('auth.Adminuser_id')) {
$user_id = $this->ci->session->userdata('auth.Adminuser_id');
$this->user = $this->db->query("SELECT * FROM members WHERE u_id=? LIMIT 1", array($user_id))->row();
}
return !empty($this->user) ? $this->user->u_name : $default;
}
public function is_logged(){
return $this->ci->session->has_userdata('auth.Adminuser_id');
}
public function login($user_id){
return $this->ci->session->set_userdata('auth.Adminuser_id', $user_id);
}
public function logout(){
return $this->ci->session->unset_userdata('auth.Adminuser_id');
}
}

codeigniter using function inside a controller

For some reason I cant run the change() function and everything after it stops. I try with $this->change() but the effect is the same. If I run the function in a php file its working and the num is changing.
class Test extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$num = 1;
change($num);
function change($num,$rand=false){
echo 'inside function'; // this is not shown
if($num < 4) {
$rand = rand(1,9);
$num = $num.$rand;
change($num,$rand);
} else {
echo $num;
}
}
echo 'done'; // this is not shown
}
}
You will probably be better off calling a private or public function to process data ( or for more complex/involved processes a library ).
ie
class Test extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function home()
{
$num = 1;
echo $this->_change($num);
echo 'done'; // this is not shown
}
// private or public is w/o the underscore
// its better to use private so that CI doesn't route to this function
private function _change($num, $rand = FALSE)
{
if($num < 4) {
$rand = rand(1,9);
$num = $num + $rand;
$this->_change($num,$rand);
} else {
return $num;
}
}
}
lol, you are trying to write function inside function.
try running your class with this code
class Test extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$num = 1;
$this->change($num);
echo 'done'; // this is not shown
}
public function change($num,$rand=false){
echo 'inside function'; // this is not shown
if($num < 4) {
$rand = rand(1,9);
$num = $num.$rand;
change($num,$rand);
} else {
echo $num;
}
}
}

Categories