簡體   English   中英

登錄/注銷會話問題

[英]Login / Logout Session Issue

我現在正在創建某種登錄/注冊系統。 注冊表格,電子郵件確認和登錄已經可以使用。 我的會議現在有問題。 請記住,該項目只是一個測試項目。 我知道我應該使用PDO,但是出於此測試目的,我需要找出為什么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!

有什么建議么?

正如Fred -ii-在上面的評論中已經提到的那樣,您的$_SESSION['email']從未設置,因此每次都將您重定向到登錄頁面。

還值得注意的是,當使用header("Location: ..."); ,標頭之前不能任何輸出! 否則,標題將失敗。 輸出通常是任何HTML,echo,空格( 請參見此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; 使用header("Location: ..."); 看這個
  • 您沒有對密碼進行哈希處理,因此它以純文本格式存儲在數據庫中(大號否定)
  • 適當縮進代碼可以使代碼更容易閱讀,從而更容易進行故障排除

如果您執行上述操作后仍然無法使用,我們將需要更多信息來進一步解決問題(例如您登錄時會發生什么(是否如預期?),返回什么結果,等等。向前)。

嘗試改變

 $_SESSION = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);

 $results = mysqli_fetch_row($result_check_credentials, MYSQLI_ASSOC);

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

並嘗試在登錄時檢查數據庫中的“激活”字段是否為空...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM