Jump to content

Session help plz!


reportingsjr
 Share

Recommended Posts

I have session start() and I get this error:Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/html/login/index.php:14) in /var/www/html/login/index.php on line 36Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /var/www/html/login/index.php:14) in /var/www/html/login/index.php on line 36Im not sure why it does this and I have to have it!!! PLease help!

Link to comment
Share on other sites

Post your code. This typically means that you tried to do something else first, being as that cookies have to be sent first. You are probably getting this because the first line of your code is not session_start().

Link to comment
Share on other sites

You need to start the session before you send any output. Session information goes in a header, and headers are the start of the page, so you can't send output to the browser and then try to send a header, the headers have already been sent.You need to move session_start to the top of the page, or at least before you send any output. You can also enable output buffering, and buffer the entire page and send it once execution is finished.

Link to comment
Share on other sites

Hmm, it still gives me the error, I put it before any of the coding except th <? of course...The coding is:

		<? 	       	session_start();		if(isset($_POST['name']) || isset($_POST['pwd'])){			if(empty($_POST['name'])){				die ("Please enter a username!");			}			if(empty($_POST['pwd'])){				die ("Please enter a password!");			}					$host = "";			$user = "";			$pass = "";			$db   = "";						$connection = mysql_connect($host, $user, $pass) or die ("Sorry, couldnt connect!");						mysql_select_db($db) or die ("Sorry, couldnt select a database!");						$query = "SELECT * FROM `users` WHERE name = '" . $_POST['name'] . "' AND password = '" . $_POST['pwd'] . "'";						$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());			    			if (mysql_num_rows($result) == 1) {        			$_SESSION['login'] = 1;				echo "succesfully logged in!";				if ($row[2] == "yes"){					$session['admin'];				}				if ($_POST['save'] == 1){					setcookie("username", $_POST['name'], time()+(84600*30));					setcookie("pwd", $_POST['pwd'], time()+(84600*30));				}else{					setcookie("username", NULL, mktime() - 3600);					setcookie("pwd", NULL, mktime() - 3600);									}			}else{			echo "Wrong username and password, please go back and try again.";			}			mysql_free_result($result);			mysql_close($connection);		}																		?>

Thanks for the comments

Link to comment
Share on other sites

Make sure you do not have any spaces or blank lines before the <?. Anything outside of the <? (it should really be <?php) will be sent to the browser, including any whitespace like blank lines.

Link to comment
Share on other sites

what do you mean no spaces or anything before it? this is in the middle of the page!ohhh, I put it at the begining before <html> and it worked! ty, but now it says:Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/login/index.php:14) in /var/www/html/login/index.php on line 45Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/login/index.php:14) in /var/www/html/login/index.php on line 46.. All I did was put some variables into two cookies =\Ohh, its the same for cookies to, nvm!

Link to comment
Share on other sites

Haha...that's why. You can't have anything sent to the browser before session_start(). You need to put this in a PHP block at the verryyy beginning of the file.Edit:That means you are trying to send more headers at those locations. You have to send all of the headers at the beginning of the file.

Link to comment
Share on other sites

Yeah, cookies, sessions, all that type of stuff are all contained in headers. There is an HTTP header called set-cookie that specifies a cookie to save on the client. The session itself is actually just another cookie, which contains a session ID number that the PHP engine uses to look up all the info saved in the client's session.

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
 Share

×
×
  • Create New...