Thursday, September 10, 2009

How to get full url path in php by using server parameter

From $_server variable has several parameter related to get the url information in different format

Getting the url information from the $server depends on in which way you want to display information like getting only server host or server request url ,server port etc…..

Here iam giving some information about getting url information in different ways in different cases.

-To get server host name

.echo $_SERVER['HTTP_HOST'];

-To get server name

echo $_SERVER['SERVER_NAME'];

-To get server port

echo $_SERVER["SERVER_PORT"];

By using the above predefined variables we can able to get the full u rl of the present page in which ur working

Here is example to get the full url in php

/**
*
* @to get the full url of page on which ur working
*
*/
function getUrlAddress()
{
/*** check for https is on or not ***/
$url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
/*** return the full address ***/
return $ url .'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}

/*** example usage ***/
echo getUrlAddress ();

No comments: