Home » PHP

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

29 June 2009 586 views 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. PHP: How to get Main or Base URL?
  2. Website statistic (User Information) in PHP
  3. How to find php version and php.ini file location path?
  4. Fun with strings in PHP (Part 1)
  5. Magento: How to change Admin URL Path?
  6. Session Handling in PHP
  7. WordPress MU: Admin login problem
  8. WAMP XAMPP: Localhost server not working
  9. WordPress: Archive link not working
  10. WordPress Fix: open_basedir restriction in effect. File(/tmp) is not within the allowed path(s)