2017-08-04 107 views
0

我正在創建登錄腳本,並且無法讓用戶登錄並直接進入正確的頁面。目前,用戶提交登錄表單,該登錄表單指向「安全」頁面,該頁面檢查會話匹配,如下所示,但它似乎不斷踢我。會話未創建導致無法登錄

這裏是我的代碼 -

的login.php

session_start(); 
include("clientarea/inc/config.php"); //Establishing connection with our database 

$error = ""; //Variable for storing our errors. 
if(isset($_POST["login"])){ 
if(empty($_POST["email"]) || empty($_POST["password"])){ 
    $error = "Both fields are required"; 
} else { 
// Define $email and $password 
    $email = $_POST['email']; 
    $password = $_POST['password']; 

// To protect from MySQL injection 
    $email = mysqli_real_escape_string($connection, $email); 
    $password = mysqli_real_escape_string($connection, $password); 
    //$password = md5($password); 

//Check username and password from database 
    $stmt = "SELECT * FROM users WHERE email='$email' and password='$password'"; 
    $result = mysqli_query($connection, $stmt); 
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC); 

//If username and password exist in our database then create a session. 

    if(mysqli_num_rows($result) == 1){ 
     $_SESSION['email'] = $login_user; // Initializing Session 
     header("location: clientarea/index.php"); // Redirecting To Other Page 
    } else { 
     $error = "Incorrect username or password."; 
    } 
} 
} 

正如你可以將它檢查,看看是否有一個用戶,然後引導到「安全」頁面驗證後看到。

這裏是auth.php

include('inc/config.php'); 
session_start(); 
$user_check = $_SESSION['email']; 

$sql = mysqli_query($connection, "SELECT email FROM users WHERE email='$user_check' "); 

$row = mysqli_fetch_array($sql,MYSQLI_ASSOC); 

$login_user = $row['email']; 

if(!isset($user_check)) 
{ 
header("Location: http://localhost/findpt/index.php"); 
} 

現在auth.php是在「安全」頁面的頂部運行,以確保用戶登錄,否則重定向到主頁。現在,如果我刪除auth.php文件,那麼我會直接進入'安全'頁面,但是它已經到位,它會將我引導至主頁。

如果我回顯$ _SESSION ['email']我什麼也收不到?

回答

0

如果我正確閱讀你的文章,login.php是一個表單,它將它們重定向到auth.php的安全頁面。

通過login.php查看,它看起來並沒有初始化$ login_user。所以你將一個未分配的變量分配給$ _SESSION ['email']。

此外,也許可以在login.php中創建另一個條件來檢查重複登錄,以覆蓋您的P的& Q's。