Hi guys,
I'm new to the forum today, I'm currently developing a PHP based website which will allow logins, profiles, and a search of all profiles etc...
Each person should be able to login and edit their profile securely. Therefore I need a login system.
At the moment, I have an index page which includes a header, the header checks if there is a cookie which exists on that persons machine, or if there is a session currently running.
The problem I have is the login page, I have a checkbox that asks if they want their password to be remembered. Based on that decision, it either starts a cookie or a session. The problem with that is that this code has to come at the top of the file, which means I can't put the cookie/session stuff at the top, so I'm getting the "Headers already sent error".
I thought I could cheat the system and use functions, but that doesn't work as when the function is called, it still gives the error.
Anyway, heres some code:
loginform.htm:
(this has more html round it, but i left that out because its irrelevant)
<form action="login.php" method=POST>
<center>
<table border=1 bordercolor=#000000 width=50%>
<tr>
<td>Username:</td>
<td><INPUT type=TEXT name=username width=25></td>
</tr>
<tr>
<td>Password:</td>
<td><INPUT type=password name=password width=25></td>
</tr>
<tr>
<td colspan=2><center><INPUT type=checkbox name=remember> Remember your username and password?</center></td>
</tr>
<tr>
<td colspan=2><center><INPUT type=submit value="Login!"></center></td>
</tr>
</table>
</center>
</form>
and the action behind the form, login.php:
<?php
function newCookie(){
setcookie('username', $_POST['username'], time()+60*2);
setcookie('password', $_POST['password'], time()+60*2);
}
function newSession(){
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
}
$username = $_POST['username'];
$password = $_POST['password'];
// do all database stuff here, looking up passwords etc
if ( ($username == "Tim") && ($password == "pass")){
// lines for debugging
echo $username;
echo "<br>";
echo $password;
// check the users preference of whether to remember password/username or not
if ($_POST['remember'] == "on"){
echo "good";
newCookie();
echo "made new cookie";
}
else{
echo "boo";
newSession();
echo "made new session";
}
}
// display message if username/password are incorrect
else
echo "username and password incorrect";
?>
The headers already sent error depends on if you check the box or not and what function is being called.
Does anyone have any ideas / advice how to get around this?
cheers,
Tim.