Page 1 of 1
How can I verify a user exists?
Posted: Thu Dec 28, 2006 12:38 pm
by bobby9101
Hi, I have a MySQL Db that contains the table, users
That contains all the users.
I am using sessions to verify users, and on the login page, I check to make sure that the username and password is correct and that there is only one, then I set a session variable that I check on each page of the site.
My question is, I would like to instead of checking: isset(session). use something that verifies that the session is not only set, but that it matches a user from the Database. (other than on the login page) I set two sessions on the login page. "loggedin" and "username" where username is the actual username. So i would need to check the session "username" to make sure it is the DB. But I don't know how to do that.

Posted: Thu Dec 28, 2006 12:42 pm
by daedalus__
Use a
SELECT statement to try to select the username from the database.
Posted: Thu Dec 28, 2006 12:57 pm
by bobby9101
so us an if to see if it can slelect. if it cant then they dont exist.
there ahs to be a better way
Posted: Thu Dec 28, 2006 1:03 pm
by Kieran Huggins
I'd definitely get sober before trying to find that better way....

Posted: Thu Dec 28, 2006 1:08 pm
by Luke
what's wrong with doing it that way? I use something like this...
Code: Select all
public function usernameAvailable($username)
{
$db = $this->DB();
$result = $db->execute("
SELECT `id`
FROM " . $this->_table . "
WHERE `username` = " . $db->qstr($username)
);
return empty($result->fields);
}
Posted: Thu Dec 28, 2006 1:09 pm
by Oren
Do something like:
Code: Select all
SELECT COUNT(id) FROM users_tbl WHERE username='$username'
(Assuming you have a field called "id" in your table)
The returned value will be the number of users in the
users_tbl table which have a username that is equal to the value of
$username.
Posted: Thu Dec 28, 2006 3:52 pm
by bobby9101
^ I get; Resource id #8 when I try to echo.
I have to sue mysql_num_rows
Posted: Thu Dec 28, 2006 4:11 pm
by hawleyjr
A couple things to remember....
Validate the username before you even search. If the username is an email address make sure it's an email address before you check. If the username can't have spaces or wild cards check that also.
Also, use count( id ) or LIMIT 0,1 in your query. All you are looking for is a yes/no answer

Posted: Thu Dec 28, 2006 4:15 pm
by John Cartwright
Oren wrote:Do something like:
Code: Select all
SELECT COUNT(id) FROM users_tbl WHERE username='$username'
(Assuming you have a field called "id" in your table)
The returned value will be the number of users in the
users_tbl table which have a username that is equal to the value of
$username.
Better to alias the count for easy referencing.
Code: Select all
SELECT COUNT(id) as `count` FROM users_tbl WHERE username='$username'
bobby9101 wrote:^ I get; Resource id #8 when I try to echo.
I have to sue mysql_num_rows
Because you have to use mysql_fetch_assoc() or it's sibling functions. You would then reference the count returned with `count`, as you would any other column.