Hit Counter in Codeigniter - php

I have the code below:
(Step by step)
Put counter.txt in APPPATH . 'logs/counter.txt'
Make counter_helper.php set in APPPATH . 'helpers/counter_helper.php';
Autoload newly created helper in APPPATH . 'config/autoload.php' file;
Make MY_Controller.php in APPPATH . 'core/MY_Controller.php'
Any controller should extend MY_Controller instead of CI_Controller;
Echo it on page with: <?php echo $this->count_visitor;?>
The Helper :
<?php defined('BASEPATH') OR exit('No direct script access allowed.');
if ( ! function_exists('count_visitor')) {
function count_visitor()
{
$filecounter=(APPPATH . 'logs/counter.txt');
$kunjungan=file($filecounter);
$kunjungan[0]++;
$file=fopen($filecounter, 'w');
fputs($file, $kunjungan[0]);
fclose($file);
return $kunjungan[0];
}
}
The Core :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $count_visitor;
public function __construct()
{
parent::__construct();
$this->count_visitor = count_visitor();
}
}
/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */
The Controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
public function index() {
$data=array('isi' =>'home/index_home');
$this->load->view('layout/wrapper',$data);
}
}
The View :
<?php echo $this->count_visitor;?>
The code return an error like below :

I got it to work fine when I loaded the helper $this->load->helper('counter');
application > core > MY_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $count_visitor;
public function __construct()
{
parent::__construct();
$this->load->helper('counter');
$this->count_visitor = count_visitor();
}
}

Yes, must load the helper:
$this->load->helper('counter');
or
config/autoload.php: $autoload['helper'] = array('counter');

Related

I get " {"status":false,"error":"false"} " in codignater .. why?

I have an error in my code ,, I'm writing code in php framework ` codeignater ,, i want to extend REST_Controller but i can't , my code not working clearly,, please help me ,, here is my error and my code ,
{ "status": false, "error": false }
controller / Auth.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//require('libraries\REST_Controller.php');
require APPPATH . '/libraries/REST_Controller.php';
//require('libraries\Format.php');
class Auth extends REST_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('api_model');
// $this->load->library('rest_controller_lang');
}
function data_post()
{
$data = array(
'name' => $_POST['name'],
'surename' => $_POST['surename'],
'email' => $_POST['email'],
'password' => $_POST['password'],
);
$this->api_model->insert($data);
$this->response("my first api");
var_dump($data);
}
}
?>
models/Api_model.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Api_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function insert($data){
if($this->db->insert('peoples', $data)){
return FALSE;
}
return TRUE;
}
}
?>
config/autoload.php
$autoload['libraries'] = array('database','REST_Controller');
Drag and drop the application/libraries/Format.php and application/libraries/REST_Controller.php files into your application's directories.Copy the rest.php into your ** application/config** directory.
For the code try referencing the controller directly.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require(APPPATH . 'libraries/REST_Controller.php');
class Auth extends \Restserver\Libraries\REST_Controller
{......}
I have added namespace Restserver\Libraries; in my REST_CONTROLLER
<?php
namespace Restserver\Libraries;
use Exception;
use stdClass;
defined('BASEPATH') OR exit('No direct script access allowed');
abstract class REST_Controller extends \CI_Controller
{......}
For the response try sending an HTTP Code along with it
$this->response("my first api", 200);

Integrate File manager [ Codeigniter ]

I am using code codeigniter 3x. I am trying to integrate a file manager - Responsive FileManager 9.11.3 .
I tried many time to integrate but everytime getting error.
Controller file .
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Filemanager extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->helper('path');
$this->load->library('filemanagerx');
$this->filemanagerx->fmanager();
}
}
library file .
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Filemanagerx {
public function fmanager() {
include_once 'filemanager/dialog.php';
}
}

Codeigniter error shows in my homepage call to a member function on null

I just started my website developing using the CodeIgniter framework.
My working steps are given below:
1. I created a controller named by Home.php
2. Then I go to the view folder and created a file named by index.php under sub-directory 'home'.
3. When I browse my website it shows me an error
Fatal error: Call to a member function view() on null in /home/ontutors/public_html/application/controllers/Home.php on line 18
Home.php code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
*
*/
class Home extends CI_Controller
{
function __construct()
{
}
function index()
{
//echo "Hello welcome";
$this->load->view('home/index');
}
}
?>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
*
*/
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
//echo "Hello welcome";
$this->load->view('home/index');
}
}
?>
Overwrite your code like this.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller
{
function index()
{
//echo "Hello welcome";
$this->load->view('home/index');
}
}
?>

extending core controller on codeigniter

I have placed this controller on application/core, called MY_base.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Base_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
function set_spaces($post_array, $primary_key){
$post_array['nombre']=str_replace(" ", "_", $post_array['nombre']);
return $post_array;
}
}
?>
and this controller on the controllers folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Clase extends Base_Controller {
function __construct(){
parent::__construct();
$this->load->library('grocery_CRUD');
}
function index(){
if($this->session->userdata('logged_in')){
$this->grocery_crud->set_table('clase');
$this->grocery_crud->set_subject('Clase');
$this->grocery_crud->required_fields('nombre');
$this->grocery_crud->unset_columns('id');
$this->grocery_crud->unset_print();
$this->grocery_crud->unset_export();
$this->grocery_crud->unset_add();
$this->grocery_crud->callback_before_update(array($this,'set_spaces'));
//$this->grocery_crud->set_theme('datatables');
$output = $this->grocery_crud->render();
//$this->load->view('header', $data);
$this->load->view('header2.php',$output);
$this->load->view('clase_view', $output);
}
else{
redirect('login', 'refresh');
}
}
}
?>
I think I've followed documentation's instructions correctly but I get this error
Fatal error: Class 'Base_Controller' not found in C:\xampp\htdocs\esi3d\application\controllers\clase.php on line 3
It looks like is looking for base_controler on controllers folder not in core folder.
Place your MY_base.php also in controllers folder

prevent cookie when running script from CLI in codeigniter

Is there a way of preventing session class to set cookie and adding a record in database when calling it from the command line?
The simplest way I think would be to extend the Session class like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Session extends CI_Session
{
public function __construct()
{
$CI = get_instance();
if ($CI->input->is_cli_request())
{
return;
}
parent::__construct();
}
}
Just place it in the application/libraries folder. And with a controller like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller
{
public function no_cli_session()
{
$this->load->library('session');
$this->session->set_userdata('logged', 'yes');
}
}
When you call the function from your browser the session is set and when you call it from cli it is not.

Categories