Code Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Code Issue

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

Code: Select all

<?php

if (!empty($_POST))
{
    if ($_POST['username'] && $_POST['password'])
    {
        // etc
    }
}
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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");

}

?>
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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]
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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']))
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

Furthermore, the "Incorrect information" string is still there, even though I 100% used the correct user and password.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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);
Post Reply