How to find current URL using php?
Sometimes we need to find current URL for processing on script. URL is construct by three component :
- Domain name : www.domain.com
- Script name : index.php
- 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’];