Forum Moderators: coopster

Message Too Old, No Replies

What does it influence predefined $ SERVER array?

         

toplisek

10:30 am on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What does it influence predefined $_SERVER array and how to read this predefined arrays.

penders

10:49 am on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not sure that I completely follow? Many scripts could make use of the $_SERVER array, so changing it could influence any 3rd party scripts you might have - but it won't affect anything outside of your scripts.

<?php echo $_SERVER['DOCUMENT_ROOT']; ?>


<?php 
// Display the entire variable
print_r($_SERVER);
?>

Matthew1980

10:52 am on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there toplisek,

From the manual: $_SERVER [php.net]

A very valuable resource. Hope this is what your referring to.

[EDIT] Sometimes the values in this array are only set if they are in use prime example of this is: $_SERVER['HTTP_X_FORWARDED_FOR']; useful to know if your intending to track IP addresses so that you can inform your users of what IP they are currently logged in from.

And what penders has posted will answer your question as to what is set when you load the page.

Cheers,
MRb

penders

11:14 am on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And... the values contained in this array can vary from server to server (as it's name suggests). Some servers will not even set certain values, or it might be set differently. Something to bare in mind when writing portable code, particularly between Win/Linux, IIS/Apache.

toplisek

11:22 am on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there some example to do useful track of IP addresses so that I can inform my users of what IP they are currently logged in from.

Matthew1980

11:44 am on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there toplisek,


function get_ip(){

$ip_address = array();

if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strpos($_SERVER['HTTP_X_FORWARDED_FOR'],',')){
$ip_address += explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
}
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip_address[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$ip_address[] = $_SERVER['REMOTE_ADDR'];

return $ip_address;
}


That's what I use to get users IP, please note that this will only function IF the $_SERVER['HTTP_X_FORWARDED_FOR'] is actually set.

Cheers,
MRb

toplisek

12:57 pm on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you have some application example how you meant:
..what IP they are currently logged in from?

toplisek

12:19 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think this is better to detect IP:
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}

penders

1:09 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];


It's possible that HTTP_X_FORWARDED_FOR can contain multiple IP addresses, comma separated, depending on whether the user is accessing your site through 1 or more proxy servers.

toplisek

1:44 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You mean all working code is correct:

<?PHP
function getRealIpAddr()
{ $ip_address = array();
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strpos($_SERVER['HTTP_X_FORWARDED_FOR'],','))

//to check ip is pass from proxy
{
$ip_address += explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
}
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){$ip_address[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip_address = $_SERVER['REMOTE_ADDR'];
}
return $ip_address;
}
?>