登录/退出会话问题

时间:2015-08-24 16:01:25

标签: php html session login login-script

我正在创建某种登录/注册系统。注册表,电子邮件确认和登录已经开始。我现在的课程有问题。请记住,这个项目只是一个测试项目。我知道我应该使用PDO但是为了这个测试目的,我需要找出它为什么不能按照我的方式工作。

这是我的login.php PHP代码:

<?php include ('inc/database.php');

if (isset($_POST['submit'])) {
 // Initialize a session:
session_start();
 $error = array();//this aaray will store all error messages

 if (empty($_POST['email'])) {//if the email supplied is empty
 $error[] = 'You forgot to enter  your Email ';
 } else {

 if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['email'])) {
 $Email = $_POST['email'];
 } else {
 $error[] = 'Your EMail Address is invalid  ';
 }
}

if (empty($_POST['passwort'])) {
 $error[] = 'Please Enter Your Password ';
 } else {
 $Password = $_POST['passwort'];
 }

 if (empty($error))//if the array is empty , it means no error found
 {
$query_check_credentials = "SELECT * FROM user WHERE email='$Email' AND password='$Password' AND activation IS NULL";
 $result_check_credentials = mysqli_query($connect, $query_check_credentials);
 if(!$result_check_credentials){//If the QUery Failed
 echo 'Query Failed ';
 }

 if (@mysqli_num_rows($result_check_credentials) == 1)//if Query is successfull
 { // A match was made.

$row = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);
$_SESSION['email'] = $row["email"];


//Assign the result of this query to SESSION Global Variable

 header("Location: index.php");

 }else
 { $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect';
 }
}  else {
 echo '<div> <ol>';
 foreach ($error as $key => $values) {
 echo '    <li>'.$values.'</li>';
}
 echo '</ol></div>';
}
 if(isset($msg_error)){
 echo '<div>'.$msg_error.' </div>';
 }
 /// var_dump($error);

} // End of the main Submit conditional.
?>

这是我受保护的index.php

的开头
<?php 
ob_start();
session_start();
if(!isset($_SESSION['email'])){
header("Location: login.php");
}
include 'header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
</head>
.....

我的会话一定有问题,我不知道为什么。将电子邮件用作会话是错误的吗?我是否将电子邮件用作会话?我还有其他选择吗?

现在问题是,如果我点击登录,则没有任何反应。我将被重定向到login.php而不是index.php!

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

正如Fred -ii-在上面的评论中已经提到的那样,你的$_SESSION['email']永远不会被设置,因此你每次都会被重定向到你的登录页面。

值得注意的是,在使用header("Location: ...");时,您可以在标头之前输出任何输出!否则标题将失败。输出通常是任何HTML,echo,空格(see this SO)。

因此,一旦确定header("Location: index.php");确实有效,请继续修复$_SESSION

$_SESSION = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);未设置$_SESSION['email'](如Fred -ii-所述)。要解决此问题,您需要修复数据库中的结果。

$row = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);
$_SESSION['email'] = $row["email"];

上面的代码将从数据库中的结果返回“email”行,并将其设置为“email”会话,稍后在您尝试访问index.php时会进行检查。

一些侧面指针(不是你当前的问题,而是一些使你的代码更好的技巧)。

  • 使用exit;See this SO
  • 后,您应该使用header("Location: ...");
  • 您没有哈希密码,因此它以纯文本形式存储在您的数据库中(大禁忌)
  • 正确缩进代码使其更易于阅读,从而更容易进行故障排除

如果您执行上述操作,但仍然无效,我们需要更多信息来帮助您进一步排查(例如登录时会发生什么情况(是否符合预期?),返回的结果是什么?等等)。

答案 1 :(得分:0)

尝试改变,

 $_SESSION = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);

 $results = mysqli_fetch_row($result_check_credentials, MYSQLI_ASSOC);

 $_SESSION['email']=$results['email'];

并尝试在登录时检查数据库中的“激活”字段为空...