I'm having trouble setting cookies. I've searched these forums to try to find the solution but I haven't been able to figure out where I'm going wrong.
I have created a login script and I want to assign a cookie to users who login correctly. Every time I test it out and sign in with a correct username/password, I get an error indicating that headers were sent out before the cookie.
I realize that cookies have to be sent out before headers but I'm still not sure where I'm going wrong. This is the first time I've tried to code cookies so the error is probably something obvious.
Here's my code (I get the same error with or without line 2). If anyone can see where the error is please let me know. Thanks.
<?php
setcookie("LoggedIn", "false");
require_once("connect.php");
?>
<html>
<head>
<title>HTML Form</title>
</head>
<body>
<table width="425" align="center" border="1" bordercolor="black" cellspacing="0" cellpadding="4">
<form name="login" method="post" action="login.php">
Name: <input type="text" name="Name"><br>
Password: <input type="password" name="Password"><br>
<input type="submit" value="login">
<br>
<input type="hidden" name="submit" value="true">
</form>
<?php
$TableName = "z_forum_users";
if($_POST['submit']==true) // This means the form HAS been submitted:
{
if($Name!='' and $Password!='')
{
$check=mysql("forum", "select * from z_forum_users where z_users_Name='$Name' and z_users_Password='$Password'");
$count=mysql_numrows($check); // check the rows returned.
if($count==1) // if the number of rows returned = 1 then do this
{
setcookie("LoggedIn", "true");
$logged = $true;
echo "<br><a href='form.php'>Go back</a>";
}
else
{
echo "You did not enter a valid username";
}
}
}
mysql_close($connect);
?>
</table>
</body>
</html>