簡體   English   中英

如何從數據庫中獲取特定值並將其基於該值與PHP進行比較?

[英]How can I get a specific value from a database and compare it based on that value with PHP?

我的登錄頁面有問題。 在我的注冊頁面中,我詢問用戶他/她是學生還是老師。 這被放入數據庫“ dbuploaden”。 當用戶想要登錄時,我需要從數據庫中獲取該信息,因為學生會看到與教師不同的主頁。

這里的問題是,當我按下“登錄”按鈕時,我的頁面似乎刷新了,沒有出現錯誤或其他提示。 這是我使用的PHP代碼:

<?php
session_start();
include_once 'connection.php';

if(isset($_SESSION['user'])!="")
{
//header("Location: home.php");
}
if(isset($_POST['btn-login']))
{
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$res=mysql_query("SELECT * FROM tblgebruikers WHERE email='$email'");
$row=mysql_fetch_array($res);

if($row['password']==md5($upass))
{
if(mysql_query("SELECT * FROM tblgebruikers WHERE soortgebruiker =       student")=="student")
 {  
 die('Following connection error has occured: '.mysql_error());
 $_SESSION['user'] = $row['gebruiker_id'];
 header("Location: index.php");
 }
if(mysql_query("SELECT * FROM tblgebruikers WHERE soortgebruiker =   docent")=="docent")
 {  
 die('Following connection error has occured: '.mysql_error());   
 $_SESSION['user'] = $row['gebruiker_id'];
 header("Location: index2.php");
 }
 }
 if($row['password']!=md5($upass))
 {
 echo "Foute gegevens. Probeer opnieuw.";
 }
 }

?>

謝謝

我剛開始從事網絡編程時,幾乎沒有接受過培訓,當然也沒有老師來指導我。 學習起來可能比許多人一旦已經掌握了在哪里尋找答案以及如何閱讀文檔的困難要難得多。

首先,請,請,請,請使用mysqli功能,而不是mysql人。 此擴展程序已更新是有原因的。 或者,甚至更好的方法是,使用PDO或其他類似的適配器,這樣,如果您決定超出此分配范圍,則代碼將更安全,更易於編寫和以后維護。

此外,參數化查詢! 立即開始使用它們,而忘了mysql_real_escape_string對您來說將是一個美好的世界。

其次,請查看為什么使用md5哈希存儲密碼是個壞主意。 即使是這樣的作業,您也應該練習使用安全和標准的編碼方法,以便在前進時養成良好的習慣。

我已經刪除了“ connection.php”文件的用法,並對數據庫的結構進行了一些假設,以便為您提供有效的代碼片段。 您可以使用以下代碼在很多方面進行優化和改進,但確實可以達到預期的效果。

<?php
session_start();
//make sure you never do this in production
//you should store your connection usernames and passwords outside the web root
//to make unintentional disclosure of them harder
$mysqli = new mysqli( 'localhost', 'username', 'password', 'dpuploaden' );

if( mysqli_connect_errno() ){
    //in real production code, this would be insecure as you shouldn't give away error information
    //that could allow an attacker to gain more knowledge about your systems if at all possible
    exit( printf( "Kan geen verbinding met de database: %s\n", mysqli_connect_error() ) );
}

if( isset( $_POST[ 'btn-login' ] ) ){
    $email = $_POST[ 'email' ];
    $upass = $_POST[ 'pass' ];
    $id = NULL;
    $password = NULL;
    $soortgebruiker = NULL;

    if( $stmt = $mysqli->prepare( 'SELECT `gebruiker_id`, `password`, `soortgebruiker` FROM `tblgebruikers` WHERE `email` = ?' ) ){
        $stmt->bind_param( 's', $email );
        $stmt->execute();
        $stmt->bind_result( $id, $password, $soortgebruiker );
        $stmt->fetch();
        $stmt->close();

        //please do not ever use md5 has a password hashing solution in real code
        //look up the php function password_hash, or if you have PHP < 5.5.0 bcrypt
        //for their proper usage, or better yet, seek out a library that implements
        //this kind of login code and just use it
        //roll your own is always a bad idea when it comes to security and, even with
        //a lot of experience and information under your belt, it is all too easy to make mistakes
        if( $row[ 'password' ] === md5( $upass ) ){
            $_SESSION[ 'user' ] = $id;
            $_SESSION[ 'soortgebruiker' ] = $soortgebruiker;
        }
        else
            exit( "Foute gegevens. Probeer opnieuw." );
    }
    else{
        exit( 'Fout retrieveing ​​informatie.' );
    }
}

if( isset( $_SESSION[ 'user' ] ) && $_SESSION[ 'user' ] != '' ){
    if( $_SESSION[ 'soortgebruiker' ] === 'student' ){
        header( "Location: index.php" );
        exit;
    }
    else if( $_SESSION[ 'soortgebruiker' ] === 'docent' ){
        header( "Location: index2.php" );
        exit;
    }
}

//output login page here
?>

暫無
暫無

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

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