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

1 comment:

Ellen Paul said...

The Blog you are running is just awesome. The stuff and theme of the blog is so impressive. The actual match, like your blog, is found on very rare places. This blog is so clear and simple, there is no useless content you have added just to show a fake knowledge about the topic. I am new to blogging and picking lot from your blog. Here is my blog if you want to take a look and do suggest me if any problem or something missing.
Thanks a lot!!!