0

I'm still new at php and trying to learn. I have the following code and the variables $username and password are not being set, and I'm 99% sure that nothing is wrong with them.

Can you guide me in what I can do to solve this. Everytime I run the script I get a username and password not set message (set with if statement).

    <?php
        require 'connect.inc.php';

        if (isset($_POST['login_button'])&&($_POST['username'])&&($_POST['password'])){
                        $login_button = $_POST['login_button'];             
                        $username = $_POST['username'];
                        $password = $_POST['password'];

                            $password_hash = md5($password);

                                if(!empty($username)&&!empty($password)){
                                    $sql = "SELECT `id` FROM `golden_acres_username` WHERE `uname`='$username' AND `password`='$password_hash'";
                                        if($sql_run = mysql_query($sql)){
                                            $query_num_rows = mysql_num_rows($sql_run);
                                        }
                                            if($query_num_rows==0){
                                                echo mysql_result($sql_run);
                                            }
                                            else if($query_num_rows==1){
                                                echo 'ok';
                                            }
                                } else {
                                echo 'You must supply a username and a password.';
                                }
                            }

                     else {
                        echo $array;
                        echo 'Username and password are not set';
                    }
                    ?>
                        <form class="home_logon_area" action="" method="POST">
                        <table border="0">
                            <tr><td colspan="2">Username: </td></tr>
                            <tr><td colspan="2"><input type="text" class="text_field" name="username" size="30"/></td></tr>
                            <tr><td colspan="2">Password: </td></tr>
                            <tr><td colspan="2"><input type="password" class="text_field " type="password" name="password" size="30"/></td></tr>
                            <tr><td valign="top"><a class="home_content_link_form" href="no_password.html"> Forgor Password </a></td>
                                <td align="right" rowspan="2"><input type="submit" name="login_button" id="login_button" value="Login"/></td></tr>
                            <tr><td><a class="home_content_link_form" href="register.php"> Register </a></td></tr>
                        </table>
                        </form>
?>

Thanks in advance,

Joseph

3
  • 2
    FYI, you are wide open to SQL injections Commented Sep 23, 2013 at 20:49
  • I'm new just learning. I'm sure I can elaborate on security later on. For now if you can help me out on this one it would be great :) Commented Sep 23, 2013 at 20:50
  • FYI, a) you shouldn't use the MySQL extension anymore (prefer MySQLi or PDO, both with prepared statements), b) you shouldn't use md5() for hashing passwords (prefer bcrypt or scrypt). Commented Sep 23, 2013 at 20:51

1 Answer 1

2

Change:

if (isset($_POST['login_button'])&&($_POST['username'])&&($_POST['password'])){

to:

if (isset($_POST['login_button']) && isset($_POST['username']) && isset($_POST['password'])){
Sign up to request clarification or add additional context in comments.

2 Comments

The missing isset for the last two calls to it. An even better way to do this would be: isset($_POST['login_button'], $_POST['username'], $_POST['password']);
Yeah, I just got it, too ;) BTW, it's also possible to use isset($_POST['login_button'], $_POST['username'], $_POST['password']).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.