0

My website has a PHP login script but the if(isset($_POST['submit'])) doesn't seem to work. It keeps returning with echo "Unable to login. Please contact administrator.";. The code is

<?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-->

This is the page where you type the information

<!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-->

I also have one last thing to say, the second script it the one where you type in your login information, when you click 'Login' it directs it to the first one. If you have any questions, please ask me at any time.

Geeoon Chung
  • 79
  • 1
  • 9
  • You are using `mysql_*` functions in your code. Please see this question on why you should stay away from them: http://stackoverflow.com/q/12859942/4464702 – randers May 21 '16 at 21:26

1 Answers1

4

You don't have any input with name="submit" in your html code, so thats why the if clause is skiped

Change

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

To:

<input type="submit" value="Login" name="submit"> 
Neobugu
  • 333
  • 6
  • 15