Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTTP Variables

Status
Not open for further replies.

tobyheywood

IS-IT--Management
Joined
Apr 20, 2001
Messages
122
Location
GB
Can anyone point out why I'm not getting the HTTP variables displayed on the page whilst using this script.

I think it may be somehing to do with the way that the server is setup! Are there differences between Apache 1 and Apache 2 which would change the way you get HTTP variables?

Anyway here is the code...

<?php
$envs = array(&quot;HTTP_REFERER&quot;, &quot;HTTP_USER_AGENT&quot;, &quot;REMOTE_ADDR&quot;, &quot;REMOTE_HOST&quot;, &quot;QUERY_STRING&quot;, &quot;PATH_INFO&quot;);
foreach($envs as $env)
print &quot;$env: $GLOBALS[$env]<br>&quot;;
?>

Thanks in advance.

Regards

Toby Heywood
 
Toby,

Try first to print all global variables using the print_r command. That will show you everything that's there. You can go from there.
Code:
echo(&quot;<pre>&quot;);
print_r($_GLOBAL);
echo(&quot;</pre>&quot;);
 
One caution about using print_r() with $GLOBALS...

$GLOBALS contains all variables in PHP, including itself. If you are running a version of PHP prior to 4.0.4, using print_r() on $GLOBAL will create an infinite loop.



If your version of PHP is 4.1.0 or newer, the values you want will be in the array $_SERVER. With older versions the values will be in $_HTTP_SERVER_VARS.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I have managed to work out what was causing the problem.

In order for global variables to be accessible you need to have this line in your php.ini

register_globals = on

A swift reboot and I was back on my way! Thanks again for your prompt replies.

Regards



Toby Heywood
 
Toby

I can only assert the recommendation given by sleipnir214. Besides the security concerns, which are eminent, there is a further advantage you'll be able to reap:

Code written with explicit addressing of the superglobal arrays ($_POST etc.) is portable regardless of the setting of register_globals. This is true as long as the PHP version is 4.1.0 or later. This consideration is a huge advantage. You'll never have to rewrite your code if it migrates to a different server.
 
don't worry, I had already taken into consideration the security aspect and had decided against using the register_globals = on, line in the php.ini.

Instead after some thought I worked out that I could use the following which would give me what I wanted (can you tell I'm still getting to grips with PHP??? :).

<?php
$envs = array(&quot;HTTP_REFERER&quot;, &quot;HTTP_USER_AGENT&quot;, &quot;REMOTE_ADDR&quot;, &quot;REMOTE_HOST&quot;, &quot;QUERY_STRING&quot;, &quot;PATH_INFO&quot;);
foreach($envs as $env)
print &quot;$env: $HTTP_SERVER_VARS[$env]
?>

Thanks for all your guidance.

Regards

Toby Heywood
 
If your version of PHP is newer than 4.1.0, I recommend you use $_SERVER in lieu of $HTTP_SERVER_VARS.

$_SERVER is a superglobal array, and as such is available in all variable scopes. $_HTTP_SERVER_VARS is not -- inside a function, for example, you will have to use the &quot;global&quot; operator to make $HTTP_SERVER_VARS.

This does, however, restrict your code to PHP versions 4.1.0 and newer. The superglobals were only introduced then.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top