Best practice for using path from php var?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Best practice for using path from php var?

Post by flashpipe »

I've got a site that I'm doing in php using several includes that are shared by pages in various folders so I've got to use a full path to the links, graphics, etc.

We're developing on a test location on the client's server and will eventually migrate to the production area.

Is there an easy way to declare a php variable in the first include to set the path for all the graphics, links, etc.?

For example, I've got index.php and contact/index.php which call:

Code: Select all

<?php include("header.php") ?>
and

Code: Select all

<?php include("../header.php") ?>
respectively...

Now, in header.php I've got:

Code: Select all

<link rel="stylesheet" type="text/css" href="http://clientsite.com/css/style.css" />
and

Code: Select all

<script type="text/javascript" src="http://clientsite.com/js/jquery.js"></script>
and

Code: Select all

<a href="http://clientsite.com/page.html"><img src="http://clientsite.com/images/image.jpg" alt="1" border="0" /></a>
Is there a way (in header.php) to do something like:

Code: Select all

<?php  
$root= "http://clientsite.com/";
?>
If that will work, how would I write the above calls? Would I just have to wrap the php tags around them, or do I need to use echo?? Something like:

Code: Select all

<?php  
<link rel="stylesheet" type="text/css" href=$root.'/css/style.css' />
?>
Haven't done this with php before so I'm not sure what the best practice is and what the code details are...

THANKS!!
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Best practice for using path from php var?

Post by mikosiko »

use $_SERVER['DOCUMENT_ROOT']
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Re: Best practice for using path from php var?

Post by flashpipe »

Cool. Thanks! How would I structure that in my include statements?

Something like:

Code: Select all

<?php 
echo'<link rel="stylesheet" type="text/css" href='.($_SERVER["DOCUMENT_ROOT"].'"css/style.css"/>'
?>

or, are you saying that if I use:

Code: Select all

<?php include($_SERVER['DOCUMENT_ROOT']."header.php") ?>
I don't have to include the full pathname in my include.php file??

Thanks!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best practice for using path from php var?

Post by AbraCadaver »

For what its worth, I've always found this way a big hassle. I would always use a main index.php in the root that includes others like contact/something.php etc... This makes it much easier. But for your question, why use the full URL?

My way you would always use this:

Code: Select all

echo '<link rel="stylesheet" type="text/css" href="css/style.css"/>'
Your way you can do it like this (notice the /):

Code: Select all

echo '<link rel="stylesheet" type="text/css" href="/css/style.css"/>'
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Re: Best practice for using path from php var?

Post by flashpipe »

Hmmm...not sure I'm getting this...or I'm not communicating effectively...

I've got an include.php file in the root of the site, which imports all the css and js files that are needed by every page in the site.

However, my site is separated into numerous folders for the different menu items and each page in those sub-folders calls the same include.php file.

So, I was using the full path for the includes so that the sub-folder pages would "point" to the correct files...

...and, I was hoping to be able to define a variable in the include file that I could use that would give the complete pathname so that I don't have to change each link when the site changes servers/goes live.

Is that different than what you're thinking?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Best practice for using path from php var?

Post by requinix »

1. $_SERVER["DOCUMENT_ROOT"] is for including PHP files, not HTML content.

Code: Select all

include $_SERVER["DOCUMENT_ROOT"] . "/header.php";

2. If your site is clientsite.com then you prepend a slash to HTML srcs and hrefs to avoid having to specify the domain name too.

Code: Select all

<link rel="stylesheet" type="text/css"  href="/css/style.css" />
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Re: Best practice for using path from php var?

Post by flashpipe »

Hmmm....

If I put:

Code: Select all

<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes.php"; ?>
in my contact/index.php

and put:
<link rel="stylesheet" type="text/css" href="/css/style.css" />
in the includes.php file, it doesn't find the css sheet...or any of the other css or js files. Does it make a difference that this is on a development server in a sub-folder and not at the main domain level??
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best practice for using path from php var?

Post by AbraCadaver »

Yes, it makes a difference if it is in a sub directory and what you do won't work if you move it to another server and it not in that same sub directory. This is exactly why when I develop and app that any file that is loaded in the browser is in the site root directory. This is also why I said this is a big hassle.

If you do this, then everything is relative to the root so you can just do "css/style.css" no matter what server you're on or what sub directory you're in and also with includes. There may be strategies for how you're doing it, but I'll have to defer to someone else.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Best practice for using path from php var?

Post by flying_circus »

Abra makes some good points about using relative paths. It makes life easy for portability. It can also make life hell if you need to restructure (which you shouldn't).

I typically use a global config file and define all my common paths. It always feels a bit cumbersome to set up, but when you're done, it really makes portability easy by having only to modify 1 file.

Code: Select all

<?php
/* Start of config.php*/
  # Define Shorthand Directory Seperator
     define('DS', DIRECTORY_SEPARATOR);
    
  # Define Path's
     define('PATH_DOCROOT', DS . 'path' . DS . 'to' . DS . 'my' . DS . 'docroot'. DS);
    define('PATH_INCLUDES', PATH_DOCROOT . 'includes' . DS);
    define('PATH_CLASSES', PATH_INCLUDES . 'classes' . DS);
    define('PATH_MODULES', PATH_INCLUDES . 'modules' . DS);
    
  # Define URL's
     define('HTTP_ROOT', 'http://clientsite.com');
    define('HTTP_IMAGES', HTTP_ROOT . '/images/');
    define('HTTP_SCRIPTS', HTTP_ROOT . '/js/');
    define('HTTP_STYLES', HTTP_ROOT . '/css/');
/* End of config.php*/

/* Start index.php*/
  # Define Local Path
     define('PATH_LOCAL', dirname(__FILE__));
    
  # Include Config File
     include_once(PATH_LOCAL . '..' . DIRECTORY_SEPARATOR . 'private_config' . DIRECTORY_SEPARATOR . 'config.php');

  # Include Other Stuff
     include_once(PATH_CLASSES . 'database.php');
    include_once(PATH_CLASSES . 'email.php');
?>
<link rel="stylesheet" type="text/css" href="<?php print HTTP_STYLES; ?>style.css" />
<script type="text/javascript" src="<?php print HTTP_SCRIPTS ?>jquery.js"></script>
<a href="<?php print HTTP_ROOT; ?>/page.html"><img src="<?php print HTTP_IMAGES; ?>image.jpg" alt="1" border="0" /></a>
Neither way is better than the other, just choose what fits your project best.
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Re: Best practice for using path from php var?

Post by flashpipe »

Thank you both for the help! Excellent stuff. Might not be applicable on this site (since the client requested that separate "divisions" be organized in different folders).

flying_circus, your solution looks like it might fit much better for what I'm trying to accomplish. I'm wondering, is there an easy way to assign a php variable in my includes.php file that will reference the full pathname so at least I only have to change it in one place?

I think what I really needed was this line:
<?php print HTTP_STYLES; ?>

I was hoping that I could set a variable in the includes.php file:

Code: Select all

<?php 
$rootPath= "http://www.clientsite.com/";
?>
and then use the print function to define that path within the site:

Code: Select all

<link rel="stylesheet" type="text/css" href=<?php print rootPath; ?>"css/style.css" />
but I just tried that and it didn't work...hmmm...maybe I'm just stuck using full path names...ugg...
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Re: Best practice for using path from php var?

Post by flashpipe »

Ah! Wait, typo!! I think this will do it!!

Code: Select all

<link rel="stylesheet" type="text/css" href=<?php print $rootPath; ?>"css/style.css" />
Does that seem like it will create any problems?

Thanks!!!!
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Re: Best practice for using path from php var?

Post by flashpipe »

Also, do I need the quotation mark in there? When I rollover a menu link, it shows at http://www.clientsite.com/"contact-us/index.php in the status bar.

It navigates to the correct page, but I'm not sure if that will cause problems? Does it need to be there?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best practice for using path from php var?

Post by AbraCadaver »

flashpipe wrote:Also, do I need the quotation mark in there? When I rollover a menu link, it shows at http://www.clientsite.com/"contact-us/index.php in the status bar.

It navigates to the correct page, but I'm not sure if that will cause problems? Does it need to be there?
You need to move it to after the = sign.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
flashpipe
Forum Newbie
Posts: 17
Joined: Thu May 31, 2007 7:27 am

Re: Best practice for using path from php var?

Post by flashpipe »

That did it!! It's a beautiful thing! Thanks again for your help, both of you!!!
Post Reply