2017-05-05 38 views
1

我需要能夠定位與來自mysql數據庫的數據進行通信的php代碼,該文件被稱爲「validate.php」。它的主要功能是驗證登錄時是否有空字段,如果用戶的值爲1,則分配一個配置文件,如果表「記錄」中的值爲0,則分配另一個配置文件在php中的會話不加載配置文件,php + mysql

想法是,「validate.php」檢查用戶,並根據他們的個人資料將其引導到一個頁面,但我不能這樣做。

我的代碼是:

<?php 
require('access_db.php'); 
session_start(); 
if(isset($_POST['send'])) { // We verify that the form data has been sent 
    //We verify that the user_name and the user_pass fields are not empty 
    if(empty($_POST['user_name']) || empty($_POST['user_pass'])) { 
    echo" 
    <script> 
     alert('Please enter your username and password correctly '); 
     location.replace('login.php'); 
    </script> 
    "; 
    }else { 
     //"Clean" the form fields of possible malicious code 
     $user_name = mysqli_real_escape_string($link,trim($_POST['user_name'])); 
     $user_pass = mysqli_real_escape_string($link,trim($_POST['user_pass'])); 
     // We verify that the data entered in the form match those of the DB 
     $query=mysqli_query($link,"select user_id,user_name,user_admin FROM users WHERE user_name='".$user_name."' and user_pass ='".$user_pass."'"); 
$row = mysqli_fetch_array($query); 
$_SESSION['user_id'] = $row['user_id']; 
$_SESSION['user_name'] = $row["user_name"]; 
$_SESSION['user_admin'] = $row["user_admin"]; 
if($_SESSION['user_admin']==1){ 
    echo "dashboard.php"; 
    }else{ 
    echo "dashboard2.php"; 
} 

    { 
     } 

}else{ 
    header("Location: login.php"); 
}?> 

我的主要問題是在這裏:

if($_SESSION['user_admin']==1){ 
    echo "dashboard.php"; 
    }else{ 
    echo "dashboard2.php"; 
} 

當我在我的頁面「的login.php」我的管理員用戶登錄,你應該檢查信息和去根據您的個人資料頁面只出現在瀏覽器「http://localhost/proyect/validate.php」和頁面上的文本「儀表板」上,但是,如果我在瀏覽器「http://localhost/proyect/dashboard.php」中加載了帶有所有信息的頁面。

我不知道我在做什麼錯。 有人可以幫助我,我會非常感激,我已經這麼多天了。 謝謝。

+0

爲什麼在屏幕上打印時想要重定向它們。 – programmingArrow

+0

您是否將密碼以明文形式存儲在數據庫中? **不要以明文存儲密碼!**只存儲密碼哈希值!使用PHP的['password_hash()'](http://php.net/manual/en/function.password-hash.php)和['password_verify()'](http://php.net/manual/en /function.password-verify.php)。如果您運行的PHP版本低於5.5(我希望不是),那麼可以使用[password_compat庫](https://github.com/ircmaxell/password_compat)來獲得相同的功能。 –

+1

您的代碼易受SQL注入的影響 - 使用預準備語句而不是直接在sql – RamRaider

回答

1

不打印,試試這個來代替:

if($_SESSION['user_admin']==1){ 
     header('location:dashboard.php'); 
     exit; 
}else{ 
     header('location:dashboard2.php'); 
     exit; 
} 

感謝您的建議Magnus Eriksson

+0

感謝您的幫助,修改並最終輸入我的個人資料。 – Andrew

1

你需要不重定向回聲出php文件 的內容,也做檢查{因爲有多餘的人

if($_SESSION['user_admin']==1){ 
     header("Location: dashboard.php"); 
     }else{ 
     header("Location: dashboard2.php"); 
    } 
+0

不要忘記在標題後面有'exit;'以確保頭文件被直接發送,並且在被解析後沒有任何內容。 –