2016-05-30 8 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プログラミングに始まった、と確かに先生が私を導いていないし。すでに多くの人が解答を探す場所とドキュメンテーションの読み方を理解していれば、多くの人が手放すことができます。

mysqlではなく、mysqliの機能を使用してください。この拡張機能は理由で更新されました。 PDOなどのアダプタを使用して、この1つの割り当てを超えることを決心した場合は、コードの安全性が向上し、後で書き込みと保守が容易になります。

また、パラメータ化されたクエリ!それはあなたに今すぐそれらを使用して開始してmysql_real_escape_stringを忘れることの良い世界を行います。

第2に、パスワード保管に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 
?> 
関連する問題