including with relinking

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

including with relinking

Post by kendall »

hey guys,

got a question...how do you guys handle the url paths of links and images for included files when the 'includer file' is different from the 'included file' ...

to use a pseudo example
i have /default/test1.php
and i have /custom/test2.php

in test1.php i include test2.php with test2.php having a link to an image (image/test.jpg) /custom/images/test.jpg. The obvious solution would be to 'copy' the test.jpg over to the default/images/ but is there anyway to avoid having the same file in to different places?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: including with relinking

Post by John Cartwright »

use absolute paths
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: including with relinking

Post by RobertGonzalez »

Or set a global var that points up to the root of your app and use that throughout. Though I like Jcarts idea better.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: including with relinking

Post by Ambush Commander »

The usual solution is absolute paths. If you want to get fancy, you can try to run the included file through a post-processor that munges things accordingly.
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: including with relinking

Post by Popcorn »

sorry, absolute, that would mean a 'literal' right?
so it is common to use a literal over a 'global' var?
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: including with relinking

Post by kendall »

Yeah....i had to use absolute....theres no getting around it...

tanks guys
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: including with relinking

Post by josh »

In the including file:

Code: Select all

 
// this file is located in /var/www/public_html/tests/
define( 'MY_PATH', dirname(__FILE__));
include('../other/arbitrary/path/included.php');
 
in included.php:

Code: Select all

 
echo MY_PATH; //  /var/www/public_html/tests/
 
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: including with relinking

Post by Popcorn »

i don't know why anyone would want to use path literals, ever. when adding to sites or doing recovery or upgrade installs (yuck), path fixing is pretty much step number one.
'jshpro2's define() is good but i'd say you'd want this to be available everywhere and not have only *some* 'big' concept values like the example 'MY_PATH', available just for the instance you are fixing.
there are lots of $_SERVER variables for filenames and paths that are i think used by loads of us. why not, like 'Everah' said, define a few more constants (or even variables) that refer to the documentroot and also some other common dirs.
they can be available everywhere like $_SERVER and mean you never have to use literals.
despite fear about global vars, and modularizing scripts that depend on particular vars, surely adding and maintaining those vars has to be easier than littering your scripts with literals ...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: including with relinking

Post by RobertGonzalez »

$_SERVER variables are insecure and should not be used for anything that have any importance to you.

jshspro's suggestion, for the time being, is a good way to go. And a heads up, in PHP 5.3 there is going to be a __DIR constant introduced that will take care of all of this for you.
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: including with relinking

Post by Popcorn »

ok, totally clueless about this stuff, but i looked a little but what i can see is that there is a specific problem with $_SERVER['PHP_SELF'] and a couple of others (http://seancoates.com/xss-woes).

i am using $_SERVER['DOCUMENT_ROOT'] and $_SERVER['SCRIPT_FILENAME']. anyone know of problems with these? (you know there's no mention in the PHP docs of the PHP_SELF thing. they do omit things sometimes don't they?)

AFAIK , it looks like "__DIR__ == dirname(__FILE__)" which is for the current file (not the main/parent/includer), so this may not help.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: including with relinking

Post by RobertGonzalez »

Not all server variables that you see in tutorials are going to be available to you. PHP_SELF is one of them. Never rely on it being available to you. Instead consider using either the constant __FILE__ or the SCRIPT_FILENAME server var.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: including with relinking

Post by Jenk »

.. or

Code: Select all

realpath('.');
Post Reply