How to get working site path and directory name in php?
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:
- PHP: How to get Main or Base URL?
- Website statistic (User Information) in PHP
- Fun with strings in PHP (Part 1)
- Session Handling in PHP
- PHP : Read Write Xml with SimpleXML
- Magento: How to change Admin URL Path?
- jQuery: Grey out background and preview image as popup
- How to find php version and php.ini file location path?
- Page refresh in PHP
- Making a tree navigation menu in PHP
