Free Android CakePHP GMaps Articles by Bali Web Design

April 13, 2008

How to find current URL using php?

Filed under: php,php-tips-basic — Tags: — admin @ 9:56 am

Sometimes we need to find current URL for processing on script. URL is construct by three component :

  1. Domain name : www.domain.com
  2. Script name : index.php
  3. Query string : parameter on url, example component=member&task=edit

Using $_SERVER variable we can get this all information and construct it to get full URL. We need to retrieve value ‘HTTP_HOST’, ‘SCRIPT_NAME’ and ‘QUERY_STRING’ on $_SERVER array. Here is the code :

$url = “http://” . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘SCRIPT_NAME’] . “?” . $_SERVER[‘QUERY_STRING’];

this script produce complete current url, if you don’t need host to be included you can just use values from $_SERVER[‘REQUEST_URI’];