-4

I am trying to learn how to make a basic login system using PHP and MySQL. I am following a tutorial (http://www.phpeasystep.com/phptu/6.html), but it isn't working.

I tested it out and the inputs from the form on the previous page are working, but it isn't redirecting to the next page. Here's the code. Also, the MySQL connection is working, I'm just hiding the password.

<?php

    mysql_connect("localhost", "metacano_joe", "************") or die ("cannot   connect");

    mysql_select_db("metacano_metacanon") or die(mysql_error());

    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

    $sql = "SELECT * FROM users WHERE username = '$myusername' and password = '$mypassword'";
    $result = mysql_query($sql);

    $count = mysql_num_rows($result);

    if($count==1){
        session_register("myusername");
        session_register("mypassword");
        header("location:login_success.php");
    }
    else {
        echo "Wrong Username or Password";
    }
?>
Phil
  • 157,677
  • 23
  • 242
  • 245

2 Answers2

4

Gotta love not being able to comment

Assuming your code is correct, there are 2 possibilities why it isn't redirecting.

  • $count != 1. Aka username password combination doesn't exit.

  • You're outputting something before the header.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Side notes. Stop using deprecated functions. Please.

Also, do you get any error outputs?

Andrei P.
  • 302
  • 3
  • 9
  • 1
    I think this stands perfectly fine as an answer. +1 – Phil Sep 02 '14 at 00:48
  • 2
    Or the username / password contains a `'`. And I agree with @Phil :-) – jeroen Sep 02 '14 at 00:49
  • 1
    ...it could be a mix of many possible reasons. – Funk Forty Niner Sep 02 '14 at 00:50
  • 1
    I did specify that I'm assuming his code is correct. Either way, a few prepared statements wouldn't hurt... – Andrei P. Sep 02 '14 at 00:51
  • 1
    @user2997488 I couldn't agree with you more ;) OP's code contains too many possible errors and we don't know what the PHP version is, which could be the major issue, maybe even form's elements aren't named, could be anything. You stand at being correct. Will +1 - You're well on your way to be able to comment, cheers. – Funk Forty Niner Sep 02 '14 at 00:53
  • 3
    Live and learn brother, live and learn. God knows I'd be laughing at what I wrote 2 months ago. And I'll probably laugh 2 months from now at what I write today. – Andrei P. Sep 02 '14 at 00:55
  • Thanks for the help. I am going to learn the updated functions the tutorial wasn't using. – Joseph Benedetti Sep 02 '14 at 02:50
  • No problem man. When in doubt always look it up on php.net. Search for similar questions on stackoverflow, google is always there for you. – Andrei P. Sep 02 '14 at 03:04
0
<?php

    mysql_connect("localhost", "metacano_joe", "************") or die ("cannot   connect");

    mysql_select_db("metacano_metacanon") or die(mysql_error());

    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

    $sql = "SELECT * FROM users WHERE username = '$myusername' and password = '$mypassword'";
    $result = mysql_query($sql);

    $count = mysql_num_rows($result);

    if($count>1){
        session_start();
        $_SESSION['myusername'] = "myusername";
        $_SESSION['mypassword'] = "mypassword";
        header("location:login_success.php");
    }
    else {
        echo "Wrong Username or Password";
    }
?>

I edit your script, you can use $_SESSION, it's good for starter