How can I get this multi-user login system script to work?

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

Post Reply
APD1993
Forum Newbie
Posts: 10
Joined: Sat Mar 03, 2012 4:47 pm

How can I get this multi-user login system script to work?

Post by APD1993 »

I am working on a multi-user login script where if a username and password number combination (specified in a MySQL database) is entered correctly, then users are to be redirected to the "congrats.php" page. However, with my current code, even with correct username + password input, the script does not work and instead of going to the "congrats.php" page, the login page (loginform2.php) just reloads :/

My coding is:

Code: Select all

<html><head><title>Multi-User Log In Form</title></head>
<body>
<?php
$self = '';
$name = $_POST['username'];
$pass = $_POST['password'];
?>
<form action = "<?php echo $self; ?>" method = "post">
Username <input type = "text" name = "username" size = "8">
Password <input type = "password" name = "password" size = "8">
<input type = "submit" value="Submit">
</form>
<?php
$conn = mysql_connect("localhost", "root", "") or die ("Err: Conn");
$rs = mysql_select_db("test3", $conn) or die ("Err: Db");
$name = mysql_real_escape_string(trim($name));
$pass = mysql_real_escape_string(trim($pass));
$sql = "select * from users where name='$name' and pass='$pass'";
$rs = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($rs)){
if (mysql_num_rows($rs) >=1)
{
header('Location:congrats.php');
}
else
{
echo "Invalid username and password combination";
}
}
?></body>
</html>

Any help is appreciated :D
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How can I get this multi-user login system script to wor

Post by Celauran »

Storing passwords in plain text is a horrible idea. Don't do it.

Replace this:

Code: Select all

$sql = "select * from users where name='$name' and pass='$pass'";
$rs = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($rs)){
if (mysql_num_rows($rs) >=1)
{
header('Location:congrats.php');
}
else
{
echo "Invalid username and password combination";
}
}
With this:

Code: Select all

$sql = "SELECT COUNT(name) FROM users WHERE name = '{$name}' AND pass = '{$pass}'";
list($count) = mysql_fetch_row(mysql_query($sql));

// If you have more than one result, that's also a problem
if ($count == 1)
{
    // You might want to set some session variables here, too.
    header('Location:congrats.php');
}
else
{
    echo "Invalid username and password combination";
}
Of course, you really shouldn't be using mysql_ functions either, but one thing at a time.
APD1993
Forum Newbie
Posts: 10
Joined: Sat Mar 03, 2012 4:47 pm

Re: How can I get this multi-user login system script to wor

Post by APD1993 »

Celauran wrote:Storing passwords in plain text is a horrible idea. Don't do it.

Replace this:

Code: Select all

$sql = "select * from users where name='$name' and pass='$pass'";
$rs = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($rs)){
if (mysql_num_rows($rs) >=1)
{
header('Location:congrats.php');
}
else
{
echo "Invalid username and password combination";
}
}
With this:

Code: Select all

$sql = "SELECT COUNT(name) FROM users WHERE name = '{$name}' AND pass = '{$pass}'";
list($count) = mysql_fetch_row(mysql_query($sql));

// If you have more than one result, that's also a problem
if ($count == 1)
{
    // You might want to set some session variables here, too.
    header('Location:congrats.php');
}
else
{
    echo "Invalid username and password combination";
}
Of course, you really shouldn't be using mysql_ functions either, but one thing at a time.
Still no luck it seems :(
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How can I get this multi-user login system script to wor

Post by Celauran »

Can you provide maybe a little more detail? What is the output of

Code: Select all

var_dump($count);
APD1993
Forum Newbie
Posts: 10
Joined: Sat Mar 03, 2012 4:47 pm

Re: How can I get this multi-user login system script to wor

Post by APD1993 »

Celauran wrote:Can you provide maybe a little more detail? What is the output of

Code: Select all

var_dump($count);
I get NULL when I try that :/
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How can I get this multi-user login system script to wor

Post by Celauran »

And what happens if you run the query manually?
APD1993
Forum Newbie
Posts: 10
Joined: Sat Mar 03, 2012 4:47 pm

Re: How can I get this multi-user login system script to wor

Post by APD1993 »

Celauran wrote:And what happens if you run the query manually?
I just ran a basic query such as
Use test3;
SELECT uname, pword FROM users WHERE uname="mrjones";

And the relevant results in the database appeared
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How can I get this multi-user login system script to wor

Post by Celauran »

Well that explains it. You were using

Code: Select all

$sql = "select * from users where name='$name' and pass='$pass'";
Post Reply