PHP登录脚本'if(isset($ _ POST ['submit']))'不工作

时间:2016-03-04 22:40:11

标签: php

我的网站有一个PHP登录脚本,但if(isset($_POST['submit']))似乎不起作用。它一直以echo "Unable to login. Please contact administrator.";返回。代码是

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();

require "dbc.php";
if(isset($_POST['submit']))
{
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $enc_password = md5($password);

    if($username && $password)
    {
        $query = mysql_query("SELECT * FROM users WHERE username='$username'");
        $numrow = mysql_num_rows($query);

        if($numrow!=0)
        {
            while($row = mysql_fetch_assoc($query))
            {
                $db_username = $row['username'];
                $db_password = $row['password'];
            }

            if($username==$db_username&&$enc_password==$db_password)
            {
                echo "Logged in <a href='membersarea.php'>Click here to enter the members area</a>";
                $_SESSION['username']=$db_username;
                header("location: membersarea.php");
            }
            else 
            {
                echo "Incorrect Username/Password";
            }
        }
        else 
        {
            echo "Incorrect Username/Password";
        }
    }
    else 
    {
        echo "All fields are required";
    }
}
else{
    echo "Unable to login.  Please contact administrator.";
}

?>
<!--V0.3.5 Alpha-->

这是您输入信息的页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <meta name="description" content="Home to  and its game downloads."/>
        <meta name="keywords" content="Games, Downloads, Free, Fun, Game Development, Roll Ball, "/>
        <meta name="author" />
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <title>

        </title>
    </head>
    <body>
        <div id="validxhtml">
            <p>
                <a href="http://validator.w3.org/check?uri=referer"><img
                src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a>
            </p>
        </div>
        <div id="validcss">
        <p>
            <a href="http://jigsaw.w3.org/css-validator/check/referer">
                <img style="border:0;width:88px;height:31px"
                    src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
                    alt="Valid CSS!" />
            </a>
        </p>

    </div>
        <div id="title">
            <h1 class="hcenter"></h1>
        </div>
        <div id="subtitle"><h2 class="hcenter">Login</h2></div>
        <div id="links">
            <ul>
                <li><a href="index.html" title="Home">Home</a></li>
                <li><a href="game.html" title="Current Games">Games</a></li>
                <li>Login</li>
                <li><a href="contact.html" title="Contact Me">Contact Me</a></li>
            </ul>
        </div>
        <div id="body"><h3>Please Login</h3>
         <?php if(isset($_GET['error'])){echo $_GET['error'];echo "<br />";} ?>
            <form action="login.php" method="POST">
                    Username: <input type="text" name="username"><br>
                    Password: <input type="password" name="password"><p>
                <input type="submit" value="Login"> 
            </form>

            <a href="register.php">Don't have an account?  Register here.</a>
        </div>
        <div id="policy" class="hcenter">
            <a target="_blank" href="http://privacypolicies.com/privacy/view/PUlI8q">Privacy policy</a>
        </div>
            <script type="text/javascript">
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-73371388-2', 'auto');
  ga('send', 'pageview');

</script>
    </body>
</html>
<!--V0.3.5 Alpha-->

我还有最后一件事要说,第二个脚本是您输入登录信息的那个,当您点击“登录”时,它会将其指向第一个。如果您有任何疑问,请随时与我联系。

1 个答案:

答案 0 :(得分:4)

你的html代码中没有name="submit"的任何输入,所以这就是为什么if子句被滑落了

更改

<input type="submit" value="Login"> 

要:

<input type="submit" value="Login" name="submit">