Jump to content

Newbie question: dealing with paths


msaz87

Recommended Posts

I'm new to basing sites in PHP and a prevailing problem I've run into is dealing with relative/absolute paths.

 

I'll try to explain it as best I can and if anyone can give me some advice or let me know if I'm just setting things up poorly... I'd greatly appreciate it.

 

Suppose on all my pages I include a header.php -- which is located in an "includes" folder. So it would look like:

<? include('includes/header.php'); ?>

 

But if I have a page not located on the root, that path will be incorrect. So if this second page is in a different directory, one off the root, I adjust the include to:

<? include('../includes/header.php'); ?>

 

But now the problem is that the CSS, JS and other files being linked to within that header document are also off.

 

One solution would be to use absolute paths, but for the sake of developing in an environment, I'd rather not do that.

 

Is there an easy way to deal with this that I'm just unaware of?

 

Thanks so much!

 

Link to comment
https://forums.phpfreaks.com/topic/164490-newbie-question-dealing-with-paths/
Share on other sites

I used what you said and kind of figured out a little more, but am still having issues configuring it to work well when I develop it in XAMPP.

 

I now have a checker in my config file to see what environment it's in:

if ( $_SERVER['SERVER_NAME'] == "localhost" ) {
$root	= "/Applications/XAMPP/......../";	
}
else {
$root	= "http://www..../";
}

 

Then for the header:

 

<link rel="stylesheet" href="<? echo $root; ?>css/style.css" type="text/css" />

 

This works well for the version actually on the web... but I can't seem to get it to work with XAMPP.

 

Any ideas?

 

Thanks for the help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.