BuildMyWeb Posted July 17, 2015 Share Posted July 17, 2015 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! Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/ Share on other sites More sharing options...
fastsol Posted July 17, 2015 Share Posted July 17, 2015 You missed a / in the include include( C_URL . '/incs/header.php' ); Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516631 Share on other sites More sharing options...
Ch0cu3r Posted July 17, 2015 Share Posted July 17, 2015 When using urls to include files, PHP will only include output of the script, not the actual source code. You should use file system paths for including/requiring files. Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516632 Share on other sites More sharing options...
BuildMyWeb Posted July 17, 2015 Author Share Posted July 17, 2015 i have the constant defined with the slash: define( 'C_URL', 'http://mydomain.com/' ); Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516633 Share on other sites More sharing options...
mac_gyver Posted July 17, 2015 Share Posted July 17, 2015 a) does your C_URL value have a trailing / b) are both the allow_url_fopen and allow_url_include settings turned on so that you can use a URL in an include statement? c) are you sure you want to use a URL? see reply #3 Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516635 Share on other sites More sharing options...
BuildMyWeb Posted July 17, 2015 Author Share Posted July 17, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516638 Share on other sites More sharing options...
BuildMyWeb Posted July 17, 2015 Author Share Posted July 17, 2015 addendum: seems like allow_url_include=on permits remote includes. i understand thats bad. :/ Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516639 Share on other sites More sharing options...
Solution maxxd Posted July 17, 2015 Solution Share Posted July 17, 2015 Why not define the system path in the constant? Instead of 'http://example.com/site/2/' use '/public_html/your_account/site/2' - this way you can put your included files above the web root for security if you want, plus you still only have to change the constant value once across the site. 1 Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516640 Share on other sites More sharing options...
BuildMyWeb Posted July 17, 2015 Author Share Posted July 17, 2015 thank you maxxd. that is the solution i was looking for Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516641 Share on other sites More sharing options...
Ch0cu3r Posted July 17, 2015 Share Posted July 17, 2015 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/297339-using-a-constant-in-include-statement/#findComment-1516642 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.