Page 1 of 6

Code Issue

Posted: Thu Apr 12, 2012 8:40 am
by TheHappyPeanut
I'm creating a log-in and this is my code:

Code: Select all

<?php

// error reporting temporarily disabled
error_reporting (E_ALL ^ E_NOTICE);

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password)
{

$connect = mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("tracker") or die("Could not find database");
    
}
else
    die("Incorrect information");
?>
I followed a tutorial but something is wrong. If I type in an incorrect database name or any incorrect MySQL information, it says "Incorrect information", which is my else for if the password/user is incorrect. And when I do type in the correct pass/username, it still says Incorrect information. Can you see anything I've blatantly done wrong?

Re: Code Issue

Posted: Thu Apr 12, 2012 8:56 am
by Celauran
You aren't checking if the form has been submitted. When it hasn't, $username and $password contain no value, so your if statement fails and the else is executed.

Perhaps more importantly, any tutorial advocating the use of mysql_connect et al. isn't worth following.

Re: Code Issue

Posted: Thu Apr 12, 2012 8:57 am
by TheHappyPeanut
I read your response on the other guy's thread about that. Is it a security concern or just a bad method in general? What method would you use?

Re: Code Issue

Posted: Thu Apr 12, 2012 9:04 am
by Celauran
It's designed for MySQL 4, is missing features, is no longer developed, and was replaced by MySQLi way back in 2004. That people still use it is mind-boggling. MySQLi is a better option if you'll only be working with MySQL databases. PDO is better still as it offers portability across multiple DBMS without having to rewrite any code.

Re: Code Issue

Posted: Thu Apr 12, 2012 9:21 am
by TheHappyPeanut
Celauran wrote:It's designed for MySQL 4, is missing features, is no longer developed, and was replaced by MySQLi way back in 2004. That people still use it is mind-boggling. MySQLi is a better option if you'll only be working with MySQL databases. PDO is better still as it offers portability across multiple DBMS without having to rewrite any code.
Well, looks like I'll be reading for a while. :P

In response to your first reply where you said it was not checking to verify submission, I Google'd it and found:

Code: Select all

if($_SERVER['REQUEST_METHOD'] == "POST") 
But that didn't seem to work, either.

Re: Code Issue

Posted: Thu Apr 12, 2012 9:23 am
by Celauran

Code: Select all

<?php

if (!empty($_POST))
{
    if ($_POST['username'] && $_POST['password'])
    {
        // etc
    }
}

Re: Code Issue

Posted: Thu Apr 12, 2012 9:33 am
by TheHappyPeanut
This is the markup after I implemented your code:

Code: Select all

<?php

// error reporting temporarily disabled
error_reporting (E_ALL ^ E_NOTICE);

$username = $_POST['username'];
$password = $_POST['password'];

if (!empty($_POST))
{
    if ($_POST['username'] && $_POST['password'])
    {
    $connect = mysql_connect("localhost","root","") or die("Could not connect");
    mysql_select_db("tracker") or die("Could not find database");
    }
}
    
else
    die("Incorrect information");

?>
Is this correct?

Re: Code Issue

Posted: Thu Apr 12, 2012 9:39 am
by Celauran

Code: Select all

<?php

/*
 * Turn error reporting off in production code. Turning it off in a development
 * environment just makes debugging harder and provides no benefit. You really 
 * ought to set this in your php.ini
 */
error_reporting (E_ALL | E_STRICT);

// This serves no purpose.
// $username = $_POST['username'];
// $password = $_POST['password'];

if (!empty($_POST))
{
    if ($_POST['username'] && $_POST['password'])
    {
        // Better to abstract this away and use an include
        $connect = mysql_connect("localhost","root","") or die("Could not connect");
        mysql_select_db("tracker") or die("Could not find database");
    }
    // Move this inside the $_POST check
    else
        die("Incorrect information");

}

?>

Re: Code Issue

Posted: Thu Apr 12, 2012 9:49 am
by TheHappyPeanut
This is what I have now. I apologize for being so helpless, but I've only started learning; I'm certainly trying to troubleshoot as much as I can.

Code: Select all

<?php

if (!empty($_POST))
{
    if ($_POST['username'] && $_POST['password'])
    {
    $connect = mysql_connect("localhost","root","") or die("Could not connect");
    mysql_select_db("tracker") or die("Could not find database");
    }
    else
        die("Incorrect information");    
}
   
?>
I didn't change the mysql_connect because I have not yet read up on includes, so I'll do that in a little while. Other than that, have I placed my code correctly?

Re: Code Issue

Posted: Thu Apr 12, 2012 9:51 am
by Celauran
That looks good.

There really isn't much to includes. Cut/paste the mysql_ bit into its own file, then call it when the page loads.

Code: Select all

include 'path/to/file';
Easy peasy.

Re: Code Issue

Posted: Thu Apr 12, 2012 9:53 am
by Celauran
I'll also note at this point that or die() is fine for development code and debugging, but is completely unacceptable in production code. Be sure to take the time to read up on error and exception handling.

Re: Code Issue

Posted: Thu Apr 12, 2012 9:54 am
by TheHappyPeanut
Celauran wrote:That looks good.

There really isn't much to includes. Cut/paste the mysql_ bit into its own file, then call it when the page loads.

Code: Select all

include 'path/to/file';
Easy peasy.
Would I use the same code as I used with my existing mysql_connect or would it be a different bit of code? From what I've seen on Google, the difference is basically placing the data into a separate file. If I'm wrong, certainly correct me.

In regards to my file, I am now seeing:

[text]Notice: Undefined index: username in C:\xampp\htdocs\Tracker\login.php on line 5
Incorrect information[/text]

Re: Code Issue

Posted: Thu Apr 12, 2012 9:57 am
by Celauran
TheHappyPeanut wrote:Would I use the same code as I used with my existing mysql_connect or would it be a different bit of code? From what I've seen on Google, the difference is basically placing the data into a separate file. If I'm wrong, certainly correct me.
That's right. Straight cut and paste. Again, you don't want to use mysql_ functions, but you can read up on MySQLi/PDO when you have the time.
TheHappyPeanut wrote:In regards to my file, I am now seeing:

[text]Notice: Undefined index: username in C:\xampp\htdocs\Tracker\login.php on line 5
Incorrect information[/text]
That's my bad.

Code: Select all

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

Re: Code Issue

Posted: Thu Apr 12, 2012 9:57 am
by TheHappyPeanut
Furthermore, the "Incorrect information" string is still there, even though I 100% used the correct user and password.

Re: Code Issue

Posted: Thu Apr 12, 2012 9:59 am
by Celauran
TheHappyPeanut wrote:Furthermore, the "Incorrect information" string is still there, even though I 100% used the correct user and password.
What does this give you?

Code: Select all

var_dump($_POST);