Click to See Complete Forum and Search --> : PHP environment variables not responding


philaweb
09-17-2005, 06:34 AM
Perhaps this is not the right forum since my question also involves Apache server settings, but here goes:

For a long time I have used this fairly simple PHP code to redirect:


<?
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: $url_name" );
?>


I also have this piece of code to detect different environment variables when testing applications:


<html>
<title>User Agent Detector</title>
<body>
<?php
print "User agent: $HTTP_USER_AGENT";
?>
<br />
<?php
print "Language: $HTTP_ACCEPT_LANGUAGE";
?>
<br />
<?php
print "Remote address: $REMOTE_ADDR";
?>
<br />
<?php
print "Referer: $HTTP_REFERER";
?>
</body>
</html>



Both examples works fine on Linux and FreeBSD operated webhosts I use and have used in the past.

This week I set up a MacOS X 10.4.2 server and got everything to work - except the above code. All Apache modules are enabled, still no go. According to my web logs the files are loaded but nothing happens, no redirect just a blank page with complete redirect URL in browser status bar, the second example gives a page with four lines of text, no environment variables though.

Does anyone know what I am missing here?

Is it PHP related or Apache server related?

Scleppel
09-17-2005, 07:18 AM
Is the PHP in in the page source?

If not, your PHP version or PHP settings might mean that you need $_SERVER['HTTP_USER_AGENT'] instead of $HTTP_USER_AGENT etc., although those variables did work for me.

And for you're redirect with HTTP/1.1 you need a absolute URI if you don't alreay have one, or you could do this:

header("Location: http://www.example.com/somewhere_else.php", true, 301);

which does the right headers for the 301 status code and redirect.

philaweb
09-18-2005, 05:49 PM
Is the PHP in in the page source?

Yes. :)

And for you're redirect with HTTP/1.1 you need a absolute URI if you don't alreay have one, or you could do this:

header("Location: http://www.example.com/somewhere_else.php", true, 301);

which does the right headers for the 301 status code and redirect.

As you can see from my code, there is a "$url_name" variable in there.
The page only contains the code I showed, it is called with an URL like this:

http://www.domain.ext/redirect_to/index.php?url_name=http://www.otherdomain.ext/

The several Wordpress API's I've got installed works flawlessly, so either I'm using depreciated PHP in my own and other downloaded scripts, or the server has some requirements I do not know of.

Scleppel
09-19-2005, 09:37 AM
Instead of $url_name you might need to use $_GET['url_name'] if register globals is off. Also always use <?php instead of <?, because some servers don't recognise <? as the php start tag.

This works fine for me: (called using http://127.0.0.1/redirect.php?url_name=http://www.google.co.uk/)

<?php
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $_GET['url_name']);
?>


Did you get the second page working?

philaweb
09-19-2005, 06:35 PM
Woaw... Thanks for your help.

Finally figured out what was causing the problem - register_globals (http://www.slettes.dk/redirect_to/?url_name=http://www.php.net/register_globals).

New PHP versions always come with register_globals turned Off due to security reasons. That was why I could not get the old globals to work.

Did you get the second page working?

Yeah...


<html>
<title>User Agent Detector</title>
<body>
<?php
print('User-Agent: ' . $_SERVER['HTTP_USER_AGENT']);
?>
<br />
<?php
print('Language: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE']);?>
<br />
<?php
print('Remote Address: ' . $_SERVER['REMOTE_ADDR']);?>
<br />
<?php
print('Referer: ' . $_SERVER['HTTP_REFERER']);?>
</body>
</html>