Home » PHP

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

29 June 2009 563 views Popularity: 2% Share/Bookmark

email

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 :)

Related posts:

  1. PHP: How to get Main or Base URL?
  2. Website statistic (User Information) in PHP
  3. Fun with strings in PHP (Part 1)
  4. Session Handling in PHP
  5. PHP : Read Write Xml with SimpleXML
  6. Magento: How to change Admin URL Path?
  7. jQuery: Grey out background and preview image as popup
  8. How to find php version and php.ini file location path?
  9. Page refresh in PHP
  10. Making a tree navigation menu in PHP