How can I verify a user exists?

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
bobby9101
Forum Commoner
Posts: 28
Joined: Thu Apr 27, 2006 1:18 pm

How can I verify a user exists?

Post 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. :roll:
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Use a SELECT statement to try to select the username from the database.
bobby9101
Forum Commoner
Posts: 28
Joined: Thu Apr 27, 2006 1:18 pm

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I'd definitely get sober before trying to find that better way.... 8O
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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);
    }
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
bobby9101
Forum Commoner
Posts: 28
Joined: Thu Apr 27, 2006 1:18 pm

Post by bobby9101 »

^ I get; Resource id #8 when I try to echo.
I have to sue mysql_num_rows
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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

:)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply