-5

因此,我正在尝试制作一个基本网站,您可以在其中注册和登录等等。注册没问题,它将记录保存在数据库中,如果您拥有正确的用户名和密码,则可以登录。如果您没有填写所有字段,它会将您带回 loginform.php 页面并显示“登录失败!”。这也有效,很好。但是,如果您没有输入正确的信息,它应该只是简单地回显“不正确!”。出于某种原因,它没有回应我告诉它的内容,而是进入了一个空白的白页。这是登录表单的代码:

<?php
   echo include("php_includes/header.php");
?>

<h3 class="first">Member Login</h3>         
<form name="loginform" method="post" action="login.php">
<strong></strong><br>
Username: <input name="username" type="text" id="username" /><br><br>
Password: <input name="password" type="password" id="password" /><br><br>
<input type="submit" name="Submit" value="Login" /></form><br>
<a href='register.php'>Register</a>

<?php echo include("php_includes/footer.php");?>

这是实际登录过程的代码:

<?php

session_start();
include ("db_connect.php");

$username = $_POST["username"];
$password = $_POST["password"];

if($username && $password)
{
    $password = md5($password);
    $queryget = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
    $numrows = mysql_num_rows($queryget) or die(mysql_error());
    if($numrows != 0) {//if they can find it in the database
    $_SESSION['username'] = $username; //starts the session
    header("Location: members.php"); //log in
    } else {
       echo "Incorrect!";
    }
 } else {
    echo "Login failed!";
    include("loginform.php");
 }
4

1 回答 1

0

尝试这个:

<?php
session_start();
include ("db_connect.php");

$errors = [];

// Assigning the variables
if ( isset($_POST['username']) && !empty($_POST['username']) ) {
  $username = $_POST['username'];
} else {
  $errors[] = "Username cannot be blank <br />";
}

if ( isset($_POST['password']) && !empty($_POST['password'])) {
  $password = $_POST['password'];
} else {
  $errors[] = "Password cannot be blank <br />";
}

if( isset($username) && isset($password) )
{
  $password = sha1($password);
  $queryget = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
  $numrows = mysql_num_rows($queryget) or die(mysql_error());
  if($numrows != 0) {//if they can find it in the database
    $_SESSION['username'] = $username; //starts the session
    header("Location: members.php"); //log in
    exit;
  } else {
    $error[] = 'Incorrect Username or password';
  }
} else {
  foreach ($errors as $error) {
    echo $error . "<br />";
  } 
}

希望这可以帮助。

于 2013-10-30T00:22:20.460 回答