PHP Help

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
Okay, so this is the reason why I never became a web developer... Too much hassle for something that seems as though it should be so simple. What i'm trying to do is get an include path to work for all directory levels so that I don't end up having to just hardcode the hyperlink in everywhere as an absolute path. The thing is, even if I was to do that, it basically eliminates the benefit of having apache and all of that on my local system for web development. And using the $_SERVER["DOCUMENT_ROOT"] variable also doesn't seem like a good thing because where I am developing locally, is not going to be the same path relative to $_SERVER["DOCUMENT_ROOT"] by the time I upload it to public view... So I would have hardcoded everything with paths relative to $_SERVER["DOCUMENT_ROOT"] locally, and before uploading via FTP to my website, they would yet again have to be changed.

So... What's the trick? Modifying php.ini for the include path? (Seems to be the best strategy thus far... Although this does mean that I need a php.ini everwhere where include is used am I right?)

Thing is... I've got stuff like this:
PHP:
<?php include('vars/variables.php'); ?>

<!-- START #Social_Bar -->
<div id="t_social_bar">
	<span><a href="https://twitter.com/TechLifeForum" target="_blank"><img src=<"images/social/twitter.png"></a></span>
	<span><a href="http://blogs.msmvps.com/aceinfinity/" target="_blank"><img src="images/social/wordpress.png"></a></span>
</div>
<!-- END #Social_Bar -->

So I'm assuming I'm going to have to <?php echo... those image paths as well relative to the root somehow. I've got .htaccess set up so that my includes folder denies access to public, for security reasons.

Just looking for some feedback on this before I go bald from pulling my hair out. :lol: This has always been annoying for me whenever I attempt to develop a PHP based website instead of strictly HTML. PHP makes web development faster, as long as you can get past annoying issues like this as far as I'm concerned. I just don't have enough PHP experience to know all the tricks.
 
Last edited:
Perhaps my explanation is confusing... Say I have an include directly from a PHP page on the web directories. I include sidebar.php, and since I know where I am including sidebar.php from... I easily know where to include from, whether that is:
Code:
../sidebar.php

or:
Code:
sidebar.php

or maybe even:
Code:
../../sidebar.php

Now inside this template, I have other includes, and references to other files, yet the relative path can't be the same from all levels that I call it from.

For instance, I call to include sidebar.php, which includes variables.php. I know where sidebar.php is, but how do I get this included template to know where variables.php is, RELATIVE, to where I called sidebar.php from. Because when sidebar.php includes variables.php... I might not know whether it is "../variables.php" or "variables.php", and I can't have both.

This is made an issue, because where I call sidebar.php, it may be from the root, or from within a folder from the root.
 
I don't have much experience with PHP, but maybe the application could be restructured to make this easier to deal with. Does sidebar.php need to be called from multiple levels?

Are all your includes in a single /includes/ folder, or separated out? vBulletin makes calls like this:

Code:
require_once('./includes/file.php');

Can you not just use a relative path shortcut? Assuming variables.php is never going to move, then:

Code:
require_once('public_html/includes/variables.php');

I think that works, it's a relative path to public_html from the directory you're in. If you're configuring this application for one specific server, and you know where the systen root is, then you could use something like:

Code:
require_once('/var/www/public_html/includes/variables.php');

The / defines an absolute path, starting at the system root.

Hmm, I tested public_html/includes etc on a site, couldn't get it work exactly like that. Not sure on the exact code (which makes my post pretty useless :grin1:), but is something like that what you mean?
 
Last edited:
Does sidebar.php need to be called from multiple levels?

Yes, I can have all my files in the same folder, but that's definitely not what I want as the folder I need to call the sidebar from (aside from root), will have an expanding number of php pages. Within a few years, I could have 50 php files all in root if I didn't call it from different levels to keep things organized.

All my includes are in an includes folder, the thing is, I can't call it like this:
require_once('public_html/includes/variables.php');

This would only work if I was in the root, if I was in a subfolder of root, this path, relative to that location would not exist. (And that's my problem here...)

I need to be able to figure out how to determine what the relative path is, according to where I include from... As I can't have multiple locations referenced for the include. I could recreate include folders everywhere to adapt to where I include from, but that's not what I would like either...

Code:
require_once('/var/www/public_html/includes/variables.php');

Something like that may just be what i have to do unfortunately... It's still a pain in the a$$ though because the folder structure from root on my localhost, is nowhere near what it would be like when I upload to the website. localhost it would be something like /dev/aceinfinity... ... And maybe on my website (I haven't seen), but it would be /public_html/... or something similar. Just to develop on my localhost and see what it looks like, it still takes work and changes just to see what it would look like on the site.

I guess the only thing to do is to re-create what the web directories look like, from my localhost so that I don't have to modify and fool around with stuff like that. Thinking about it, I think it's just the way I went about it with the way I have things set up on my localhost that is making things difficult for me.

Problem solved though I think for now... Thankyou :)

edit: I think I'm just going to have to see what my root is on the website, by echo'ing $_SERVER["DOCUMENT_ROOT"], and then I'll re-create that structure from the root of my localhost folder.
 
I would have thought there would be a shortcut to refer to public_html from any location, perhaps not. As for calling sidebar.php from multiple levels, I was just thinking about how a CMS would handle this problem.

I didn't think I'd be able to help much, I'm still pretty limited to modifying existing code at the moment. I might rummage through some Linux stuff and see if there is a simpler way of referring to the base directory, interesting problem.
 
I would have thought there would be a shortcut to refer to public_html from any location, perhaps not. As for calling sidebar.php from multiple levels, I was just thinking about how a CMS would handle this problem.

I didn't think I'd be able to help much, I'm still pretty limited to modifying existing code at the moment. I might rummage through some Linux stuff and see if there is a simpler way of referring to the base directory, interesting problem.

public_html doesn't exist for everybody. It depends on what host you have for having your website online..

So far the only solutions I have found is declaring hardcoded paths from the document root special variable, modifying .htaccess for the path, using set_ini(), or php.ini to define include paths.
 
I will take a look tomorrow, I'm done on the computer for today. I'll keep this tab open to take a look at those links later. :)
 
Last edited:
From what I read, it seems better to use ini_set() as I mentioned before because it is for all PHP versions.
 
From what I read, it seems better to use ini_set() as I mentioned before because it is for all PHP versions.

They change the same thing depending on what parameter of ini_set you change

I know, here was their example in the documentation though:
PHP:
<?php
// Works as of PHP 4.3.0
set_include_path('/usr/lib/pear');

// Works in all PHP versions
ini_set('include_path', '/usr/lib/pear');
?>
 
Back
Top