3
        $username;
        $welcomeMessage;
        if( isset( $_SESSION['username'] ) ){
            $username = $_SESSION['username'];
            $welcomeMessage = "Hello $username! | ";
            $welcomeMessage .= '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/nmc/Admin/LogoutProcessor.php">Logout</a>';
        } else {
            $welcomeMessage = "Welcome | ";
            $welcomeMessage .= '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/nmc/Admin/LoginPage.php">Login</a>';
        }

The code above returns "file:///C:/xampp/htdocs/nmc/Admin/LoginPage.php"

I am using xampp to develop a website, basically uses its own server processing, therefore I can't use the above link. I have several webpages in different directories which have to link to the LoginPage.php, and I need a standard link. The above code is in a Class where other pages in different directories can call it.

Can anyone tell me how to resolve this problem?

Thanks!

4
  • 3
    How about removing the $_SERVER['DOCUMENT_ROOT']? Why are you using it in the first place? Commented Nov 15, 2012 at 9:51
  • 3
    Or try using $_SERVER['HTTP_HOST'] instead; that should give you something along the lines of localhost Commented Nov 15, 2012 at 9:51
  • @Carsten Because the above code is in a class to build a webpage. There are many pages from different directories calling this class. Commented Nov 15, 2012 at 9:53
  • 2
    I think a great reason for using $_SERVER["DOCUMENT_ROOT"] is because the live server only works that way, so you need it to be identical for when you upload it to your live server. That's the boat I'm in right now. I have to use $_SERVER["DOCUMENT_ROOT"] for my live server. So I need to make my local server behave in the same way. Commented Jul 15, 2015 at 18:55

5 Answers 5

17

$_SERVER['DOCUMENT_ROOT'] returns

The document root directory under which the current script is executing, as defined in the server's configuration file.

You could use $_SERVER['HTTP_HOST'] or absolute paths like <a href="/nmc/Admin/LoginPage.php">Login</a>

Sign up to request clarification or add additional context in comments.

Comments

8

In such kind of scenarios, its always better to create a config.php file and save it in your root directory. In the config file you define few parameters.

Call this config file in every page. Your config file can be similar to one below.

define('APP_NAME',"beta");  
define('HTTP_SERVER', 'http://localhost/'); 
define('SITE_NAME', 'http://localhost/');   
define('DOCUMENT_ROOT',$_SERVER['DOCUMENT_ROOT'].APP_NAME); 

You can also define your directory for images, css, etc. which you think will be used in multiple places.

So instead of

$welcomeMessage .= '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/nmc/Admin/LogoutProcessor.php">Logout</a>';

you can write,

$welcomeMessage .= '<a href="'.HTTP_SERVER.'/nmc/Admin/LogoutProcessor.php">Logout</a>';

1 Comment

Generally concur, though sometimes (especially in inherited code) you have to use something along these lines to access that config file itself.
7
 $_SERVER['DOCUMENT_ROOT']

returns a physical file system path. It's not an HTTP URL.

Try removing it to use an absolute URL path:

 <a href="/nmc/Admin/LoginPage.php">Login</a>

or try using $_SERVER['HTTP_HOST'] instead.

BTW, read $_SERVER documentation.

Comments

0

If you're using Wamp Server or similar, "/" is the document root of the "www" folder, so you have to write "/yoursitesfoldername/nmc/Admin/LogoutProcessor.php".

Comments

0

I inherited a site that used this everywhere, so when I went to test it with xampp, I ultimately created a virtualhost to load the site.

See this article for details: http://www.dreamincode.net/forums/topic/307265-change-serverdocument-root-path-for-xammp-light/

Here is my httpd-vhosts.conf file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "C:/xampp/htdocs/examplesite/nested/path/to/content"
    ServerName example.localhost
    ErrorLog "logs/example.localhost-error.log"
    CustomLog "logs/example.localhost-access.log" common
</VirtualHost>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.