Jump to content

Function For Using Absolute Path?


glassfish
 Share

Recommended Posts

For the includes I use the following:

 include($_SERVER['DOCUMENT_ROOT'] . "/cms/includes/connection.php");

And for these following examples I use "http://localhost/":

<img class="header_image" src="http://localhost/cms/assets/image/capture.png" />
<script type="text/javascript" src="http://localhost/cms/jquery/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="http://localhost/cms/assets/style.css" />

With these examples I use "http://localhost/" because "$_SERVER['DOCUMENT_ROOT']" is not working with these above.

 

My Question:

Is there a function I could be using and replace it with "http://localhost/"?

 

I am looking to have it like following: When I upload the web app onto the web server it still should be working out.

 

 

EDIT:

 

"Un-linking" those links which happen did not work.

Edited by glassfish
Link to comment
Share on other sites

cyberRobot,

 

The problem I have had with that is that I may have to use "../../" to go "up" with the directories.

 

And "for some reason" I have had issues with this, when for example including the stylesheet file in the header file and the header file gets included in a sub-directory.

Edited by glassfish
Link to comment
Share on other sites

cyberRobot,

 

The problem I have had with that is that I may have to use "../../" to go "up" with the directories.

 

I'm not sure what you mean. Could you provide an example?

 

Root-relative links always start in the website's root folder. So I'm not seeing why you would need to use syntax associated with document-relative links.

 

Are you talking about the include() function from the original post? If so, the include() function should still use the $_SERVER['DOCUMENT_ROOT'] variable. To reference a file outside (or above) the root, you can use "..". For example:

include($_SERVER['DOCUMENT_ROOT'] . "/../file-outside-root.php");
Link to comment
Share on other sites

include($_SERVER['DOCUMENT_ROOT'] . "/cms/includes/header.php");

This gets inlucded in a sub directory for example than the stylesheets will not load anymore, when I have used "/" or also "../", that is why I have used "http://localhost/" to load the styelsheets, with that it works then.

 

This was what I actually meant.

 

Though, I have had just checked it and using the following indeed works for myself now:

<link rel="stylesheet" type="text/css" href="/cms/assets/style.css" />

With the "forward slash" at the beginning.

 

So, basically always starting with the "project name" in the "href" could be working then.

Link to comment
Share on other sites

Unless you start adding items in a sub directory.

 

You can also do something like this

$server_host = "http://".$_SERVER['HTTP_HOST'];
echo "<script type='text/javascript' src='".$server_host."/cms/jquery/jquery.js'></script>";
<script type="text/javascript" src="//<?php echo $_SERVER['HTTP_HOST'];?>/cms/jquery/jquery.js"></script>
Link to comment
Share on other sites

With these examples I use "http://localhost/" because "$_SERVER['DOCUMENT_ROOT']" is not working with these above.

 

 

the reason $_SERVER['DOCUMENT_ROOT'] doesn't work for things the browser requests (images, and external css/javascrpt files) is because $_SERVER['DOCUMENT_ROOT'] contains file system path information on your server. it has no meaning to the browser.

 

there are two different things going on -

 

1) include expects a file system path and a filename, because php is reading and including the file through the file system on your server. include can use a url, but this is not common, since it requires some settings to be enabled and including a .php file through a url doesn't include the actual php code, it includes the output from the .php file, the same as if you browsed to the file being included.

 

2) the <img > <script > and <link > tags you put on your web page contain URLs to the images, javascript, and css files. it's the browser that requests these and the browser needs to know the URL to use.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

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