Jump to content

using a CONSTANT in include() statement


BuildMyWeb
Go to solution Solved by maxxd,

Recommended Posts

i dont know if im having a brain fart or maybe server settings are interfering.

 

i have an include statement such as:

include( C_URL . 'incs/header.php' );

i echoed out the constant C_URL after the include statement to make sure it is assigned and it seems to be.  however, the file is not being included.  i belive it is interpreting my path literally.   so it includes 'C_URLincs/header.php' instead of 'http://mydomain.com/incs/header.php'.

 

help is appreciated!

Link to comment
Share on other sites

ill try to explain what im trying to do and why a little better.

 

when im developing a site on a test server, the current project might be buried in a sub directory.  so my include path might look something like: 'http://mydomain.com/dev/2/incs/header.php'.  eventually the production server might have my project at the root level. so i want my include path to look like this: 'http://mynewdomain.com/incs/header.php'.

 

rather than rewriting include paths on each file when the project is moved to production, i wanted to prepend a variable string to the filename that is in one central place.  so in a settings.php file i define a CONSTANT:

define( 'C_URL', 'http://mydomain.com/dev/2/' );

i then use that constant in all of my include statements as such:

include( C_URL . 'incs/header.php' );

therefore, whenever the project is moved, i only have to make a one-time change to the constant definition in settings.php.  ive read in other forums that using variables in an include statement ( 

include( $url . 'incs/header.php' );

) is a security risk but constants are not.  but having no luck.  i will check to see if allow_url_fopen and allow_url_include settings are turned on.  but it sounds like some of you are advising me NOT to pursue this course.  is it a security risk?  or bad practice for some other reason?




			
		
Link to comment
Share on other sites

Its perfectly fine to use a constant in your includes. The problem is you are including your files using urls. Including files using urls results in different behaviour, as I said it only returns the output of the script you are including, meaning if you are going to use variables, functions etc defined from your parent script in the script you are going to include they will not be available.

 

For my projects I always use  dirname(__FILE__) in my main file to get the root directory for my application, I usually define the constant like

define('ROOT', dirname(__FILE__));

Then prepend ROOT constant to my file paths. This way no matter where my application is located on the server it will always find the files I am trying to include

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.