Showing posts with label Codeigniter. Show all posts
Showing posts with label Codeigniter. Show all posts

Saturday, May 13, 2017

[Codeigniter] Find out your version of Codeigniter

Go to directory of your Codeigniter project and look for the file at :
/system/core/CodeIgniter.php
For example:


 And then search for define with name "CI_VERSION" :


For this case, CodeIgniter version 2.1.4 is found.

Reference

http://stackoverflow.com/questions/2196799/which-version-of-codeigniter-am-i-currently-using

Wednesday, April 27, 2016

[Codeigniter] Call to undefined function base_url()

base_url() in a function in Url helper , please check have you loaded it before you use.
$this->load->helper('url');
for example, load that for specified function :


Or load for all functions in controllers :

Or you can load that to you whole website, load globally. just edit application/config/autoload.php and add 'url' as one of the value of $autoload['helper']:

$autoload['helper'] = array('url');



Reference:
http://stackoverflow.com/questions/11581636/fatal-error-call-to-undefined-function-base-url-in-c-wamp-www-test-ci-applic

Wednesday, February 17, 2016

[Codeigniter 2][Resolved] Unable to access an error message corresponding to your field name

This is the example code return the validation error  message as "Unable to access an error message corresponding to your field name":
function _article_valid() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('url', 'Url', 'callback_url_check');
    return $this->form_validation->run();
}    

function url_check($url){
    if ($url == 'test') {
        $this->form_validation->set_message('url', 'The field can not be the word "test"');
        return FALSE;
    }else{
        return TRUE;
    }  
}
And i found the problem is caused by the text in red with yellow background shown above.

Firstly, according example code shown at official site, that value same as the callback function name, and also same as function name after "callback_" rule prefix :




and then I change the first argument of set_message which's same as the callback function name and found its work.

function url_check($url){
    if ($url == 'test') {
        $this->form_validation->set_message('url_check', 'The field can not be the word "test"');
        return FALSE;
    }else{
        return TRUE;
    }  
}

of cos it's work for this too:
function _article_valid() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('pwd', 'pwd', 'callback_pwd_valid');
    return $this->form_validation->run();
}    

function pwd_valid($pwd){
    if ($pwd == 'test') {
        $this->form_validation->set_message('pwd_valid', 'The field can not be the word "test"');
        return FALSE;
    }else{
        return TRUE;
    }  
}
And according the article by brianantonelli in brianistech.wordpress.com, you can also change the first argument of set_message function to __FUNCTION__ , also is a solution. (tested works in this articcase)
function url_check($url){
    if ($url == 'test') {
        $this->form_validation->set_message(__FUNCTION__, 'The field can not be the word "test"');
        return FALSE;
    }else{
        return TRUE;
    }  
} 

Reference:

http://www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks 
https://brianistech.wordpress.com/2010/11/22/unable-to-access-an-error-message-corresponding-to-your-field-name/

Wednesday, May 27, 2015

[Codeigniter][Resolved] Remove index.php from url

To remove the “index.php” away from url, following the official documents only is not always works. After some searching in google found following a tutorial from Formget.com can do a good jobs and let share here:

Edit Config.php file:
 look for the "application/config/config.php" file, change the value of $config['index_page'] from "index.php" to "".
 From
$config['index_page'] = "index.php"; 
To
$config['index_page'] = "";


And Then change the value of $config['uri_protocol'] from "AUTO" to "REQUEST_URI":



.htaccess
Create an empty files named ".htaccess" at your Codeigniter project root, and paste these code inside the file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L] 







Reference:
https://ellislab.com/codeigniter/user-guide/general/urls.html
http://www.formget.com/codeigniter-htaccess-remove-index-php/

Wednesday, April 1, 2015

[Codeigniter] Use of undefined constant VIEWPATH - assumed 'VIEWPATH' system/core/Exceptions.php



*** This is for version 2.0 ***

You can download the new Codeigniter package and unzip it. Copy the original index.php of root and replace the existing one in your project.

You may edited something and make files can’t get constant “VIEWPATH” defined in this statement (around line 283)
define('VIEWPATH', $view_folder);
Or you can copy this content in your index.php located at project root.

 <?php
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.2.4 or newer
 *
 * This content is released under the MIT License (MIT)
 *
 * Copyright (c) 2014, British Columbia Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @package    CodeIgniter
 * @author    EllisLab Dev Team
 * @copyright    Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
 * @copyright    Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
 * @license    http://opensource.org/licenses/MIT    MIT License
 * @link    http://codeigniter.com
 * @since    Version 1.0.0
 * @filesource
 */

/*
 *---------------------------------------------------------------
 * APPLICATION ENVIRONMENT
 *---------------------------------------------------------------
 *
 * You can load different configurations depending on your
 * current environment. Setting the environment also influences
 * things like logging and error reporting.
 *
 * This can be set to anything, but default usage is:
 *
 *     development
 *     testing
 *     production
 *
 * NOTE: If you change these, also change the error_reporting() code below
 */
    define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 *
 * Different environments will require different levels of error reporting.
 * By default development will show errors but testing and live will hide them.
 */
switch (ENVIRONMENT)
{
    case 'development':
        error_reporting(-1);
        ini_set('display_errors', 1);
    break;

    case 'testing':
    case 'production':
        ini_set('display_errors', 0);
        if (version_compare(PHP_VERSION, '5.3', '>='))
        {
            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
        }
        else
        {
            error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
        }
    break;

    default:
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'The application environment is not set correctly.';
        exit(1); // EXIT_ERROR
}

/*
 *---------------------------------------------------------------
 * SYSTEM FOLDER NAME
 *---------------------------------------------------------------
 *
 * This variable must contain the name of your "system" folder.
 * Include the path if the folder is not in the same directory
 * as this file.
 */
    $system_path = 'system';

/*
 *---------------------------------------------------------------
 * APPLICATION FOLDER NAME
 *---------------------------------------------------------------
 *
 * If you want this front controller to use a different "application"
 * folder than the default one you can set its name here. The folder
 * can also be renamed or relocated anywhere on your server. If
 * you do, use a full server path. For more info please see the user guide:
 * http://codeigniter.com/user_guide/general/managing_apps.html
 *
 * NO TRAILING SLASH!
 */
    $application_folder = 'application';

/*
 *---------------------------------------------------------------
 * VIEW FOLDER NAME
 *---------------------------------------------------------------
 *
 * If you want to move the view folder out of the application
 * folder set the path to the folder here. The folder can be renamed
 * and relocated anywhere on your server. If blank, it will default
 * to the standard location inside your application folder. If you
 * do move this, use the full server path to this folder.
 *
 * NO TRAILING SLASH!
 */
    $view_folder = '';


/*
 * --------------------------------------------------------------------
 * DEFAULT CONTROLLER
 * --------------------------------------------------------------------
 *
 * Normally you will set your default controller in the routes.php file.
 * You can, however, force a custom routing by hard-coding a
 * specific controller class/function here. For most applications, you
 * WILL NOT set your routing here, but it's an option for those
 * special instances where you might want to override the standard
 * routing in a specific front controller that shares a common CI installation.
 *
 * IMPORTANT: If you set the routing here, NO OTHER controller will be
 * callable. In essence, this preference limits your application to ONE
 * specific controller. Leave the function name blank if you need
 * to call functions dynamically via the URI.
 *
 * Un-comment the $routing array below to use this feature
 */
    // The directory name, relative to the "controllers" folder.  Leave blank
    // if your controller is not in a sub-folder within the "controllers" folder
    // $routing['directory'] = '';

    // The controller class file name.  Example:  mycontroller
    // $routing['controller'] = '';

    // The controller function you wish to be called.
    // $routing['function']    = '';


/*
 * -------------------------------------------------------------------
 *  CUSTOM CONFIG VALUES
 * -------------------------------------------------------------------
 *
 * The $assign_to_config array below will be passed dynamically to the
 * config class when initialized. This allows you to set custom config
 * items or override any default config values found in the config.php file.
 * This can be handy as it permits you to share one application between
 * multiple front controller files, with each file containing different
 * config values.
 *
 * Un-comment the $assign_to_config array below to use this feature
 */
    // $assign_to_config['name_of_config_item'] = 'value of config item';



// --------------------------------------------------------------------
// END OF USER CONFIGURABLE SETTINGS.  DO NOT EDIT BELOW THIS LINE
// --------------------------------------------------------------------

/*
 * ---------------------------------------------------------------
 *  Resolve the system path for increased reliability
 * ---------------------------------------------------------------
 */

    // Set the current directory correctly for CLI requests
    if (defined('STDIN'))
    {
        chdir(dirname(__FILE__));
    }

    if (($_temp = realpath($system_path)) !== FALSE)
    {
        $system_path = $_temp.'/';
    }
    else
    {
        // Ensure there's a trailing slash
        $system_path = rtrim($system_path, '/').'/';
    }

    // Is the system path correct?
    if ( ! is_dir($system_path))
    {
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME);
        exit(3); // EXIT_CONFIG
    }

/*
 * -------------------------------------------------------------------
 *  Now that we know the path, set the main path constants
 * -------------------------------------------------------------------
 */
    // The name of THIS file
    define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));

    // Path to the system folder
    define('BASEPATH', str_replace('\\', '/', $system_path));

    // Path to the front controller (this file)
    define('FCPATH', str_replace(SELF, '', __FILE__));

    // Name of the "system folder"
    define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));

    // The path to the "application" folder
    if (is_dir($application_folder))
    {
        if (($_temp = realpath($application_folder)) !== FALSE)
        {
            $application_folder = $_temp;
        }

        define('APPPATH', $application_folder.DIRECTORY_SEPARATOR);
    }
    else
    {
        if ( ! is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR))
        {
            header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
            echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
            exit(3); // EXIT_CONFIG
        }

        define('APPPATH', BASEPATH.$application_folder.DIRECTORY_SEPARATOR);
    }

    // The path to the "views" folder
    if ( ! is_dir($view_folder))
    {
        if ( ! empty($view_folder) && is_dir(APPPATH.$view_folder.DIRECTORY_SEPARATOR))
        {
            $view_folder = APPPATH.$view_folder;
        }
        elseif ( ! is_dir(APPPATH.'views'.DIRECTORY_SEPARATOR))
        {
            header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
            echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
            exit(3); // EXIT_CONFIG
        }
        else
        {
            $view_folder = APPPATH.'views';
        }
    }

    if (($_temp = realpath($view_folder)) !== FALSE)
    {
        $view_folder = $_temp.DIRECTORY_SEPARATOR;
    }
    else
    {
        $view_folder = rtrim($view_folder, '/\\').DIRECTORY_SEPARATOR;
    }

    define('VIEWPATH', $view_folder);


/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 */
require_once BASEPATH.'core/CodeIgniter.php';

/* End of file index.php */
/* Location: ./index.php */


Reference:

Monday, February 9, 2015

[CodeIgnitor][Resloved] Cannot get user_data from database session

I have visited the page in stackoverflow many times but never aware this is the solution works for me. Really thanks so much for vlad.

There is my story:
I was using this table to store the session data:

Since I cleared all my session record in database, I visited 2-3 page, ci generated around 23 records.


However, I can’t get the userdata I set in session.




And after many test and tried many method, found as vlas mentioned , my user_agent in database table is not long enough to store the user agent, some string was trim and cannot be recorded.


So the solution is using sql to change the length of user_agent column.
ALTER TABLE ci_sessions
CHANGE COLUMN `user_agent` `user_agent` VARCHAR(200) NOT NULL;

And then the user_agent is recorded is not "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (K" anymore, it's "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"now and the session feature works really nice!

Reference:
http://stackoverflow.com/questions/2438835/codeignitor-is-generating-multiple-sessions-in-the-database-why

Wednesday, April 16, 2014

[Codenigter][Solved] $this- form_validation- run() not working , always return false

The idea to solve the problem is from stackoverflow. The link was posted at the end of this post but the solution of my case is not exactly same as the answer.

Firstly, let have an overviews of my case:
function _slider_validation($item_no) {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('slider_title', 'TITLE NOT OKAY', 'required');
        $this->form_validation->set_rules('slider_url', 'URL NOT OKAY', 'required');
        $this->form_validation->set_rules('slider_photo', 'PHOTO NOT OKAY', 'required');
        return $this->form_validation->run();
    }

And referenced to Stackoverflow, I found I missed loading form helper in my case:

function _slider_validation($item_no) {
        $this->load->helper('form'); //<-- I missed this line !
        $this->load->library('form_validation');
        $this->form_validation->set_rules('slider_title', 'TITLE NOT OKAY', 'required');
        $this->form_validation->set_rules('slider_url', 'URL NOT OKAY', 'required');
        $this->form_validation->set_rules('slider_photo', 'PHOTO NOT OKAY', 'required');

        return $this->form_validation->run();
    }

So If your $this- form_validation- run() not working , always return false, try checking these points:
1) Remember checking you have load the form helper
2) load form_validation library
3) and set validation rules

Official's reason about why form_validation return false:
Since you haven't told the Form Validation class to validate anything yet, it returns FALSE (boolean false) by default. The run() function only returns TRUE if it has successfully applied your rules without any of them failing.

Reference:
http://stackoverflow.com/questions/17000803/codeigniter-form-validation-run-always-returns-false

Monday, March 10, 2014

[CodeIgniter][Resolved] Call to a member function view() on a non-object



If you got this error message you should check have you extended the CI_controller first. Such as text in yellow background below:

class index extends CI_Controller {}


If you have checked you have extended the CI_controller but still got “Undefined property: index::$load” error. You should try adding the class constructor to your code,

Here is my example case:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class index extends CI_Controller {
    public function index()
    {
        $this->load->view(‘home’);
    }
}


Add text in green:


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class index extends CI_Controller {
    public function __construct(){
        parent::__construct();
        // Your own constructor code
    }
    public function index() {
        $this->data['template'] = "dynamic/home";
        $this->data['pagetitle'] = "home";       
        $this->load->view(‘home’);
    }
}


Reference:
CodeIgniter Version 2.2.0

Sunday, March 2, 2014

[Codeigniter] Show .html extension in URL

The way was found at Stackoverflow, Answered by Peter

Go to your codeigniter directory and look for the config.php file
/application/config/config.php

Go to line 60 and change the linr to this:

$config['url_suffix'] = '.html';

Line to edit was highlightedat the image below:

 


Reference:
http://stackoverflow.com/questions/5309490/how-do-i-render-php-url-with-html-extension


[CodeIgniter][solved] Unable to load the requested class: database

Finally found used the wrong syntax to import database class.

Use
$this->load->database();
Instead of
$this->load->library('database');
  


The reason why cause this error ?
We need to import "database class" but not "database library",
so use "$this->load->database();" but not "$this->load->library('database');"


Referance:
http://stackoverflow.com/questions/18045342/codeigniter-loading-database-error