PHP成功登录无法正常工作

时间:2015-02-19 18:00:33

标签: php html login location

我正在使用PHP和HTML进行用户登录,它运行正常,当有空字段或输入错误的密码时它会向我发送错误消息,但是当我想将我重新指向我时,它会失败文件,如果'用户名'和'密码'被正确录取。有什么想法吗?

我的PHP:

 <?php
 session_start();
?>

<?php require_once("../includes/connection.php"); ?>
<?php include("../includes/header.php"); ?>

<?php

if(isset($_SESSION["session_username"])){
// echo "Session is set"; // for testing purposes
header("Location:intropage.php");
}

if(isset($_POST["login"])){

if(!empty($_POST['username']) && !empty($_POST['password'])) {
$username=$_POST['username'];
$password=$_POST['password'];

$query =mysql_query("SELECT * FROM admin_list WHERE username='".$username."' AND password='".$password."'");

$numrows=mysql_num_rows($query);
 if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
  $dbusername=$row['username'];
  $dbpassword=$row['password'];
}

if($username == $dbusername && $password == $dbpassword)

{
$_SESSION['session_username']=$username;

/* Redirect browser */
  header("Location:intropage.php");
  }
} else {
  $message =  "Nombre de usuario ó contraseña invalida!";
  }

} else {
  $message = "Todos los campos son requeridos!";
  }
}
?>

HTML

<div class="container mlogin">
        <div id="login">
<h1>Logueo</h1>
 <form name="loginform" id="loginform" action="" method="POST">
<p>
    <label for="user_login">Nombre De Usuario<br />
    <input type="text" name="username" id="username" class="input" value=""     size="20" /></label>
</p>
<p>
    <label for="user_pass">Contraseña<br />
    <input type="password" name="password" id="password" class="input"   value="" size="20" /></label>
</p>
    <p class="submit">
    <input type="submit" name="login" class="button" value="Entrar" />
</p>
    <p class="regtext">No estas registrado? <a href="register.php"  >Registrate Aquí</a>!</p>
</form>

</div>

</div>

PHP空字段错误消息

<?php if (!empty($message)) {echo "<p class=\"error\">" . "MESSAGE: ". $message . "</p>";} ?>

1 个答案:

答案 0 :(得分:0)

我认为这可能是一个字符串文字问题导致mysql查询失败。

尝试更改:

$query =mysql_query("SELECT * FROM admin_list WHERE username='".$username."' AND password='".$password."'");

改为:

$query =mysql_query("SELECT * FROM admin_list WHERE username='$username' AND password='$password'");