20

Hi all I have a site developed in codeigniter and I wanto to store into a file called common.php some javascript/PHP function that I use in many pages. I have tried in this mode:

require(base_url().'application/libraries/common.php'); //I have tried also include

This return me this error:

A PHP Error was encountered

Severity: Warning

Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0

I'm going to my php.ini and I turn On allow_url_include, restart apache and when I try to load the page return me now this error:

A PHP Error was encountered

Severity: Warning

Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0

Filename: backend/hotel_view.php

Line Number: 6

A PHP Error was encountered

Severity: Warning

Message: require(http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php) [function.require]: failed to open stream: no suitable wrapper could be found

Filename: backend/hotel_view.php

Line Number: 6


Fatal error: require() [function.require]: Failed opening required 'http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/public/klikkahotel.com/application/views/backend/hotel_view.php on line 6

What can I do to include a simple file into my pages?

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • There are so many answer given below but it depends on the size of your common file whether you want to make a helper function or directly view.but according to me you should always keep js and php in different files for good practices !! – saurabh kamble Oct 05 '15 at 06:52

8 Answers8

39

Pull it into whichever views you want using $this->load->view('common'); You can include other views from either the controller or the view.

Example 1

your_controller.php

public function index() {
   $this->load->view('homepage');
}

views/homepage.php

<?php
$this->load->view('common');
?>

<body>
  <!-- html -->
</body>

Example 2

your_controller.php

public function index() {
  $this->load->view('common');
  $this->load->view('homepage');
}
Ayush
  • 41,754
  • 51
  • 164
  • 239
  • read what MVC stands for. This is how CodeIgniter is build. If all of us would build everything where we want, without knowing what we do, what's the point in using MVC standards or a framework? – machineaddict May 08 '13 at 21:45
  • This is bad practices you cannot load same view for all the request all the time you should use helpers – saurabh kamble Oct 05 '15 at 06:54
  • @saurabh2836: There is no need to pull it in for every request all the time. If you see my examples, you only include the view when you need it (whether that be through the view or through the controller). – Ayush Oct 05 '15 at 18:35
  • @xbonez as per the question common view will be required through out the project so you have to load this file in each function i think am i correct!!!! – saurabh kamble Oct 06 '15 at 04:17
  • Not in each function, just the ones you need it in. If this is done as a helper, wouldn't you need to call the helper multiple times as well? – Ayush Oct 06 '15 at 04:21
2

You should use APPPATH or BASEPATH or just type the full path to the file.

For security, require_once should be passed a local file, not a URL. I wouldn't really suggest using require_once() in CodeIgniter. It might be better to use:

$this -> load -> view('common_file');

ajtrichards
  • 29,723
  • 13
  • 94
  • 101
2

How To Make Global Functions In CodeIgnitor

To create global functions, define them in CodeIgnitor Helper files and auto load them. Here's how:

Create Your Helper

To create [helpers][2], create a .php file in the application/helpers/ folder and save your functions there.

Note: It is good practice to use the following format, to avoid function name collisions:

if ( ! function_exists('my_function_name'))
{
    function my_function_name($arg)
    {
        /* Your code here */

    }
}

Making Them Global

If you use these functions all the time (Global), auto-load them.

  1. Open the file: /config/autoload.php
  2. Find the section $autoload['helper'] = array();
  3. Add the name of your helper file (excluding the.php extension).

For instance, if you created a helper file called myhelper.php, It should look something like this:

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

Using Your Global Functions

Now your functions will be available throughout the site. Just call them wholesale:

my_sample_function('argument');

1
  1. base_url() refers to the web path like http://localhost/myproject/. You cannot include a remote file, actually you should not. It's a security risk. See Can't include file on remote server

  2. Building a custom library is a good choice and if you are using it a lot in your website, you can include it in application/config/autoload.php under the section $autoload['libraries']. It will autoload every time you reload the application/website based on codeigniter. Example: $autoload['libraries'] = array('common'); if your library is called common.php and is located in application/libraries/

  3. DO NOT put functions into a viewer, that's why libraries and helpers exists. A viewer should contain only what a user should see. Example: a view is some form of visualisation of the model.

Community
  • 1
  • 1
machineaddict
  • 3,216
  • 8
  • 37
  • 61
  • Ok but if i want to load this function in certain pages and not in every? because for example I have a jQuery function and don't want that this function works in other pages – Alessandro Minoccheri May 09 '13 at 06:34
  • Is this library of yours a php helper to build javascript functions (like [this](https://github.com/EllisLab/CodeIgniter/blob/develop/system/libraries/Javascript.php))? Or is it a simple javascript function? – machineaddict May 09 '13 at 06:46
  • This file contain jQuery function with some PHP code inside, isn't an helper, only pure function – Alessandro Minoccheri May 09 '13 at 06:49
  • Usually, the javascript functions will go between tags and in document ready, like [this](http://api.jquery.com/ready/) – machineaddict May 10 '13 at 07:06
  • I know, but I have a lot of common function and isn't correct to repeat in every page – Alessandro Minoccheri May 10 '13 at 07:55
  • I structure my website like this: have a header, content and then footer. On my header there are the javascript functions (you can also put them in a .js file). Then I call the header on every page that I build. Hope this clears your mind. – machineaddict May 13 '13 at 06:30
1

You should use APPPATH or BASEPATH or just type the full path to the file. require_once(APPPATH.'libraries/common.php');

demongolem
  • 9,474
  • 36
  • 90
  • 105
prash.patil
  • 687
  • 8
  • 7
1

You Can include anyware using

<?php $this->load->view('file'); ?>
Fel
  • 354
  • 2
  • 12
0

For solve in more efficient way this problem I have done so:

You create a new helper (in application/helpers) with name (es. common_helpers.php, the underscore is important). In this file, you put all the functions for example build pieces of html in common.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    function getHead(){
    require_once(APPPATH."views/common/head.php");
    }   

    function getScripts(){
    require_once(APPPATH."views/common/scripts.php");
    }

    function getFooter(){
    require_once(APPPATH."views/common/footer.php");
    }

In your controller you call only one view in respect of MVC and call the functions from your custom helper.

class Hello extends CI_Controller {

   public function index(){
       $this->load->helper('common');
       $this->load->view('index');   
   }

}
Tudor
  • 633
  • 1
  • 7
  • 18
-2

You can write php function in helper file steps - create a helper file name common_helper inside application/helperfolder - and create a function like

    function getBoosters($id){
        $ci=& get_instance();
        $ci->load->database(); 

        $sql = "select * from boosters where id ='".$id."' "; 
        $query = $ci->db->query($sql);
        return $query->result();
   }

This common function you can use where you want by loading this helper.

Suppose you want to use this method in FrontController simply load the helper by this line

$this->load->helper('common');

Now you can call the method. Add your js code in footer.php, all the functions are available on every page you can easily use them

sfjac
  • 7,119
  • 5
  • 45
  • 69
Aman Attari
  • 181
  • 3
  • 12