我要做什么
我正在使用登录表单创建单页成员的区域。
登录后,用户唯一要看到的内容(在此阶段)是一个注销按钮。
怎么了
只要数据库中存在匹配项(例如,用户名正确,或者用户名和密码均正确),就会有一个500 Server Error
。
刷新页面,无论密码是否匹配,都可以登录。
当用户单击注销链接时,还会有一个500 Server Error
。
代码
<?php
session_start();
if(isset($_GET['logout'])&&$_GET['logout']==='true'){
logout();
header('location: ./');
}
if(isset($_POST['submit'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$hostname = 'REDACTED';
$username = 'REDACTED';
$password = 'REDACTED';
$dbname = 'REDACTED';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
}
catch(PDOException $e){
echo($e->getMessage());
}
$stmt=$dbh->prepare('SELECT `password`, `id` FROM `tap_users` WHERE `email`=?');
$stmt->execute(array($email));
if($stmt->rowCount()==0){
//incorrect username
header('location: ./?error=user');
exit();
}
$userData = $stmt->fetch(PDO::FETCH_ASSOC);
if($password != $userData['password']) {
//incorrect password
header('location: ./?error=pass');
exit();
} else {
validateUser($userData['id']);
header('location: ./');
}
}
?>
<head></head>
<body>
<?php
if(!isLoggedIn()) {
show_login();
exit();
}
?>
<a href="?logout=true">Logout</a>
</body>
</html>
<?php
//Show login form
function show_login(){
echo '<form method="post">
<input type="text" name="email" placeholder="Email Address" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" value="Submit" name="submit" />
</form>
</body>
</html>';
}
//Check if a user is logged in
function isLoggedIn(){
if(isset($_SESSION['valid']) && $_SESSION['valid'])
return true;
return false;
}
//validate the user
function validateUser($userid){
//this is a security measure
session_regenerate_id();
$_SESSION['valid'] = 1;
$_SESSION['userid'] = $userid;
}
//logout the user
function logout(){
//destroy all of the session variables
$_SESSION = array();
session_destroy();
}
?>
您正在做的是,如果用户登录正确,那么您将重定向到header('location:./'); 也许您没有index.php或您的apache配置中的目录索引已关闭。
做一件事将其更改为
header('location:index.php');
它会工作。
这与php或mysql不相关,这是服务器配置错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句