I don't know if it works, but try this:
[php]<?php
function loginForm(){
echo '
<form action="
'.
$_SERVER['REQUEST_URI'].
'
" method="POST">
<b>Brukernavn: </b><input type="text" name="loginUser" /><br/>
<b>Passord: </b><input type="password" name="loginPass" /><br/>
<input type="submit" value="login" />
</form>
';
}
//1: something was posted
if ($_POST['loginUser'] && $_POST['loginPass']){
// Take the user name and pass and clean them and put them into variables
$password = mysql_real_escape_string(trim(htmlspecialchars(striptags($_POST['loginPass']))));
$username = mysql_real_escape_string(trim(htmlspecialchars(striptags($_POST['loginUser']))));
$result = mysql_query('SELECT * FROM admins WHERE aUser = `'.$username.'`');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ( md5($password) == mysql_fetch_assoc($row["aPass"]) ){
setcookie('logedin','true',time()+3600);
$madeit = true;
// Now that we sent a cookie we have to refreash the page so that the cookie will be sent back to us!
// Remeber you cant use a cookie on the same request you make it!
header('Location: '. $_SERVER['REQUEST_URI']);
}
}
if ($madeit != true) { echo 'Error! Invalid password!<br>'. loginForm(); }
//2: the person already had a cookie set
} elseif ($_COOKIE['logedin'] == true){
echo'you made it!';
} else {
//3: first page visit
loginForm();
}
?>[/php]
Now this is a bad way to do a login... never store users in a cookie as anyone can just make one that has the data you just stuck in it.
Store the data in sessions instead.
cookies and sessions