Using a constant in require_once
Asked Answered
C

1

7

I'm using the following to define root while in development:

define('LOCAL_URL', 'http://localhost/~xampp/Mysite');
define('REMOTE_URL', 'http://example.com');
define('DEV_VERSION', true);
if(DEV_VERSION)
    define('URL', LOCAL_URL);
else
    define('URL', REMOTE_URL);

This concept is new to me and I'm a bit of a PHP beginner so please be gentle.

I'm trying to use 'URL' in 'require_once' and I can't seem to get it to work (as in the required file is not included). I've tried the following:

require_once URL.'/phpincludes/coming-events.php';
require_once(URL.'/phpincludes/coming-events.php');
require_once URL . '/phpincludes/coming-events.php';
require_once (URL . '/phpincludes/coming-events.php');
require_once(URL.'phpincludes/coming-events.php');

I've checked http://www.php.net/manual/en/function.require-once.php and have searched on Stackoverflow and I'm not really making any headway at all.

I'm sure it's a really stupid and basic error I'm making here, but I'm at a loss and would really appreciate some help!

Claritaclarity answered 3/11, 2011 at 3:54 Comment(3)
Looks like you got bad advise with your last question. Use the different base URLs only for outputting HTML references. (Regarding that stylesheet issue, just use it in <base href=...> to compensate).Brnaby
Thanks mario. So I shouldn't even be using it for img paths? ie. <img src="<?php echo URL ?>/images/something.jpg">Claritaclarity
A single base href will make images and stylesheets work. But only if you have relative paths. Otherwise you need the URL prefix everywhere.Brnaby
I
4

The problem is probably because URL has a protocol, and it is trying to open the file over HTTP.

Including files remotely is disabled by default in PHP (allow_url_include), and for good reason.

You should be passing relative or absolute paths to files in your filesystem to be included.

Ironwood answered 3/11, 2011 at 3:58 Comment(1)
Thanks Alex. I'm using define so I can use relative paths, so now I'm really confused. I posted this question yesterday which resulted in my using define. I don't fully understand what you're saying but I'll... uhm... try something else perhaps?Claritaclarity

© 2022 - 2024 — McMap. All rights reserved.