what is Server variable in php?

What is server variable?

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server

$_SERVER :-

<!DOCTYPE html>
<html>
<body>

<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>

</body>
</html>

How to get server name or domain name.

File name index.php

echo $_SERVER['SERVER_NAME'];
output :-
itechtuto.com

How to get server ip address.

File name index.php

<?php

$localIP = getHostByName(getHostName());

echo "server ip address = " .$localIP;

echo "<br>";
?>
output :-
server ip address = 198.54.114.11

How to get network ip address.

File name index.php

<?php


function get_client_ip_env() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
{
$ipaddress = getenv('HTTP_CLIENT_IP');
echo "Client ip address : ".$ipaddress;
}
else if(getenv('HTTP_X_FORWARDED_FOR'))
{
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
echo "country network ip address (HTTP_X_FORWARDED_FOR) : " .$ipaddress;
}
else if(getenv('HTTP_X_FORWARDED'))
{
$ipaddress = getenv('HTTP_X_FORWARDED');
echo "HTTP_X_FORWARDED : ".$ipaddress;
}
else if(getenv('HTTP_FORWARDED_FOR'))
{
$ipaddress = getenv('HTTP_FORWARDED_FOR');
echo "HTTP_FORWARDED_FOR : ".$ipaddress;
}
else if(getenv('HTTP_FORWARDED'))
{
$ipaddress = getenv('HTTP_FORWARDED');
echo "Remote forwarded address : ".$ipaddress;
}
else if(getenv('REMOTE_ADDR'))
{
$ipaddress = getenv('REMOTE_ADDR');
echo "Host ip address : ".$ipaddress;
}

else
{
$ipaddress = 'UNKNOWN';
}

//return $ipaddress;



}



echo get_client_ip_env();




?>
output :-
country network ip address (HTTP_X_FORWARDED_FOR) : 103.16.29.185

How to get server ip address.

File name index.php

<?php

$localIP = getHostByName(getHostName());

echo "server ip address = " .$localIP;

echo "<br>";
?>
output :-
server ip address = 198.54.114.11

How to get host ip address.

File name index.php

function get_client_ip_env()
{
$ipaddress = '';
if(getenv('REMOTE_ADDR'))
{
$hostip = getenv('REMOTE_ADDR');
echo "Host ip address : ".$hostip;
}
}

echo get_client_ip_env(); output :-
Host ip address : 103.16.29.185

How to get network details and ip, location of user.

File name index.php

<?php


$ipaddress = $_SERVER['REMOTE_ADDR'];
echo "Server ip Address : " .$ipaddress;
function ip_details($IPaddress)
{
$json = file_get_contents("http://ipinfo.io/{$IPaddress}");
$details = json_decode($json);
return $details;
}

$details = ip_details("$ipaddress");

$city = $details->city;
echo $city;
echo "<br>";
$country = $details->country;
echo $country;
echo "<br>";
$organisation = $details->org;
echo $organisation;
echo "<br>";
$hostname = $details->hostname;
echo $hostname;
echo "<br>";

?>





if($country == "IN") { echo "i am indian"; } else if($country == "US") { echo "i am US Server."; } else { echo "unknown country"; } output :-

How to get location of user.

File name index.php

<?php
include("../geo/geoip.inc");
$gi = geoip_open("../geo/GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);

if($country_code == "US")
{
echo "us";
}
else if($country_code == "IN")
{
echo "india";
}
else
{
echo "unkonwn";
}

?> output :-

Details of server.

File name index.php

<?php
$indicesServer = array('PHP_SELF',
'argv',
'argc',
'GATEWAY_INTERFACE',
'SERVER_ADDR',
'SERVER_NAME',
'SERVER_SOFTWARE',
'SERVER_PROTOCOL',
'REQUEST_METHOD',
'REQUEST_TIME',
'REQUEST_TIME_FLOAT',
'QUERY_STRING',
'DOCUMENT_ROOT',
'HTTP_ACCEPT',
'HTTP_ACCEPT_CHARSET',
'HTTP_ACCEPT_ENCODING',
'HTTP_ACCEPT_LANGUAGE',
'HTTP_CONNECTION',
'HTTP_HOST',
'HTTP_REFERER',
'HTTP_USER_AGENT',
'HTTPS',
'REMOTE_ADDR',
'REMOTE_HOST',
'REMOTE_PORT',
'REMOTE_USER',
'REDIRECT_REMOTE_USER',
'SCRIPT_FILENAME',
'SERVER_ADMIN',
'SERVER_PORT',
'SERVER_SIGNATURE',
'PATH_TRANSLATED',
'SCRIPT_NAME',
'REQUEST_URI',
'PHP_AUTH_DIGEST',
'PHP_AUTH_USER',
'PHP_AUTH_PW',
'AUTH_TYPE',
'PATH_INFO',
'ORIG_PATH_INFO') ;

echo '<table cellpadding="10">' ;
foreach ($indicesServer as $arg) {
if (isset($_SERVER[$arg])) {
echo '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>' ;
}
else {
echo '<tr><td>'.$arg.'</td><td>-</td></tr>' ;
}
}
echo '</table>' ;
?>

output :-
PHP_SELF /php/corephp/test.php
argv Array
argc 0
GATEWAY_INTERFACE CGI/1.1
SERVER_ADDR 198.54.115.11
SERVER_NAME itechtuto.com
SERVER_SOFTWARE Apache
SERVER_PROTOCOL HTTP/1.1
REQUEST_METHOD GET
REQUEST_TIME 1461692344
REQUEST_TIME_FLOAT 1461692344.95
QUERY_STRING
DOCUMENT_ROOT /home/gmaxtcne/public_html/itechtuto.com
HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_ACCEPT_CHARSET -
HTTP_ACCEPT_ENCODING gzip, deflate, sdch
HTTP_ACCEPT_LANGUAGE en-US,en;q=0.8,ar;q=0.6,hi;q=0.4
HTTP_CONNECTION -
HTTP_HOST itechtuto.com
HTTP_REFERER -
HTTP_USER_AGENT Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36
HTTPS -
REMOTE_ADDR 103.16.29.185
REMOTE_HOST -
REMOTE_PORT 40624
REMOTE_USER -
REDIRECT_REMOTE_USER -
SCRIPT_FILENAME /home/gmaxtcne/public_html/itechtuto.com/php/corephp/test.php
SERVER_ADMIN webmaster@itechtuto.gmaxlifesciences.com
SERVER_PORT 80
SERVER_SIGNATURE
PATH_TRANSLATED -
SCRIPT_NAME /php/corephp/test.php
REQUEST_URI /php/corephp/test.php
PHP_AUTH_DIGEST -
PHP_AUTH_USER -
PHP_AUTH_PW -
AUTH_TYPE -
PATH_INFO -
ORIG_PATH_INFO -


How to get Full path of the url or Page.

we can use $_server variable for this.

<?php
echo $furl = 'http'.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>

for example : www.itechtuto.com/index.php?var=1&var=2

Here $_SERVER[?HTTP_HOST?] returns www.itechtuto.com

and $_SERVER[?REQUEST_URI?] returns /index.php?var=1&var=2

How to generate seo based url in php.

Example

<?php
function Generate_CleanUrl($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);

// trim
$text = trim($text, '-');

// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}

// lowercase
$text = strtolower($text);

// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);

if (empty($text))
{
return 'n-a';
}

return $text;
}

<?php
function Generate_CleanUrl($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);

// trim
$text = trim($text, '-');

// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}

// lowercase
$text = strtolower($text);

// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);

if (empty($text))
{
return 'n-a';
}

return $text;
}


echo Generate_CleanUrl('http://localhost:8080/test2.php?id=2&hh=4&hg=33');
?>

output :-

Output : http-localhost-8080-test2-php-id-2-hh-4-hg-33





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here