simple check if user exists

This is a discussion on "simple check if user exists" within the PHP Forum section. This forum, and the thread "simple check if user exists are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Aug 22nd, 2007, 09:04
New Member
Join Date: Aug 2007
Location: Italy
Age: 35
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
simple check if user exists

hi- first post, so i hope this is worthy-
i have a script that checks a mysql db to see if
a- the user exists
b- the password is the same as what was sent from the form

the script below works fine for point b, but what about a? if the user never "registered" (and therefore, is not in the db) how can i check for that? YOu can see in my code where i commented the part that is not working
PHP: Select all

<?php
include("connect/localhost.php");
$un=$_GET['userName'];
$ps=$_GET['password'];

if(!
$un || !$ps){
    echo (
"not valid");
    return;
}else{
    
compareConditions($un,$ps);
}
function 
compareConditions($un,$ps){
    
$sql="SELECT * FROM users WHERE userName='$un'";
    
$res=mysql_query($sql)or die(mysql_error());
    while(
$infomysql_fetch_assoc($res)){
        if(!
$info){
                echo (
"user does not exist");
            }
//does not work
        
if($ps==$info['password']){
            echo 
$un;
        }else if(
$ps!=$info['password']){
            
incorrect();
        }
        
    }
}
function 
incorrect(){
    echo (
"incorrect username or password");
}
?>
Reply With Quote

  #2 (permalink)  
Old Aug 22nd, 2007, 10:13
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: simple check if user exists

Code: Select all
if($un!=$info['userName']) {
    echo ('user does not exist');
}
That should do the trick

I know you're probably just learning, but using $_GET is not secure, you should use $_POST and your password is not encrypted, but you can look into that later. Just a warning!

Maybe you should make your query like the one below, this way it will only pull out the user if the username and the password are correct:
$sql="SELECT * FROM users WHERE userName='$un' AND password='$ps'";

If you need any more help, give us a yell.

Last edited by jimz; Aug 22nd, 2007 at 10:14. Reason: bad grammar :(
Reply With Quote
  #3 (permalink)  
Old Aug 22nd, 2007, 15:33
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: simple check if user exists

Hi Mlecho

I think the problem is that when there is no matching user name the while loop never executes so you never see your 'user does not exist' message. Specifically:

mysql_fetch_assoc will return false on an empty result set.

Jimz's solution won't work i don't think for the same reason.

You want to check if the query returned anything.

You could try a test:

Code: Select all
mysql_num_rows ($res) > 0
If that is true you know you had a user and now you can check for the password.

Assuming you are enforcing unique user names why are you using a loop at all?

Or use Jimz's SQL solution and do it all in one SQL line but watch out for SQL Injection

In the equivalent Perl DBI module you can test for undefined to see if the result set was empty. I think the above is ok but I'm really not sure what the best way to test for an empty result set should be in PHP.

Justin

Last edited by Kropotkin; Aug 22nd, 2007 at 15:37. Reason: typo
Reply With Quote
  #4 (permalink)  
Old Aug 28th, 2007, 15:48
New Member
Join Date: Aug 2007
Location: Italy
Age: 35
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: simple check if user exists

yeah...that does it (with some conditionals)- thanks guys....
Reply With Quote
Reply

Tags
mysql

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check to see if a cfwindow exists BenR41 Other Programming Languages 0 Feb 25th, 2008 12:22
fastest way to check if table exists CloudedVision PHP Forum 3 Feb 20th, 2008 16:15
Check if user has clicked an adwords ad. Kimochi JavaScript Forum 4 Mar 4th, 2007 16:31
Check whether URL exists Sheepymot PHP Forum 2 Jul 24th, 2005 08:16
Check User Amari Classic ASP 10 Dec 20th, 2004 02:29


All times are GMT. The time now is 07:18.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43