views:

5488

answers:

7

Hi, I have an unusual problem, only happening on one server. Following code

....
elseif ($_GET['action']=='login') {

 if (empty($_POST['login_name'])) { $_POST['login_name']=''; }
 if (empty($_POST['login_pass'])) { $_POST['login_pass']=''; }
 if (!empty($_POST['send'])) {
  if (($_POST['login_name']==_ADMIN_NAME) and    ($_POST['login_pass']==_ADMIN_PASS)) {
   //Successfully logged in
   $_SESSION['logged']=1;

// DOES NOT WORK
   header('Location: '.$filename);
   die('Command '.$filename);
  }
 }
// Show Form;
include('plogin.inc.php');
} 
elseif ($_GET['action']=='logout') {
 $_SESSION['logged']=-1;
// DOES WORK!
 header('Location: '.$filename);
}

So the problem is this if i click the link logout, all goes well and i get redirected to $filename. If i submit the login form it checks if the post is correct, set's the session var, but then dies instead of redirecting.

I got output buffers on, all error reporting but (no errors), it doesn't redirect when I post with the form even though it definitely passes (because the session var is set and I get to the die part)

What could be the cause for this behavior? Thanks

+1  A: 

Hi Moak,

might be a simple thing. The header() function does set a header, but does not terminate a script so that the header is sent immidiately after calling header().

You need to make php send the script result to the client. As you use output buffering, this result is held back until you trigger your output. Try this instead:

header('Location: '.$filename);
ob_end_flush();
exit();

Regards, Mario

Mario Mueller
Thanks I tried this, however same result.
Moak
+2  A: 

Did you test that a) the code is reached and b) the error reporting really is in effect?

//Successfully logged in
$_SESSION['logged']=1;
// code reached, error_reporting test echo 'debug: would send location header', $filename, $unsetVariableTriggeringWarning; flush();
if (headers_sent()) { die('cannot send location header (anymore)'); } else { header('Location: '.$filename); die(); }

VolkerK
got this "would send location header Notice: Undefined variable: unsetVariableTriggeringWarning in /var/kunden/webs/***/pbook.inc.php on line 250" BUT did not get the info saying "cannot send location header (anymore)"
Moak
Even with flush()? Is there still some ob_xyz()-buffer active? echo 'ob_level: ', ob_get_level(); can tell. Did you use the echo 'debug: would send location header', $filename, $unset... version? I've edited that in later.
VolkerK
i know the filename is set and correct, as said before the header redirect is exactly the same for login and logout. the only difference is that one is just accessed via link and the other via form submission. i have error_reporting(E_ALL);
Moak
And what about echo 'ob_level: ', ob_get_level();</i> ? I would place it directly before if(headers_Sent()) for testing
VolkerK
+1  A: 

The line with

die('Command '.$filename);

would probably be the culprit. If the die (or exit) functions (or actually language constructs) take a string as a parameter, it is printed out on the other side before halting. PHP doesn't like you trying to output anything if you are sending headers.

If you are using a reasonably new PHP (>= 4.3.0 iirc), you can use integers 0..255 to mark exit conditions (if you want to), which will not be printed.

Henrik Paul
in fact this was just added for debugging, with or without text on exit, it doesn't work.
Moak
+2  A: 

I, also, found redirect don't work sometimes after POST request. It is a browser not server-side problem, I think.

I use something like this:

if( sizeof( $_POST ) == 0 ) header( "Location: " . $url );
else echo '<html><head><meta http-equiv="refresh" content="1;url=' . $url . '"/></head><body>Redirecting to ' . $url . '</body></html>'

In short, if it is a POST request - then I use html refresh redirect, else - normal header redirect.

puzz
hey greate, someone who believes me that it has to do with the POST :) however this is really just happening on one server, not the other. but for now I will use your fix
Moak
A: 

Hi, from my experience you can't send any content when you're doing redirect. So I'd guess your problem is the die() parameter.

I can't find this in documentation right now but try dying() it without a parameter or just use exit; I bet it will work.

jab11
nope,, sorry I removed the parameter, like i told henrik, exit() die() with or without text. The same code works when it's just a link, not if a form was submitted.
Moak
A: 

Hello, I found same problem: Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\php\projects\admin\admin-header.php:12)

arifhossen
in that case you go to E:\xampp\htdocs\php\projects\admin\admin-header.php and look at line 12
Moak
A: 

**Hi this is abhishek PHP Developer for any query Contact :09582452928

abhishek