2016-05-30 84 views
0

我的登录页面出现问题。在我的注册页面中,我询问用户他/她是学生还是老师。这被放入数据库'dbuploaden'。当用户想要登录时,我需要从数据库中获取这些信息,因为学生看到的不是主页而是主页。如何从数据库中获取特定值并根据该值与PHP进行比较?

这里的问题是,当我按下“登录”按钮,我的页面似乎刷新,并没有给我一个错误或任何东西。这是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."; 
} 
} 

?> 

感谢

+0

'if(isset($ _ SESSION ['user'])!=“”)'这是不正确的语法。你需要2个单独的条件。另外,我希望这不是一个现场。 –

+0

这段代码有很多错误的地方。 – Phiter

+0

没错错吧?你有没有试图捕捉/显示错误报告?它可能甚至没有使任何查询。它只是在你身上死去*无声无息*。工作中的沉默杀手。 (*末日之犬更嚎叫...... *)。 –

回答

0

我在web编程开始了与很少或几乎没有训练,当然也没有老师来指导我。学习起来可能会更难一些,因为一旦他们已经掌握了寻找答案的地方以及如何阅读文档,很多人都会放弃。

请首先请使用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 
?> 
相关问题