Setting proper relative paths

Hi

I’ve been doing PHP for a bit but I"m getting into the nitty grit and need to start off by using proper paths so my application will run no matter where I put it.

I’m starting to build my first CRUD and the paths are really throwing me. Locally in MAMP my relative paths work correctly, i.e. if I use …/myfile.php in my functions file that is one level deep in an includes folder, works fine. This also works on a subdomain. But, if I instead access the folder through the main domain and not using the subdomain URI, then all the paths break. At the same time, none of the page assembly breaks, just when I try to load the php files into the content area on clicking nav links.

I’m running a LAMP server with WHM and Cpanel, latest versions of all software are setup.

In this instance, I’m loading separate PHP files into a tag (main content area) in the html. I’m using all PHP files and assembling them, header, page, footer setup. I’m using Javascript and jQuery to trigger a function that looks in my /includes/functions.php and then includes the files based on a variable when the navigation links are clicked.

Here is how that main page is setup:

[php]<?php include ("includes/header.php");?>
<?php include ("includes/sidebar.php");?>

		<!--Page Content-->
		<section class="content" id="content">
			<!-- this is where the external php files load -->
		</section>
<?php include ("includes/footer.php");?>

[/php]

When the page first loads I have a javascript fire off to load in the welcome.php:

[php]// load welcome
window.load = swapContent(‘con1’);[/php]

My navigation is setup in an unordered list with a tags in the li: <li class="navlink"><a href="#" onClick="return false" onmousedown="javascript:swapContent('con1')">my link</a></li>

When clicked, it is using this Javascript and jQuery(latest v2.1):

function swapContent(cv){ $("#content").html("blah").show(); var url = "includes/functions.php"; $.post(url, {contentVar:cv}, function(data){ $("#content").html(data).show(); }); }

Here are some paths that didn’t work.

[php]// Javascript Variable
$contentVar = $_POST[‘contentVar’];

// Load Content

// Load Welcome Screen - Restart
if ($contentVar==“con1”){
include ("…/welcome.php");
}[/php]

this I tried next:

[php]$path = $_SERVER[‘DOCUMENT_ROOT’].’/myFolder’;

// Javascript Variable
$contentVar = $_POST[‘contentVar’];

// Load Content

// Load Welcome Screen - Restart
if ($contentVar==“con1”){
include ("$path/welcome.php");
}[/php]

and then this:

[php]$path = $_SERVER[‘DOCUMENT_ROOT’];

// Javascript Variable
$contentVar = $_POST[‘contentVar’];

// Load Content

// Load Welcome Screen - Restart
if ($contentVar==“con1”){
include ("$path/myFolder/welcome.php");
}
[/php]

I’ve been looking online for 2 days about paths, but the way people are explaining it makes no sense to me. I should mention I’m an artist and not a pragrammer, so maybe my brain isn’t wired right for this.

Please be patient, most of this stuff I’m just piecing together from web searches.

thanks

I don’t think you want to use $_SERVER[‘DOCUMENT_ROOT’], a lot of times that will return the physical path of the files. I’m sure that’s not what you’re looking for. .

If my directory structure is

https://www.unlocktheinbox.com/resources/emailauthentication/

Now on my authentication page, I’m using page routes so picture the above URL to be

https://www.unlocktheinbox.com/resources/emailauthentication.aspx

if I was to provide a link to a different resource page, inside the emailauthentication.aspx page I would do this…

[php][/php]

That would take me to.

[php]https://www.unlocktheinbox.com/resources/identifieralignments.aspx[/php]

If I did

[php][/php]

It will take me here

[php]https://www.unlocktheinbox.com/identifieralignments.aspx[/php]

The “/” takes me to the root, this page does not exist.

If I did.

[php][/php]

It will take me here

[php]https://www.unlocktheinbox.com/resources/identifieralignments.aspx[/php]

If I did
[php][/php]

It will take me here:

[php]https://www.unlocktheinbox.com/resources/resources/identifieralignments.aspx[/php]

Which doesn’t exist

that makes sense. The problem I have is that locally my paths will be different than when uploaded.

When I was looking on other sites, basically everyone was saying to use one of those PHP $_ variables so you can properly set paths when you don’t know what the directory structure will be, i.e. like if you make a web app for a variety of different users and can’t possibly know (I think wordpress uses this).

Thanks for the examples. That’s much clearer now as far as the slash. So how does php handle …/filename.php. Does that go to the public_html folder or the root of the server?

thanks

Oh I forgot to explain that…

[php]
[/php]
will take me here

[php]https://www.unlocktheinbox.com/identifieralignments.aspx[/php]

If I had a page located here:

https://www.unlocktheinbox.com/resources/test/apples/page.aspx

Then an anchor tag with

[php][/php]

It will take me here

[php]https://www.unlocktheinbox.com/resources/identifieralignments.aspx[/php]

Each …/ backs up one directory

That’s what I thought. I’ve been using that method for CSS paths for years, and have been using it for PHP until I hit this snag. I wonder why my code is not working then when I went up 1 level using …/ since it’s a relative path. Maybe it has something to do with the javascript that’s screwing things up and changing the path.

Is there a way I can log the path when the button is clicked with javscript? I tried echoing the variable $contentVar with PHP but it’s blank.

I tried using Firefox’s console to see what the javscript was doing, but when I try and view the console and click the link, Firefox crashes, so must be a bug, or something else is wrong.

I have php error_log being written but no PHP errors are showing up.

Just to refresh, this is the PHP I have in my functions file. The welcome.php is one level up from where the functions file is located. Works if all my files are loaded into root directory of a subdomain, but fails if accessed in a folder 1 level from primary domain root.

[php]// Load Welcome Screen - Restart
if ($contentVar==“con1”){

// Load Content

// Load Welcome Screen - Restart
include ("…/welcome.php");
}[/php]

You can just use alert boxes

alert(document.URL);
alert(document.baseURI);
alert(window.location);
alert(document.location);

thanks. I never used that before. I’ll look into it.

I only did a quick read of your post but I believe this is will help you. The echo’s are just there for you to see the output. You would remove them. You need to view the output on linux to see the differences.

[php]echo $path = realpath(dirname(FILE)) . DIRECTORY_SEPARATOR;
require_once($path . “config/database.php”);
require_once($path . “config/functions.php”);

$error_log_filename = ‘myerror.log’;

// Server path to this directory used for error log.
echo $realpath = realpath(’.’);

$log_directory=“logs”;
echo $errorlog_path = “$realpath”.DIRECTORY_SEPARATOR."$log_directory".DIRECTORY_SEPARATOR."$error_log_filename"; # Path To Error Log & log filename
[/php]

Run this on linux:
[php]<?php
echo $path = realpath(dirname(FILE));
echo “
” ;
echo $realpath = realpath(’.’);
?>[/php]

Thanks Kevin.

Got held up on some work the past few days but will be trying this stuff out (hopefully) by tomorrow. Will post the results after I test.

Kevin,

I ran those bits of code with realpath.

When I run the first chunk of code from the app folder (one level deep from site root) I get this:

/home/blah/public_html/appFolder

When I run it from includes (2 levels deep from site root) I get this (including the trailing slash on the end):

/home/blah/public_html/appFolder/includes/

If I run the second chunk you sent in the the app folder (one level deep from the site root), it outputs the same for both.

/home/blah/public_html/appFolder /home/blah/public_html/appFolder

If I run it in the app includes folder (2 levels deep) it outputs first level folder for $path and the includes folder for $realpath.

/home/blah/public_html/appFolder /home/blah/public_html/appFolder/includes

keep in mind I have this folder setup as a subdomain (i.e. blah.mydomain.com) but I’m accessing it now as [code]http://mydomain.com/blah/testPaths.php

and

http://mydomain.com/blah/includes/testPaths.php[/code]

Not sure if that will affect the results. Maybe you can explain what the hell this means and which I should use. I’ll be back after some much needed sleep.

thanks

You dont run it from all over the place. It goes in a config file in the root directory of your application. You then use it wherever by including the config file in your other pages.

In your example, it goes in /home/blah/public_html/appFolder
Which is the same as http://mydomain.com/blah/

I was just testing it to see what paths it produced.

So I would create a config.php with it and then include/require that in my functions file, or is it best to include it at the top of individual pages as an include, or just paste it at the top of my functions file?

Thanks a lot for this. Now I can use $path/to/myfile.php and it will actually work.

The config.php is your homebase for everything. So that is where you include your other stuff such as functions.php or database.php

In your root directory, config.php with the following at the top…

$path = realpath(dirname(FILE)) . DIRECTORY_SEPARATOR;
require_once($path . “config/database.php”);
require_once($path . “config/functions.php”);

From there, depending on how you set things up, you would then include the config.php in each file…IF you are doing individual files for your pages. I would highly recommend you take a look at the structure of my PDO bumpstart database in my signature. All files are called from index.php, so there is no need to include the config in every page. This is a much cleaner setup and brings you a lot closer to an MVC pattern.

With my download example you could also create a templates folder so you can separate your html from your code. You would just include the nearly pure html into your include pages.

I’ll check it out. This is exactly what I need to start learning.

When I build simple sites I typically use a functions file for everything because I’m not doing anything advanced, but this makes a lot of sense.

Since I’ve been working on front-end only sites that are style-driven, I would just make a separate php file for each page, and then include the header and footer, and I called the functions file at the top of the header file. Seemed to work fine and pages load really fast. Seems to be not the best for an application though.

much thanks for all the help guys. I will check the files and read up on MVC.

I would just make a separate php file for each page
I would just make a separate php file for each page, and then include the header and footer, and I called the functions file at the top of the header file.

In my early unlearned days, I did the same thing. There are better ways. Best to learn it from the beginning.

Will do, man. Thanks again for the help.

Sponsor our Newsletter | Privacy Policy | Terms of Service