CodeIgniter Forums
problem with require_once inside a view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: problem with require_once inside a view (/showthread.php?tid=64481)



problem with require_once inside a view - cupboy - 02-23-2016

I have a require_once 'inc/functions.php'; in a view and it works fine.
I would like to move functions.php to the same folder that the css files are in.
This css files are accessed like this: href=$baseurl.'inc/newmenu.css'
However I cannot access functions.php like that (require_once $baseurl.'inc/functions.php'; ) and get the error:
Failed opening required 'http://localhost:8030/inc/functions.php'
So, how might I access it then?


RE: problem with require_once inside a view - skunkbad - 02-24-2016

require and require_once are generally for paths, not URLs. Your use of $baseurl would seem to indicate that you are trying to load a URL. Instead, try to use constants like FCPATH, which is the path to the "Front Controller" or index.php. Like this:

FCPATH . 'inc/functions.php'


RE: problem with require_once inside a view - cupboy - 02-24-2016

(02-24-2016, 12:04 AM)skunkbad Wrote: require and require_once are generally for paths, not URLs. Your use of $baseurl would seem to indicate that you are trying to load a URL. Instead, try to use constants like FCPATH, which is the path to the "Front Controller" or index.php. Like this:

FCPATH . 'inc/functions.php'

Excellent! Never heard of FCPATH before but it works great. Thanks.


RE: problem with require_once inside a view - Narf - 02-24-2016

(02-24-2016, 10:45 AM)cupboy Wrote:
(02-24-2016, 12:04 AM)skunkbad Wrote: require and require_once are generally for paths, not URLs. Your use of $baseurl would seem to indicate that you are trying to load a URL. Instead, try to use constants like FCPATH, which is the path to the "Front Controller" or index.php. Like this:

FCPATH . 'inc/functions.php'

Excellent! Never heard of FCPATH before but it works great. Thanks.

Glad to see that you've learned something new in FCPATH, but there are two more important things to note here:

- Having scripts such as your functions.inc.php in a publicly-accessible area is a bad idea in the first place.
- require_once() (and its similar counterparts) load local filepaths, not URLs.