Thursday, August 30, 2012

How to get full URL in PHP


To get full url of running script, we use the following two functions in PHP

  • $_SERVER['HTTP_HOST']
  • $_SERVER['PHP_SELF']

Examples
Consider my url string is : http://mysite.com/products/booking.php

<?php echo $_SERVER['HTTP_HOST']; ?>

Output : mysite.com

echo  $_SERVER['REQUEST_URI'];

Output : /products/booking.php

<?php echo 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);?>

Output : http://mysite.com/products

No comments: