Hi,
I have a little problem with this script, all it's supposed to do is create a session so the person can stay logged in through out the website. But when ever i try to log on with a valid user name it gives the error
Warning: Cannot send session cookie - headers already sent by (output started at c:\easyphp\www\game\includes\header.php:13) in c:\easyphp\www\game\includes\boxes\login.php on line 32
Warning: Cannot send session cache limiter - headers already sent (output started at c:\easyphp\www\game\includes\header.php:13) in c:\easyphp\www\game\includes\boxes\login.php on line 32
<table cellspacing="0" cellpadding="1" width="120">
<tr>
<td id="tablehead" align="center">
<b>Login</b>
</td>
</tr>
<tr>
<td id="border-yes">
<center>
<form action="main.php?id=viewprofile" method="post">
<input type="hidden" name="submit" value="true">
Username: <input type="text" name="Username_log" id="text" size="15"><br>
Password: <input type="password" name="password_log" id="text" size="15"><br>
Remember me: <input type="checkbox" name="rememberlog" id="radio"><br>
<input type="submit" value="login"> <input type="reset" value="Clear">
</form>
<? $db_connect = mysql_pconnect('localhost','root','password');
mysql_select_db(game);
if ($_POST['submit'] == true)
{
$Username = $_POST['Username_log'];
$password = $_POST['password_log'];
$search = mysql_query("select * from game_users where Username='$Username'");
$row = mysql_fetch_array($search);
}
if ($_POST['submit'] == true && $row['password'] == $password )
{
session_start();
$HTTP_SESSION_VARS['valid_user'] = $Username;
}
elseif (!$row['password'] == $password )
{
print "invalid user/password";
}
if (isset($HTTP_SESSION_VARS['valid_user']))
{
echo 'You are logged in as: '.$HTTP_SESSION_VARS['valid_user'].' <br />';
echo '<a href=http://www.htmlforums.com/archive/index.php/"main.php?id=logout">Log out</a><br />';
}
?>
</td>
</tr>
</table>
Any Ideas as to why this is happening?Whenever you have a "headers already sent..." it means that you have HTML code spitting out twice. Hmmmm.... that probably wasn't very clear...
In your code you have to put the session stuff before any html code same as with the Header() function.
So... either you put your PHP in another file or put on top and do
no HTML here - start of page
if ($_POST['submit'] {
cookie......
} else {
<table>....
your form
<input type="submit" value="login" name="submit">
</table>
}
HTHsession_start();
that has to be the very first line right after <? in any file you want to use sessions in.
Karinne is half right. it is spitting out html twice, but what is happening is that the sessions and cookies and header() functions need to work before ANY html goes to the browser and that includes any white spice also. but the white space has to be it a certain spot for it to do it. you cannot even out put <html> or any other tags as that will give you the error.Ok, thanks i can't wait to try it out
I have a little problem with this script, all it's supposed to do is create a session so the person can stay logged in through out the website. But when ever i try to log on with a valid user name it gives the error
Warning: Cannot send session cookie - headers already sent by (output started at c:\easyphp\www\game\includes\header.php:13) in c:\easyphp\www\game\includes\boxes\login.php on line 32
Warning: Cannot send session cache limiter - headers already sent (output started at c:\easyphp\www\game\includes\header.php:13) in c:\easyphp\www\game\includes\boxes\login.php on line 32
<table cellspacing="0" cellpadding="1" width="120">
<tr>
<td id="tablehead" align="center">
<b>Login</b>
</td>
</tr>
<tr>
<td id="border-yes">
<center>
<form action="main.php?id=viewprofile" method="post">
<input type="hidden" name="submit" value="true">
Username: <input type="text" name="Username_log" id="text" size="15"><br>
Password: <input type="password" name="password_log" id="text" size="15"><br>
Remember me: <input type="checkbox" name="rememberlog" id="radio"><br>
<input type="submit" value="login"> <input type="reset" value="Clear">
</form>
<? $db_connect = mysql_pconnect('localhost','root','password');
mysql_select_db(game);
if ($_POST['submit'] == true)
{
$Username = $_POST['Username_log'];
$password = $_POST['password_log'];
$search = mysql_query("select * from game_users where Username='$Username'");
$row = mysql_fetch_array($search);
}
if ($_POST['submit'] == true && $row['password'] == $password )
{
session_start();
$HTTP_SESSION_VARS['valid_user'] = $Username;
}
elseif (!$row['password'] == $password )
{
print "invalid user/password";
}
if (isset($HTTP_SESSION_VARS['valid_user']))
{
echo 'You are logged in as: '.$HTTP_SESSION_VARS['valid_user'].' <br />';
echo '<a href=http://www.htmlforums.com/archive/index.php/"main.php?id=logout">Log out</a><br />';
}
?>
</td>
</tr>
</table>
Any Ideas as to why this is happening?Whenever you have a "headers already sent..." it means that you have HTML code spitting out twice. Hmmmm.... that probably wasn't very clear...
In your code you have to put the session stuff before any html code same as with the Header() function.
So... either you put your PHP in another file or put on top and do
no HTML here - start of page
if ($_POST['submit'] {
cookie......
} else {
<table>....
your form
<input type="submit" value="login" name="submit">
</table>
}
HTHsession_start();
that has to be the very first line right after <? in any file you want to use sessions in.
Karinne is half right. it is spitting out html twice, but what is happening is that the sessions and cookies and header() functions need to work before ANY html goes to the browser and that includes any white spice also. but the white space has to be it a certain spot for it to do it. you cannot even out put <html> or any other tags as that will give you the error.Ok, thanks i can't wait to try it out