Jump to content

session problem


jimfog

Recommended Posts

take a look at the code below:

?><!DOCTYPE html><html>	 <head>	    <title></title>	    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />		 <link rel="stylesheet"  href="css/admingeneral.css"/>	    <script  type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" ></script>	    <script type="text/javascript" src="js/js-code.js"></script>    </head>	    <?phptry{  	 if(isset($_COOKIE['cookiename']))	    {	    $cookievalue=$_COOKIE['cookiename'];	    redirect($cookievalue);	    }    if(isset($_POST['submit'])&&(!empty($_POST['remember'])))			 { 					 $username = $_POST['username'];					 $passwd = $_POST['password'];					 if ((login($username, $passwd))) {												 setcookie_pers($username);						  header("Location: adminmember.php");					 }				 }	    require 'output_functions_admin.php';

This code above, normally, when run by the browser, it should output the "headers already sent" error and that is becausethe head section is before the setting of the cookie(the setcookie_pers functions sets a cookie). The problem is that sometimes I do not get an error, as I should-other times I get it.The behavior is unstable and I do not understand why. I wonder if there is something in browser behavior I miss here.

Link to comment
Share on other sites

It could be your setting "output buffering" ON which could implicitly start buffering your output and try to flush it at the end of the script or certain amount of bytes.

Link to comment
Share on other sites

You'll get a headers already sent error as long as the header() function is called. Most likely it isn't called sometimes because the conditions of your if() statement aren't met.

Link to comment
Share on other sites

It could be your setting "output buffering" ON which could implicitly start buffering your output and try to flush it at the end of the script or certain amount of bytes.
I do not think it is output buffering because I have commented out...output_buffering = 4096
You'll get a headers already sent error as long as the header() function is called. Most likely it isn't called sometimes because the conditions of your if() statement aren't met.
No...let me explain.We have 2 scenarios here:When a cookie exists a simple redirection takes place. But when there is not cookie, the user must fill the login form and tick if he wants the "remember me " option. In the above case a new cookie is set and here is where I should get the error message.And when that happens the error message which says about headers already sent refers specifically to the head section you see above(it indicates these specific line) and NOT the header function you are mentioning. Besides that...I think it is possible that the error appears on calling header() and that because is called AFTER the cookie is set-the cookie is set by the setcookie_pers function. According to the PHP manual the problem arises if you set the cookie after any output, not before...as is the case with the header function here.
Link to comment
Share on other sites

You should only get the error if the setcookie() or header() function is being called. If you're not getting the error it's probably because they aren't getting called. According to your code, you're only calling those functions when this condition is met:

if(isset($_POST['submit'])&&(!empty($_POST['remember'])))

Link to comment
Share on other sites

Just to clarify in case you're still confused, the only reason that sending a header (setting a cookie also sends a header) after sending output would not result in the error message is if output buffering is enabled. If you can verify in phpinfo or ini_get that you are not using output buffering, and you are not seeing the error message, then you're not sending a header. If output buffering is off and you try to send a header after you send output then you will always get that error. The message might be hidden in HTML markup somewhere, but that situation is always an error. There is no situation where you would have output buffering disabled, you have already sent output, and you successfully send a header without an error. It's simply not possible for that to happen, it's not possible according to HTTP and how PHP works with the server. There is no code you can write to make that possible. Even if PHP sent the header as formatted like a normal HTTP header, the browser would not identify it as a header and would display it as text in the page. Even if it were sent (and it wouldn't get sent, PHP knows better), it still wouldn't technically be a header and wouldn't have the desired effect. It doesn't matter what the flow is through your application, what people click on or whatever. It's just not possible to send output with output buffering disabled, and then send a header. Period.

And when that happens the error message which says about headers already sent refers specifically to the head section you see above(it indicates these specific line) and NOT the header function you are mentioning.
The error message indicates both. It points out where the output is that caused headers to be sent, and where you tried to send a header that caused the error.
Link to comment
Share on other sites

Output buffering is disabled sure.I see this line in php.ini:;output_buffering = 4096So according to the post above I should always get an error header goes before setting the cookie. The important think is that it is OK. probably there was error in the logic...in the conditionals.Thanks.

Edited by jimfog
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...