Page 1 of 1

[solved w/session] Trouble passing variable on redirect

Posted: Sun Mar 06, 2005 12:18 pm
by stk
Hello, this is my first post. I am new to php (and this board) ... so if I'm not doing something the best/recommended way, let me know. :wink:

I'm trying to pass a variable (actually several environmental variables, but just testing with one that's already been defined).

I've checked my host's version of php (via phpinfo() - 4.3.10) and 'register_globals' is ON for both the local & master.

My redirect code and variable definition looks like:

Code: Select all

<?php 
function inAgent($agent) &#123; 
global $HTTP_USER_AGENT; 
$notAgent = strpos($HTTP_USER_AGENT,$agent) === false; 
return !$notAgent; &#125; 
if ( inAgent('MSIE') ) &#123; $browser = 'ie'; &#125; ?>
<?php if ($browser == 'ie'): ?>
<?php header("Location: http://mysite.com/_errors/404ie.php"); ?>
<?php else: ?>
<html>
As you can prolly gather, I'm identifying anything "Bill" and redirecting to another custom 404 error file. I'd like to pass a bunch of environmental variables across to the 404ie.php file, but I can't even seem to get the $browser variable.

I've tried a bunch of things:

Code: Select all

<?php echo $browser;
printf ('$browser');
echo $_GET&#1111;browser];
I even created a start_session(), defined "$_SESSION['browser']='ie';" and tried getting it with an "echo $_SESSION['browser'];" in the 404ie.php file.

No errors, but none result in an output of "ie" to the screen.

Are the variables being passed? How do I fetch them in the 404ie.php file?

TIA

Posted: Sun Mar 06, 2005 12:32 pm
by feyd
using 'global' with a super global isn't needed that I remember. Try just using $_SERVER['HTTP_USER_AGENT'] instead.

session_start() is required in all pages that need to access session data.

Posted: Sun Mar 06, 2005 1:01 pm
by stk
feyd, Thanks for your quick response.

Honestly, I swiped the browser detect code (modified it for MSIE-only). It works and my problem lies with the variable. (Read: I'm afraid to monkey with it for fear of introducing a 2ND problem! :wink: ) Maybe you can show me what a cleaner code looks like? (I'd learn a lot from that).

Thanks for the heads up on the session_start() requirement. (I just assumed when it's started it carries through to all pages, but I read now that it does not) Is sessions the only way to pass variables across pages?

I modified the redirect code to look like:

Code: Select all

<?php 
function inAgent($agent) &#123; 
global $HTTP_USER_AGENT; 
$notAgent = strpos($HTTP_USER_AGENT,$agent) === false; 
return !$notAgent; &#125; 
if ( inAgent('MSIE') ) &#123; 
session_start();
$_SESSION&#1111;'browser'] = 'ie'; &#125; ?>
<?php if ($_SESSION&#1111;'browser'] == 'ie'): ?>
<?php header("Location: http://mysite.com/_errors/404ie.php"); ?>
<?php else: ?>
and it's redirecting properly. No worries there.

I'm still not getting the variable, however. I added the following to the 404ie.php file:

Code: Select all

<?php session_start();
echo $_SESSION&#1111;'browser']; ?>
I get an error message:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at
What am I doing incorrectly? (Do I need session_start() at the top?)

EDIT: Aha ... that did it. I put

Code: Select all

<?php session_start();
$browser=$_SESSION&#1111;'browser']; ?>
at the top of the file and then

Code: Select all

<?php echo $browser; ?>
further down.

Thanks for your help! (Still could use a few pointers on a more succint way to write that browser test->redirect, if you feel up to it) :)

Posted: Sun Mar 06, 2005 1:40 pm
by feyd
there are many ways to detect the browser and junk.. yours is perfectly fine.. although I prefer passing in any variables I need for my functions, or at least being able to specify when to use outside ones, versus the "default" ones.