簡體   English   中英

我的網站會自動創建一個保存在數據庫中的用戶的會話

[英]My website automatically creates a session of a user saved in a database

我使用php創建了一個登錄腳本,並使用MySQL數據庫存儲用戶數據。 一切正常,直到我認識到我的一個朋友(也在數據庫中像我一樣創建了他的帳戶)打電話給我,並說我的個人資料已自動加載到他的手機上。 怎么可能這樣,因為如果不使用各自的憑據填寫登錄表單,就無法創建我的會話數據或任何人的會話!?

幫我這個! 由於這是未經用戶同意的用戶數據安全性的主要循環,因此如何通過他人的電話進行訪問!

通過login.php創建會話

session_start();
if (isset($_POST['email']) and isset($_POST['password'])){
$email = $_POST['email'];
$password = $_POST['password'];
$query = "SELECT * FROM `users` WHERE email='$email' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0){
$_SESSION['email'] = $email;
$_SESSION['id'] = $id;
echo "You are logged in.";
header("Location: index.php?log=success");
unset($_POST);
}
else{
echo " Something went wrong ";
}
}

您正在$_SESSION['id']分配一個$id的值,但是您在代碼中沒有指定$id的值是什么。 這就是導致您的會話混亂的原因,因為每個人都被賦予相同的null標識。

從數據庫中獲取實際的用戶ID,並在$_SESSION['id']使用該值,您的問題已得到解決。 參見以下示例:

/* Start the session */
session_start();

/* Check if the user has submitted the login form $_POST */
if (isset($_POST['email']) and isset($_POST['password'])){

    /* Get the values of $_POST */
    $email = $_POST['email'];
    $password = $_POST['password'];

    /* Create the SQL query - Note: you should use bindings to prevent SQL injection */
    $query = "SELECT * FROM `users` WHERE email='$email' and password='$password'";

    /* Get the MySQL result */
    $result = mysql_query($query) or die(mysql_error());

    /* Check if the user was found */
    if(mysql_num_rows($result) > 0){

        /* Get the User ID from the database */
        while($row = mysql_fetch_assoc($result)){
            $id = $row['THE NAME OF YOUR ID COLUMN']; // replace with the name of your ID column in your DB
        }

        /* Set the session values */
        $_SESSION['email'] = $email;
        $_SESSION['id'] = $id;

        /* Print output */
        echo "You are logged in.";

        /* Redirect to login page */
        header("Location: index.php?log=success");

        /* Unset POSTS */
        unset($_POST);

    } else {
        /* Show login error message */
        echo " Something went wrong ";
    }
}

另外,您的代碼當前容易受到SQL注入攻擊。 為了防止此類攻擊,我建議您將參數綁定到SQL查詢,而不是直接輸入參數。 SQL注入信息

暫無
暫無

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

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