-
Login Validation
hey,
ok heres my stituation:
I have one page displaying two text boxes (password and username) thats ok, but on the next page how would i validate the username and password?
BTW heres what the table looks like:
id | username | password
1 BD16088 <password>
-
You would have to explain yourself better.
What service scripting are you using? are you using an SQL database, flat text file, or a static user name/password?
-
if you're using mysql, and you already know how to connect to the database, you could use:
PHP Code:
$q mysql_query("SELECT * FROM <tablename>");
$d = mysql_fetch_array($q);
if (($username == $d['username']) && ($password == $d['password'])) {
echo("Login Successfull");
}
else {
echo("Username or Password incorrect");
}
-
I don't code PHP so I'm just wondering about this...
When testing for two conditions against two seperate arrays in a single if(), will it try to find $username in <array> then that part is true, then check if $password is in the next array? If so then you could match up the input username and the input password with person A's username and person B's password.
Just wondering.
-
uhm.... I can't understand what you're saying?
$username will refer to the variable $username
$var["username"] will refer to the "username" value in an array
-
when i go to http://www.gamenationx.com/MM/login.php and type in [username]revolverryan and [password]rmb1990 it works, but when i put in either
[username]saveon and [password]12345 or
[username]BlackDragon465 and [password]unknow
it doesent work. heres the code for the first page:
<html>
<head>
<base href="http://www.gamenationx.com/">
<title>Members Sign in</title>
</head>
<body>
Staff sign in:<br>
<form method="post" action="/MM/page2.php">
Staff Username <input type="text" size="30" name="loginid"><br>
Password <input type="password" size="30" name="loginpword"><br>
<input type="submit" size="30" value="Submit">
</form>
</body>
</html>
and here is the second page's coding :
<?php
$link_id = mysql_connect ("localhost", "gamenat_admin", "rmb1990") or
die ('Cannot connect to the database.');
mysql_select_db ("gamenat_everything");
$q = mysql_query("SELECT * FROM users");
$d = mysql_fetch_array($q);
if (($loginid == $d['username']) && ($loginpword == $d['password'])) {
echo("Login Successfull");
}
else {
echo("Username or Password incorrect");
}
?>
also here is the table structure :
users
userid | username | password
1 | revolverryan | rmb1990
2 | BlackDragon465 | unknow
3 | saveon | 12345
thanks in advance
-
ok, the code I gave you will only check it against the first row
in the table. If you wanna check it against every row, use this:
PHP Code:
<?
$link_id = mysql_connect("localhost","gamenat_admin","rmb1990")
or die ('Cannot connect to the database.');
mysql_select_db("gamenat_everything");
$q = mysql_query("SELECT * FROM users");
// start loop
while ($d = mysql_fetch_array($q)) {
if (($loginid == $d['username']) && ($loginpword == $ ['password'])) {
echo("Login Successfull");
}
else {
echo("Username or Password incorrect");
}
}
// end loop
?>
I've commented the section where the loop is
(it will loop through all of the rows in the table and check it against each one).
I haven't tested this, but test it and tell me how it goes.
-
i got the error:
Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /home/gamenat/public_html/MM/page2.php on line 9
-
ok, i corrected the error but now it displays results for all three:
Username or Password incorrectLogin SuccessfullUsername or Password incorrect
what do i do now?
-
PHP Code:
<?
$link_id = mysql_connect("localhost","gamenat_admin","rmb1990") or die("Cannot connect to the database.");
mysql_select_db("gamenat_everything");
$q = mysql_query("SELECT * FROM users");
$login = false;
while ($d = mysql_fetch_array($q)) {
if (($loginid == $d["username"]) && ($loginpword == $ ["password"])) {
$login = true;
exit;
}
}
if ($login) {
echo("Login Successfull");
}
else {
echo("Username or Password incorrect");
}
?>
-
Sorry for editing it about 100 times lol, that should work though
-
now, when it checks the first row it stops
<?
$link_id = mysql_connect("localhost","gamenat_admin","rmb1990") or die("Cannot connect to the database.");
mysql_select_db("gamenat_everything");
$q = mysql_query("SELECT * FROM users");
while ($d = mysql_fetch_array($q)) {
if (($loginid == $d["username"]) && ($loginpword == $d["password"])) {
echo("Login Successfull");
exit;
}
else {
echo("Username or Password incorrect");
exit;
}
}
?>
-
Check my above post again, like I said I kept editing it :P
-
-
hmm it looks fine to me lol can't see why it wouldn't work...
just for fun, try changing
if ($login) {
to
if ($login == "true") {
it shouldn't make a difference but I really can't see anything wrong
-
well, if the info is wrong it says "username or passowrd incorrect"
but if its right is a blank page
im pretty sure the problem is here somewhere:
if ($login == "true") {
echo("Login Successfull");
}
-
Weird...
PHP Code:
<?
$link_id = mysql_connect("localhost","gamenat_admin","rmb1990") or die("Cannot connect to the database.");
mysql_select_db("gamenat_everything");
$q = mysql_query("SELECT * FROM users");
$success = "no";
while ($d = mysql_fetch_array($q)) {
if (($loginid == $d["username"]) && ($loginpword == $ ["password"])) {
$success = "yes";
exit;
}
}
if ($success == "yes") {
echo("Login Successfull");
}
else {
echo("Username or Password incorrect");
}
?>
try that :/
-
well, for one thing you forgot the 'd'
($loginpword == $d["password"]))
-
-
-
-
PHP Code:
<?php
$loginid = $_POST['loginid'];
$loginpword = $_POST['loginpword'];
$link_id = mysql_connect ("localhost", "gamenat_admin", "rmb1990") or die ('Cannot connect to the database.');
mysql_select_db ("gamenat_everything");
$q = mysql_query("SELECT * FROM users WHERE username='$loginid';");
if(!mysql_num_rows($q)) die('Username does not exist');
$d = mysql_fetch_array($q);
if (($loginid == $d['username']) && ($loginpword == $d['password'])) {
echo("Login Successfull");
}
else {
echo("Password incorrect");
}
?>