How to get working website path & directory name?
Hi, in this tutorial we will discuss on current website url and current / working directory name
First you can get the name of the website by using
<?php echo $_SERVER['SERVER_NAME']; ?>
or
<?php echo $_SERVER['HTTP_HOST']; ?>
Now you can get the directory name
<?php echo dirname($_SERVER['PHP_SELF']); ?>
NOTE:
(if you are working on multi folders and you need parent folder you can use 'dirname()' function multi times
Example if you using 2 folders like /manage/user/ and you need parent folder only (i.e manage)
<?php echo dirname(dirname($_SERVER['PHP_SELF'])); ?>
you will get /admin result.
)
Now the complete path
<?php echo "http://".dirname($_SERVER['SERVER_NAME']."".$_SERVER['PHP_SELF']); ?>
Result: http://www.example.com/manage/user
and
complete the path with parent directory name
<?php echo "http://".dirname(dirname($_SERVER['SERVER_NAME']."".$_SERVER['PHP_SELF'])); ?>
Result: http://www.example.com/manage
First you can get the name of the website by using
<?php echo $_SERVER['SERVER_NAME']; ?>
or
<?php echo $_SERVER['HTTP_HOST']; ?>
Now you can get the directory name
<?php echo dirname($_SERVER['PHP_SELF']); ?>
NOTE:
(if you are working on multi folders and you need parent folder you can use 'dirname()' function multi times
Example if you using 2 folders like /manage/user/ and you need parent folder only (i.e manage)
<?php echo dirname(dirname($_SERVER['PHP_SELF'])); ?>
you will get /admin result.
)
Now the complete path
<?php echo "http://".dirname($_SERVER['SERVER_NAME']."".$_SERVER['PHP_SELF']); ?>
Result: http://www.example.com/manage/user
and
complete the path with parent directory name
<?php echo "http://".dirname(dirname($_SERVER['SERVER_NAME']."".$_SERVER['PHP_SELF'])); ?>
Result: http://www.example.com/manage