1

我正在制作一个登录页面,您应该在其中使用用户名和密码才能登录。在登录之前,您需要使用您的姓名注册一个帐户。这是我登录页面的代码:

 <?php
      // if already logged in
      if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
          if(isset($_POST['logout'])) {
              session_destroy();
              echo 'Logged Out!';
              showloginform();
          } 
          else {
              print("<p>Dear $_SESSION[username]</p>");
              print "<p>only a logged in user can see this</p>";
          }
      }
      //not logged in:
      else {
          //have login request:
          if(isset($_POST['username']) && isset($_POST['password'])) {
              include 'opendb.php';
              $username = $_POST['username'];
              $password = $_POST['password'];
              $sql = "SELECT Name, password FROM Restaurant_table WHERE Name = '$username' AND password = '$password'";
              $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
              //found username/password combination:
              if (mysql_num_rows($result) == 1) {
                  $_SESSION['logged'] = true;
                  $_SESSION['username'] = $_POST['username'];
                  $_SESSION['password'] = $_POST['password'];
                  echo 'logged in';
                  //exit;
              }
              else {
                echo 'Sorry, wrong user_id or password.';
              }
          }
          //have no login request:
          else {
            showloginform();
          }
      }

      function showloginform() {
          echo "\r\n please enter your login information to proceed with our site <br/><br/>";
          echo '<form action="start.html" method="post">';
          echo '<div class="input-group">';
          echo '<input type="text" placeholder="User ID" name="username" class="input-transparent" id="email" />';
          echo '<input type="password" placeholder="password" name="password" class="input-transparent"/>';
          echo '</div>';
          echo '<button id="login-submit" type="submit" class="login-button">Manager Login</button>';
          echo '</form>'; 
      }

      function checkpass() {
          include 'opendb';
          $sql = "select * from Restaurant_table where Name='$_POST[username]' and password='$_POST[password]'";
          $result = mysql_query($sql,$conn) or die(mysql_error());
          return  mysql_num_rows($result);
      }
      ?>

但它似乎不起作用,首先当我使用错误信息登录时它不会给我任何错误它只是移动到下一页。假设给我一个错误或什么,我不确定出了什么问题。

4

2 回答 2

4

您正在将操作用作start.html您的<form>. 因此,它重定向到start.html而不是start.php. 改变这个

echo '<form action="start.html" method="post">';

echo '<form action="start.php" method="post">';

囚犯 引述

echo '<form action="" method="post">';
于 2013-06-10T10:10:37.067 回答
3

是的,首先您必须在 php 页面而不是 html 页面中发布值。

 echo '<form action="<?php echo $_SERVER[\'PHP_SELF\']?>" method="post">';

第二个错误是

if (mysql_num_rows($result) == 1) {

而不是这个应该是。

if (mysql_num_rows($result)> 0) {
于 2013-06-10T10:18:36.757 回答