Jump to content

Define Server Variable


blsn

Recommended Posts

Hi,

 

I am building a web site and have included files that get used from the root and different sub directories within my site. For example:



<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/css/style.css">


 

These are the outputs of $_SERVER['DOCUMENT_ROOT']:

 

Testing  server (http://localhost/mywebsite):

C:/xampp/htdocs/mywebsite

 

Share host server (http://www.mywebsite.com):

/home/mywebsite/public_html

 

How to define the above path correctly to work both on testing and hosting server?
Link to comment
Share on other sites

$_SERVER['DOCUMENT_ROOT'] is the file system (disk) path to your document root folder. it is NOT used in html links/href statements on a web page. you must use a URL in links/href statements.

 

you can use a domain relative URL - href="/css/style.css" (the leading slash refers to the root of the domain of the current page.)

 

or you can use an absolute URL - href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/css/style.css"

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.