登录失败&重定向失败

时间:2017-02-22 05:51:20

标签: php authentication login

  1. auth.php登录失败。它只能接受来自数据库的1个用户
  2. 无法重定向到admin.php
  3. 上的header("Location: admin.php");

    file:auth.php

    <?php 
    error_reporting(E_ERROR | E_PARSE); 
    $settings_dir = "../settings";
    include "$settings_dir/database.php";
    $login=mysql_query('SELECT username, password FROM admin ');
    $r=mysql_fetch_array($login);
    $admin = $r['username'];
    $admin_pw = $r['password'];
    session_start();
    if (isset($_POST['user']) && isset($_POST['pass'])) {
        $username = $_POST['user'];
        $password = md5($_POST['pass']);
        if (($username == $admin) && ($password ==$admin_pw)) {
            $_SESSION['admin'] = $username;
            $_SESSION['admin_pw'] = $password;
        }
        header("Location: admin.php");
        exit();
    } elseif ((isset($_SESSION['admin']) && isset($_SESSION['admin_pw']) &&$_SESSION['admin'] == $admin && $_SESSION['admin_pw'] == $admin_pw ) || (getenv("REMOTE_ADDR")=="")) {
    } else {
    ?>
    <html>
        <head>
            <title>
                Admin Login
            </title>
            <LINK REL=STYLESHEET HREF="admin.css" TYPE="text/css">
        </head>
        <body>
            <center>
                <br>
                <br>
                <fieldset style="width:30%;">
                    <legend><b>
                        Admin Login
                        </b></legend>
                    <form action="auth.php" method="post">
                        <table>
                            <tr>
                                <td>
                                    Username
                                </td>
                                <td>
                                    <input type="text" name="user">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Password
                                </td>
                                <td>
                                    <input type="password" name="pass">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                </td>
                                <td>
                                    <input type="submit" value="Login" id="submit">
                                </td>
                            </tr>
                        </table>
                    </form>
                </fieldset>
            </center>
        </body>
    </html>
    <?php 
        exit();
    }
    ?>
    

1 个答案:

答案 0 :(得分:-1)

file: auth.php
<?php 
error_reporting(E_ERROR | E_PARSE); 


$settings_dir = "../settings";
include "$settings_dir/database.php";

$login=mysql_query('SELECT username, password FROM admin ');
$r=mysql_fetch_array($login);
$admin = $r['username'];
$admin_pw = $r['password'];

session_start();

if (isset($_POST['user']) && isset($_POST['pass'])) {

$username = $_POST['user'];
$password = md5($_POST['pass']);
if (($username == $admin) && ($password ==$admin_pw)) {
    $_SESSION['admin'] = $username;
    $_SESSION['admin_pw'] = $password;
}
?>
<script>
window.location.href='admin.php';
</script>
<?php

exit();

} elseif ((isset($_SESSION['admin']) && isset($_SESSION['admin_pw']) &&$_SESSION['admin'] == $admin && $_SESSION['admin_pw'] == $admin_pw ) || (getenv("REMOTE_ADDR")=="")) {

} else {

?>
<html>
<head>
<title>Admin Login</title>
    <LINK REL=STYLESHEET HREF="admin.css" TYPE="text/css">
</head>

<body>
<center>
<br><br>

<fieldset style="width:30%;"><legend><b>Admin Login</b></legend>
<form action="auth.php" method="post">

<table>
<tr><td>Username</td><td><input type="text" name="user"></td></tr>
<tr><td>Password</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Login" id="submit"></td>
</tr></table>
</form>
</fieldset>
</center>
</body>
</html>
<?php 
exit();
}
?>