Absolute paths within includes with localhost

Go To StackoverFlow.com

2

all.

My question is in regards to a problem I'm encountering when trying to add a universal php template for my DOCTYPE section. My DOCTYPE include (aptly entitled doctype.php) lies within the /template directory, and also includes calls pointing to my CSS and JS files that I want to be accessible to all pages.

So the problem is encountered when I try to write the absolute path to these files (the CSS and JS files). Currently, I am trying:

<script type="text/javascript" src="<?php echo $_SERVER['DOCUMENT_ROOT'] . '/file/extension/to/javascript/file.js'; ?>"></script>

and

<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER['DOCUMENT_ROOT'] . 'file/extension/to/css/file.css'; ?>"

I am running the application through WAMP on my localhost. Taking a look a look at the source code, it appears as though the links are pointing to the appropriate files (c:/wamp/www/examplesite/path/to/file/file.ext), and all should be well. BUT it is not...

The JavaScript is not accessible and the Stylesheet is not functioning. I'm at a loss for what to do.

I've also tried: -writing the absolute path without the use of PHP -creating PHP variables to hold the document root and then concatenating the appropriate directory path to access the files.

Any suggestions? And how will this change when I upload the directory structure to my online server vs. my current localhost?

2012-04-03 22:39
by asgaines
directory access problems it seems. If I am wrong, and someone answers, do tag me in. : - hjpotter92 2012-04-03 22:41
Have a look at the network panel (in chrome: ctrl+shift+i, click Network tab, refresh you webpage). The HTTP request that gets the javascript returns an error 404 or something else? If it's a 404, then the path for the js is incorrect, but it could be a problem related to the rights to acces the file. Also, you can see the path the browser uses to request the js - Benjamin Crouzier 2012-04-03 23:00


4

You might want to try $_SERVER['HTTP_REFERER'] instead. It gives you the path you are looking for.

On my local machine, which uses WAMP, I used <?php print_r($_SERVER); ?> to see what values it gives.

Also, there may be some typos in the snippets. For example, you don't need the leading / in the first example you gave.

For example:

<script type="text/javascript" src="<?php echo $_SERVER['HTTP_REFERER'] . 'file/extension/to/javascript/file.js'; ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER['HTTP_REFERER'] . 'file/extension/to/css/file.css'; ?>"></link>

Or since HTTP_REFERER can't be trusted in some case, you may want to create a function that builds the base part of the absolute path.

<?php
function getAddress() {
    $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
    $filenamepattern = '/'.basename(__FILE__).'/';
    return $protocol.'://'.$_SERVER['HTTP_HOST'].preg_replace($filenamepattern,"",$_SERVER['REQUEST_URI']);
 } 
?>

Then call it like so:

<script type="text/javascript" src="<?php echo getAddress() . 'file/extension/to/javascript/file.js'; ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo getAddress() . 'file/extension/to/css/file.css'; ?>"></link>
2012-04-03 23:11
by Justin Nafe


2

Of course any kind of resource (JS, CSS, Images, etc.) has to be accessed via http(s) request and therefore it is impossible to directly access them with an absolute path. Think of the security implications of such an approach. So you always have to use web paths relative to your web root directory.

For example:

<script type="text/javascript" src="http://localhost/myproject/media/ja/file.js"></script>
2012-04-03 23:10
by markus


1

You're pointing to files on your local machine using the file path (ex: C:\some\path\to\file) when you should be using a URL (ex: http://localhost/some/path/to/file). The HTML is parsed by the client's browser so when it attempts to access a path that isn't a URL it can't.

Instead of using $_SERVER['DOCUMENT_ROOT'] you can either use absolute URLs such as

<script type="text/javascript" src="/path/to/my/file.js"></script>

where the "/path" folder is your base web directory, or you can use relative URLs based on whatever the current directory the file is in to which you're including the doctype.php file. I'd recommend not doing the latter as it's a pain in the butt to keep track of.

If you use relative URLs you should have no problems when moving your code to a new host, provided the directory structure remains the same.

2012-04-03 23:11
by TheOx
Also practice the use of dirname(__FILE__) for relative paths - Panagiotis 2012-04-03 23:15
@Panagiotis you should mention that only works assuming assets are stored at the same level or farther down the accessible file system tree. Like a front-controller pattern - Xeoncross 2012-04-03 23:20
Right! +1 for mentioning it - Panagiotis 2012-04-03 23:22
This is a pain in the @$$! I keep getting error messages like this one: Warning: include(http://localhost/interpopulusrevised/templates/doctype.php) [function.include]: failed to open stream: no suitable wrapper could be found in C:\wamp\www\interpopulusrevised\index.ph - asgaines 2012-04-04 06:55
PHP files should be included using the actual file system path (i.e include "c:\path\to\file.php"). Javascript and CSS files should be using URLs - TheOx 2012-04-04 12:47
AHHH! @TheOx, thank you very much, that cleared up much of my confusion! It seems to be working no - asgaines 2012-04-04 17:23
@user1311538 - glad to hear it - please be sure to mark this as solved by clicking the check mark next to whichever answer best answered your question - TheOx 2012-04-04 21:49


0

You're going about the wrong way of including files in your page, you should be using HTTP URLS instead:

<script type="text/javascript" src="/file/extension/to/javascript/file.js"></script>

Or, if you prefer to use a variable with the host name:

<script type="text/javascript" src="http://<?php echo $_SERVER['HTTP_HOST']; ?>/file/extension/to/javascript/file.js"></script>
2012-04-03 23:13
by Pete
Ads