By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

Parsing $_SERVER array problem

P: n/a
CPD
I can't parse $_SERVER when using the alphabetic key instead of the numeric
index of the array. Below is some of the line-numbered code, followed by
the corresponding output. For this posting, IP address and domains shown
are fictitious. Note how the REMOTE_HOST is resolved by gethostbyaddr()
using a hard-coded IP address or the array index "10", but it fails
completely when the literal key REMOTE_ADDR is used.

Any help with this will be greatly appreciated. Thanks!

Here is the numbered PHP code fragment:

1 echo "REMOTE_ADDR direct : ". $_SERVER['REMOTE_ADDR'] . "<br>\n";
2 echo "REMOTE_HOST by var name: ". gethostbyaddr($_SERVER['REMOTE_ADDR']) .
"<br>\n";
3 echo "REMOTE_HOST hard-coded : ". gethostbyaddr('20.1.22.40') . "<br>\n";
4 echo "REMOTE_HOST by index 9 : ". gethostbyaddr($_SERVER[10]) . "<br>\n";
5 while ( list( $key, $val ) = each( $_SERVER ) ) { echo " $key => $val
<br>\n"; }

Here is the corresponding output, with notes in square brackets:

1 REMOTE_ADDR direct : [note REMOTE_ADDR was not resolved here]
2 Warning: gethostbyaddr(): Address is not a valid IPv4 or IPv6 address in
env.php on line 2
REMOTE_HOST by var name: [again REMOTE_ADDR was not resolved here]
3 REMOTE_HOST hard-coded : c-20-1-22-40.client.comcast.net
4 REMOTE_HOST by index 9 : c-20-1-22-40.client.comcast.net
5 [ below is output from while loop]
0 =>
1 => */*
2 => /bin:/sbin:/usr/bin:/usr/sbin
3 => /env.php
4 => /env.php
5 => /env.php
6 => /usr/local/psa/home/vhosts/gldomain.com/httpdocs
7 => /usr/local/psa/home/vhosts/gdomain.com/httpdocs/env.php
8 => /usr/local/psa/home/vhosts/gldomain.com/httpdocs/env.php
9 => 204.146.241.180
10 => 20.1.22.40
11 => 80
12 => 1353
13 => Apache/1.3.27 Server at www.gldomain.com Port 80
14 => Apache/1.3.27 (Unix) mod_python/2.7.8 Python/2.2.2
mod_webapp/1.2.0-dev mod_perl/1.27 mod_throttle/3.1.2 PHP/4.3.2
FrontPage/5.0.2.2510 mod_ssl/2.8.14 OpenSSL/0.9.7c
15 => CGI/1.1
16 => GET
17 => HTTP/1.1
18 => Keep-Alive
19 => Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)
20 => QaQZes2T974AAKlnBWg
21 => cs@gldomain.com
22 => 0
23 => en-us
24 => gzip, deflate
25 => www.gldomain.com
26 => www.gldomain.com
27 => Array
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
CPD wrote:
I can't parse $_SERVER when using the alphabetic key instead of the numeric
index of the array.
I find this very strange!

My $_SERVER array (and every other $_SERVER array anywhere I've seen)
does not have numeric indexes.

1 echo "REMOTE_ADDR direct : ". $_SERVER['REMOTE_ADDR'] . "<br>\n";
<snip>
Here is the corresponding output, with notes in square brackets:

1 REMOTE_ADDR direct : [note REMOTE_ADDR was not resolved here]


<snip>

What are the real contents of $_SERVER['REMOTE_ADDR']?
I'm pretty sure it should be an IP address, not a [resolved] host name.

--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #2

P: n/a
In article <aeVod.384689$wV.23482@attbi_s54>, an*******@nospam.pls
says...
I can't parse $_SERVER when using the alphabetic key instead of the numeric
index of the array. Below is some of the line-numbered code, followed by
the corresponding output. For this posting, IP address and domains shown
are fictitious. Note how the REMOTE_HOST is resolved by gethostbyaddr()
using a hard-coded IP address or the array index "10", but it fails
completely when the literal key REMOTE_ADDR is used.

Any help with this will be greatly appreciated. Thanks!


You will probably have better success inserting phpinfo() in your code
and checking to see what the $_SERVER environment have defined as names.
--
AJ
Treehouse Networks abuse@
www.treenetnz.com
Jul 17 '05 #3

P: n/a
CPD
REMOTE_ADDR does contain a numeric IP address, and I did not expect it to
have a host name. What I meant by 'resolve' was actually getting the IP
address value using the construct $_SERVER['REMOTE_ADDR']. I just found out
that I am able to get the IP address via the old superglobal
$HTTP_SERVER_VARS['REMOTE_ADDR'], but not via the newer
$_SERVER['REMOTE_ADDR'].

phpinfo() does expose all the values in each of the superglobals.

Is there some PHP setting required to use $_SERVER?
"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
CPD wrote:
I can't parse $_SERVER when using the alphabetic key instead of the numeric index of the array.
I find this very strange!

My $_SERVER array (and every other $_SERVER array anywhere I've seen)
does not have numeric indexes.

1 echo "REMOTE_ADDR direct : ". $_SERVER['REMOTE_ADDR'] . "<br>\n";


<snip>
Here is the corresponding output, with notes in square brackets:

1 REMOTE_ADDR direct : [note REMOTE_ADDR was not resolved here]


<snip>

What are the real contents of $_SERVER['REMOTE_ADDR']?
I'm pretty sure it should be an IP address, not a [resolved] host name.

--
Mail sent to my "From:" address is publicly readable at

http://www.dodgeit.com/ == ** ## !! !! ## ** == TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may bypass the spam filter. I will answer all pertinent mails from a valid

address.
Jul 17 '05 #4

P: n/a
[ Please don't toppost. ]
[ See http://www.greenend.org.uk/rjk/2000/06/14/quoting.html ]

CPD top-posted:
I just found out
that I am able to get the IP address via the old superglobal
$HTTP_SERVER_VARS['REMOTE_ADDR'], but not via the newer
$_SERVER['REMOTE_ADDR'].

phpinfo() does expose all the values in each of the superglobals.
This is getting stranger and stranger.

Did you try the phpinfo() in the same script that is giving you trouble,
right after the troublesome line?
Is there some PHP setting required to use $_SERVER?


I don't think so.

What's your PHP version?
How are you using it (as a module or as CGI)?
--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.