dtwupu6414 2012-06-06 07:53
浏览 50
已采纳

PHP登录验证在同一页面上?

Could anyone explain me how to check and register username and password in the same page?

How can I echo the "wrong username and password" message in the form?

Thanks in advance.

Check Login

$result=mysql_query($sql);
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
  // Register $myusername, $mypassword and redirect to file "login_success.php"
  session_register("myusername");
  session_register("mypassword");
  header("location:login_success.php");
}
else {
  echo "Wrong Username or Password";

FORM

<form class="clearfix" name="form1" method="POST" action="">
  • 写回答

5条回答 默认 最新

  • douluo5937 2012-06-06 08:00
    关注

    Set the form action to the current .php file. In the script, check whether you already submitted the form (e.g. the submit button has a value in $_POST)

    If there is an answer, proceed with the login code. If no, create a new variable, like $errorMessage. Echo this error message somewhere in the form. (If this is time first time the user loads the script, there will be nothing in this variable, thus it will display nothing.)

    It'd be more elegant if you put the echo of $errorMessage in an if statement with some <div>s or whatever you'd like to use for this task.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?