Home » PHP, Tips and Tricks

How to get working site path and directory name in php?

29 June 2009 146 views No Comment Popularity: 2% Share/Bookmark

Let the page path be: http://localhost/test/admin/index.php

Get the server name/site name of your website:

echo $_SERVER['SERVER_NAME'];

or,

echo $_SERVER['HTTP_HOST'];

This will print: localhost

Get the path of the page you are working on:

echo $_SERVER['PHP_SELF'];

This will print: /test/admin/index.php

Get only the directory name of the page you are working on, i.e. if you don’t want the file name to be displayed:

echo dirname($_SERVER['PHP_SELF']);

This will print: /test/admin

If you want to omit the ‘admin’ directory as well then you can use dirname() function two times:

echo dirname(dirname($_SERVER['PHP_SELF']));

This will print: /test

Finally:

echo “http://”.dirname($_SERVER['SERVER_NAME'].”".$_SERVER['PHP_SELF']);

This will print: http://localhost/test/admin

Enjoy PHPing :)

From Mukesh Chapagain's Blog | Post How to get working site path and directory name in php?

Related posts:

  1. Website statistic (User Information) in PHP
  2. How to find php version and php.ini file location path?
  3. Fun with strings in PHP (Part 1)
  4. Wordpress MU: Admin login problem
  5. Session Handling in PHP

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.