PDA

View Full Version : simple php/mysql login - need help!



StigC
May 1st, 2008, 06:19 AM
Hey Everyone.

I'm trying to build a simple php login, and I can't get it to work. It's based off of a tutorial, that I'm no longer able to find.
I remember it working at some point, but now it's not working at all.
It seems it's not checking the DB at all, and proceeds right to the "You're in" statement. And I don't understand why - I think this way of checking against the db worked initially.

Well here's the code:


<?php

require("vestconfig.php");

$myuser = addslashes($_POST['user']);
$mypass = md5($_POST['pass']);

$result = mysql_query("SELECT count(brugerID) FROM Login WHERE brugerPass='$mypass' AND brugerNavn='$myuser'")
or die("Couldn't query the user-database.");

$num_rows = mysql_num_rows($result);

if($num_rows == 1) {

echo "You're in...";

} else {
include('login_form.html');
}
mysql_close($linkID);
?>The input form is located within 'login_form.html' - which you should be redirected to, since there's nothing to post initially, IE the db would get no results.
But it's not working...

Even though this doesn't seem like the smartest way of doing it, I've spent so much time on it now, that I would like to get it to work, in order to learn something and understand php better.

I hope you guys can help me out.
Thanks.

StigC
May 2nd, 2008, 07:58 PM
Anyone?

kdd
May 3rd, 2008, 02:06 AM
There are some goofy things in that code. Like, what's in vestconfig.php? DB connection stuff, I assume?

Also, when you do SELECT COUNT(something) ... you're counting the items and it'll always return just 1 row with format similar to this:

---------
brugerID
---------
3
---------

This is just 1 row with contents of 3. This is different from

---------
brugerID
---------
1
---------
2
---------
3
---------

If you run mysql_num_rows ( $result ) on this, you'd get 3.

If you're already fine with doing login this way, then I'd say remove the num_rows line, and put this:



$result = mysql_fetch_row ( $result );
$result = $result[0];


Then your if ( $result == 1 ) ...

Hope this helps. :)

StigC
May 3rd, 2008, 09:03 AM
Hey kdd - thank you so much for your help.

Yes vestconfig is just database login, etc.

I think I understand it a little better now. I will try it out later today.

Thanks again.

kdd
May 3rd, 2008, 01:50 PM
No problem man. Glad to help! :)

StigC
May 3rd, 2008, 02:30 PM
Hmm, I don't think the whole approach to this is working.

There's something basically wrong with the script, and there must be a better way of doing it.
Basically it's not working the way it should.

It's supposed to be an entrance to a flash CMS, therefore I didn't want to bother with Sessions and so forth, as I wouldn't need to move across several html/php pages.

But I guess I have to start over.

But thanks anyway kdd.

Stig

kdd
May 3rd, 2008, 04:14 PM
Well, what's going on? Are you getting any errors or anything? How do you know it's not working?

Anyways, maybe you can follow this tutorial and see what to do: http://www.phpeasystep.com/workshopview.php?id=6

StigC
May 3rd, 2008, 11:37 PM
Well - after altering the script, when I log in it gives me a 404 error. Instead of just echoing "You're in" it goes to 404.

So where I'm now, is that I get the login screen (the else statement) but after writing the user/pass I get the 404. With both valid user/pass and gibberish...
And I can't really grasp why this is happening. Maybe it's a server issue?

But I will definately check out the tutorial.

Thanks! :)

Stig

StigC
May 4th, 2008, 08:09 AM
Okay, the problem was in the login form. It wasn't referencing the php file properly.

But I'll still give the tutorial a go.

StigC
May 4th, 2008, 08:30 AM
The tutorial worked like a charm! Thank you very much kdd!