tmallen Posted October 25, 2007 Share Posted October 25, 2007 I set a pretty global variable ($base) that defines the site root. Very useful with my includes. My only problem is using this URL prefix in include() arguments. Example: <?php $base = '/mybaseurl/'; include($base . 'includes/header.php'); ?> I've tried smushing $base in there in many different ways, and I ALWAYS get errors. How can I fit $base into this sort of statement? Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/ Share on other sites More sharing options...
kenrbnsn Posted October 25, 2007 Share Posted October 25, 2007 That should work fine. What errors are you getting? Ken Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377952 Share on other sites More sharing options...
roopurt18 Posted October 25, 2007 Share Posted October 25, 2007 Remember that the path is relative to the file system and not the URL. So in your example: $base = '/mybaseurl/'; PHP would start at the highest directory (root) and look for the directory mybaseurl, which most likely does not exist. Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377955 Share on other sites More sharing options...
rajivgonsalves Posted October 25, 2007 Share Posted October 25, 2007 Basically in includes they are always path structure not URL (if you include a URL it would give you the output of the php source not the source itself) so if you can provide some code it would be helpful... you could use $_SERVER['DOCUMENT_ROOT'] for apache to define your root directory more information on the same can be found at http://in.php.net/function.include Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377956 Share on other sites More sharing options...
tmallen Posted October 25, 2007 Author Share Posted October 25, 2007 Thanks for the help guys. I'm manually setting my own root because I'm building locally and then migrating a couple times. Using a $base URL lets me easily adapt my site to different environments. Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377960 Share on other sites More sharing options...
tmallen Posted October 25, 2007 Author Share Posted October 25, 2007 Oh, I understand my problem (sort of). Obviously $base couldn't be called because $base is defined in header.php! What's a better way of declaring this $base variable so that I can easily use it throughout the site? My thinking just feels...backward. Also, $base calls within my PHP code don't work even when added properly with $base defined (include() calls, mainly), but <?php print $base; ?> works fine inline (calling an image, stylesheet, etc.). Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377977 Share on other sites More sharing options...
rajivgonsalves Posted October 25, 2007 Share Posted October 25, 2007 tmallen you should have two base Constanst one for including file which would be something like $base_include = $_SERVER['DOCUMENT_ROOT']... and one for all your browser output (html, javascript, etc) $base_http = $_SERVER['HTTP_HOST']... hope it is helpful Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377984 Share on other sites More sharing options...
tmallen Posted October 25, 2007 Author Share Posted October 25, 2007 Problem is I can't edit $_SERVER['Doc...']. I would just precede my statements with the root indicator ('/') if that were the case. I'm declaring my own site root here, so that no matter where I deploy, I only have to change a single variable to adapt to the server and directory structure. Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377987 Share on other sites More sharing options...
roopurt18 Posted October 25, 2007 Share Posted October 25, 2007 You may be running into problems with variable scope. The easiest way around this is to use mod_rewrite to direct all request through a single PHP script. From that script you can define constants to use throughout the site; I highly recommend using constants over a variable for what you're doing. Example directory structure: /home/username/public_html/ /home/username/public_html/index.php /home/username/public_html/constants.php /home/username/public_html/Models /home/username/public_html/Models/user.php /home/username/public_html/Models/news.php First you set up mod_rewrite to direct all requests through index.php. index.php require_once(realpath(dirname(__FILE__)) . 'constants.php'); // Examine the URL and route as necessary constants.php define( 'DIR_SRC', realpath(dirname(__FILE__)) ); // source root directory define( 'DIR_MODELS', DIR_SRC . 'Models/' ); // Model directory // define more constants as necessary random_file.php // We got here through index.php, thus all the constants in constants.php are declared include(DIR_MODELS . 'user.php'); $user = new User(); $user->throwOffCliff(); Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377991 Share on other sites More sharing options...
tmallen Posted October 25, 2007 Author Share Posted October 25, 2007 OK, thanks. Let me wrap my brain around this one for a minute... Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-377997 Share on other sites More sharing options...
tmallen Posted October 25, 2007 Author Share Posted October 25, 2007 Where should I put constants.php? Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-378145 Share on other sites More sharing options...
tmallen Posted October 25, 2007 Author Share Posted October 25, 2007 First you set up mod_rewrite to direct all requests through index.php. How do I do this? I don't have SSH access, is there a way to do this with PHP or something? Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-378166 Share on other sites More sharing options...
tmallen Posted October 25, 2007 Author Share Posted October 25, 2007 One last thing, identical paths beginning with "/" are working for images, etc. but failing on plain include() statements. Why would this be? Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-378171 Share on other sites More sharing options...
roopurt18 Posted October 25, 2007 Share Posted October 25, 2007 Both constants.php and index.php should reside in your top-level web-accessible directory; this is usually the directory you land in when you FTP to your account. If your web host supports it, you can set up mod_rewrite via a .htaccess file; it should reside in the same directory as index.php and constants.php. I won't bother with the implementation details as there's plenty of tutorials on it and even a specific thread area on this discussion board for mod_rewrite. One last thing, identical paths beginning with "/" are working for images, etc. but failing on plain include() statements. Why would this be? Depends on how you're using the images. If you're just passing URLs back to the browser then it's not the PHP interpretor that's seeing the paths but the browser, in which case '/' would refer to the directory starting from public_html (or www or whatever your web host calls it). Remember, / is the top-level directory on a *nix file system. Any time in your PHP code you try and work on a file: /path/to/some/file it will look for that file relative to the top-level directory of the file system. Basically, / on *nix is equivalent to c:\ on Windows. The reality is that you probably have an account on that file system and your account is mapped to: /home/users/your_account_name/ And finally within your directory is the one accessible via web browsers, usually named public_html or www: /home/users/your_account_name/public_html Thus in your PHP code: <?php include('/some/include/file.php'); /* Above maps to: /some/include/file.php It DOESN'T map to: /home/users/your_account_name/some/include/file.php */ ?> However if you were to do: <?php echo '<img src="/images/house.jpg" alt="A House"/>'; ?> This path is seen by the web browser. The web browser sends a request to www.yourdomain.com for the file: /images/house.jpg As far as the web browser is concerned, the path / is mapped to: /home/users/your_account_name/public_html Thus, the path: /images/house.jpg is mapped to: /home/users/your_account_name/public_html/images/house.jpg I probably didn't explain that as clearly as I could have, so here is an attempt at a short summary. A file path beginning with '/' from within PHP refers to the root directory of the server. This is not the same thing as a src attribute in the HTML that begins with '/', which simply means start the path at http://www.yourdomain.com/. The reason its not the same is http://www.yourdomain.com is not mapped to the root directory of the entire server; instead, it is mapped to the web-accessible directory of your account. Quote Link to comment https://forums.phpfreaks.com/topic/74763-include-a-scalar-in-my-include-argument/#findComment-378190 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.