PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm trying to write a login script using cookies, and a database i've looked a quite a few of the tutorial login scripts on the net and cant seem to find what I am looking for. I pretty much know what I want and am pretty sure i'm doing it correctly, but its still giving me no result. If you could point out where i've gone wrong it would be cool.
<?
require("mysql.php");
if ($action == "login") {
$query = "SELECT password FROM users WHERE username='$username'";
$results = mysql_query($query);
while ($rows=mysql_fetch_assoc($results))
{ //open of mysql password retrieveal
//$encryptedpassword = md5($password);
//sets the password to encrypted version so it match the db password
if ($password === $rows['password']){ //start if password
setcookie("admin", "loggedin", "3600");
header('Location: admin.php');
}//end if password
else { //start else
$error = "incorrect password";
} //finish else
} //close of password retrieveal mysql
}//close of login.
elseif ($action == "logout") {
setcookie("admin", "loggedout", "-3600");
header('Location: http://www.google.com');
}
echo $error;
?>
<?
if ($admin == "loggedin") {
?>
your logged in!!
<?
}
?>
perhaps "register global" is off in your server ad so $action and $admin are not set. If so, try to use $_POST['action'] if $action come from a post or $_GET['action'] if it come from the url. Instead of $admin use $_COOKIE['admin']
that one is ... well.. maybe not minor, but something you should be aware of. any good hosting company will have it off because otherwise it's a security issue