2016-11-02 4 views
1

私は自分の知識を向上させるために単純なログインシステムに取り組んでいます。ユーザーが間違ったパスワードやユーザー名を入力したり、フィールドを入力しなかったり、何かを始めたときに警告メッセージを表示する方法を知っています私はそれが正しい方法であるかどうかわからないし、ログインフォームでページにメッセージを表示する方法がわからない。あなたの助けに感謝:)PHPログインエラーを表示するには?

は、これはこれはこれは私のfunction.phpあるログインフォームと私のインデックスファイル

<form class="form-signin" action="includes/ctrl_login.php" method="post"> 
    <h3 class="form-signin-heading">Login </h3> 

    <div class="form-group"> 
     <label for="username" class="sr-only">Username </label> 
     <input type="text" id="username" name="username" class="form-control" placeholder="Username"> 
    </div> 
    <div class="form-group"> 
     <label for="password" class="sr-only">Password</label> 
     <input type="password" id="password" name="userpassword" class="form-control" placeholder="Password"> 
    </div> 

    <button class="btn btn-lg btn-primary btn-block" type="submit">login</button> 
</form> 

である私のログインコントローラ

<?php 
/** 
* Nome File : ctrl_login.php 
* Descrizione: File che gestisce i dati inseriti nella pagina di login 
*/ 

include('../config/konasi.php'); 
include('functions.php'); 

$user_name = check_input($_POST['username']); 
$user_password = check_input($_POST['userpassword']); 

if (!$user_name || !$user_password) { 
    echo "Non hai inserito username o password"; 
    exit(); 
} 

if ($stmt = mysqli_prepare($conn, "SELECT user_password FROM users WHERE user_name= ? ")) { 
    /* Bind parameters: s - string, b - blob, i - int, etc */ 
    $stmt -> bind_param("s", $user_name); 
    /* Execute it */ 
    $stmt -> execute(); 
    /* Bind results */ 
    $stmt -> bind_result($result); 
    /* Fetch the value */ 
    $stmt -> fetch(); 
    /* Close statement */ 
    $stmt -> close(); 
} 

if(password_verify($user_password, $result)) {  
    echo "you are connected!";  
} else {   
    echo "Ops, wrong password"; 
} 

mysqli_close($conn); 

です

function check_input($data) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
+0

あなたは多くの可能性があります。 'ctrl_login.php'では、ユーザをフォームのログインにリダイレクトし、クエリー文字列の中でエラーを渡すことができます(' your_form_page.php?errors = Non hai inserito username o password'のように)。それは主に意見に基づく質問です。あなたに最適なものを選んでください。チャオ。 – Federkun

+0

Ciao Federkunあなたの答えに多くの感謝。私はそれを試してみましょう;)grazie – pippo

答えて

0

方法の一つは、あなたのコントローラでセッション変数を設定して、ログインページに表示し、ページにログインするためにそれらをリダイレクトすることです。

あなたのログインページとコントローラの両方の上に行を置きます。

session_start(); 

代わりにエコーメッセージのログインフォームページで、あなたのコントローラで

<p><?=$_SESSION['error_msg']?></p> 

を次の行を追加し、セッション変数にそれらを設定します。

$_SESSION['error_msg'] = 'Ops, wrong password'; 
header("location: link_to_login_form"); 
exit(); 

などです。

+0

こんにちはReza私はセッションを使用してエラーメッセージを渡すために管理していたあなたのサポートに多くの感謝。今私はセッションを使用してリダイレクトを作成したいので、ユーザーが管理者のダッシュボードに移動し、クライアントがクライアントのダッシュボードに移動する場合。私はデータベースに新しいテーブルを作成しました。私はそれをuser_roleと呼んでいます。ここでは、intの値を1または0に設定できます。 – pippo

+0

認証に成功した後。データベースからユーザー役割を確認します。次にif(user == 'admin')ヘッダ( "location:admin.php")を入力します。 else header( "location:user.php"); – reza

-1

だけでログインをチェックするために、この機能を使用します。

function login_check($username,$pwd){ 

    if ($stmt = mysqli_prepare($conn, "SELECT COUNT(id) FROM `users` WHERE `user_name`=? AND `user_password`=? ")) { 
     /* Bind parameters: s - string, b - blob, i - int, etc */ 
     $stmt -> bind_param("ss", $user_name,,$pwd); 
     /* Execute it */ 
     $stmt -> execute(); 
     /* Bind results */ 
     $stmt -> bind_result($result); 
     /* Fetch the value */ 
     $login_row=$stmt -> fetch(); 

     if($login_row[0]>=1) { 
      echo "You are connected"; 
     } else { 
      echo "Ops, wrong password"; 
     } 

     /* Close statement */ 
     $stmt -> close(); 
    } 
} 
+0

こんにちはサラスはあなたの答えのために多くのありがとう。私はPHPの本で読む私はSQLインジェクションを避けるためにSELECTクエリに変数を使用すべきではありません。また、私はこの機能を使用すれば、ログインフォームのページにメッセージを表示できますか?これはPHPコントローラとは別のファイルです – pippo

+0

更新された回答をご覧ください。 –

+0

回答を受け入れてください。 –

関連する問題