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
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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.