Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Trouble with login script
Old 09-01-2010, 10:02 AM Trouble with login script
Experienced Talker

Posts: 33
Trades: 0
Hey guys,

I am learning PHP working with a local server on my laptop. I am trying to create a membership site and so I have created a MySQL database for it. My registration script works just fine, whenever I register an account it goes into the users table in the appropriate database with no problem. Here's my problem, all my scripts where a user's email and password have to be matched to authenticate them to do things registered members are allowed to do (to change a password or just to log in) I keep getting the same problem: "email and password do not match"

Unfortunately I do not have the script on this computer from which I am posting so I can't show it to you guys, but what are some possible errors that I might be making? I simply cannot log in with any of the user accounts I create. The problem is not that the email and password don't really match. So what could it be? Are there common mistakes that beginners like me typically make in this situation?

I appreciate any feedback you guys might be able to provide!

Thanks a lot in advance
Frank Drebin is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-01-2010, 03:42 PM Re: Trouble with login script
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
a common problem I can see is input errors.. If you have magic_quotes_gpc on or improper filtering of the inputs for different fields you may get mismatched

my recommendation is first turn magic_quotes_gpc off (if not already) and just compare the input to the database field

so something like

PHP Code:
var_dump($_POST['password']);
var_dumpmysql_result mysql_query("SELECT password FROM table WHERE user_id=1"), 0'password')); 
with some mods to the last line of course, see what the output of both are... if there are slashes or escape characters in there
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 09-01-2010, 04:32 PM Re: Trouble with login script
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,615
Location: UK
Trades: 1
You could do a simple select like:

session_start();
$pass = $_POST['password'];
// md5 encrypt it if you have encrypted it in the database.
$mail = $_POST['email'];

$sql = mysql_query("SELECT * FROM users WHERE email = '$email' AND password = '$password' limit 1");

Then check how many rows are returned,
If 1 row is returned do:
$_SESSION['loggedin'] = "1";
else $_SESSION['loggedin'] = "0";

Then , On any page you want to be members only,
do this at the very top.

session_start();
// If loggedin does not equal 1, Die. ( End script )
if ($_SESSION['loggedin'] != "1") {
die ('Your not loggedin');
}

Hope this helps,
This is a basic way to do it, But works.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 09-01-2010 at 04:34 PM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 09-02-2010, 05:57 AM Re: Trouble with login script
Experienced Talker

Posts: 33
Trades: 0
Thanks a lot for the feedback guys. I will give them a try and let you know later how it worked.

Cheers
Frank Drebin is offline
Reply With Quote
View Public Profile
 
Old 09-02-2010, 06:56 AM Re: Trouble with login script
Experienced Talker

Posts: 33
Trades: 0
Ok here's another question. In the registration script I have the SQL query inserting the passwords as SHA1('$password'). Do I need to do some tinkering in phpmyadmin on the users table? Is this encryption somehow the reason why the email and password matches are never working for me?
Frank Drebin is offline
Reply With Quote
View Public Profile
 
Old 09-02-2010, 07:36 AM Re: Trouble with login script
NullPointer's Avatar
Will Code for Food

Posts: 2,774
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Frank Drebin View Post
Ok here's another question. In the registration script I have the SQL query inserting the passwords as SHA1('$password'). Do I need to do some tinkering in phpmyadmin on the users table? Is this encryption somehow the reason why the email and password matches are never working for me?
If you literally mean:
PHP Code:
sha1('$password'); 
then the problem is you are inserting the string '$password' into the database. Otherwise using a hash shouldn't cause any problems provided you are using the same hash function prior to checking the input password against the database password.

PHP Code:
$password sha1($_POST['password']);
$email $_POST['email']; //make sure to sanitize and validate this as it will be inserted into a query
$sql mysql_query("SELECT * FROM users WHERE email = '$email' AND password = '$password' limit 1"); 
Also, an sha1 hash is 40 characters long. Make sure the password field in the database is set to varchar(40)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 09-02-2010 at 07:39 AM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-02-2010, 08:03 AM Re: Trouble with login script
Average Talker

Posts: 19
Name: Colton
Location: NC, USA
Trades: 0
Don't forget to escape your email value, to avoid SQL injection:
PHP Code:
$email=mysql_real_escape_string($_POST['email']); 
Colton is offline
Reply With Quote
View Public Profile
 
Old 09-02-2010, 09:25 AM Re: Trouble with login script
Experienced Talker

Posts: 33
Trades: 0
Quote:
Originally Posted by Colton View Post
Don't forget to escape your email value, to avoid SQL injection:
PHP Code:
$email=mysql_real_escape_string($_POST['email']); 

Thanks Colton, but yes I am already escaping it with the mysql_real_escape_string()
Frank Drebin is offline
Reply With Quote
View Public Profile
 
Old 09-02-2010, 09:39 AM Re: Trouble with login script
Experienced Talker

Posts: 33
Trades: 0
Quote:
Originally Posted by NullPointer View Post
If you literally mean:
PHP Code:
sha1('$password'); 
then the problem is you are inserting the string '$password' into the database. Otherwise using a hash shouldn't cause any problems provided you are using the same hash function prior to checking the input password against the database password.

PHP Code:
$password sha1($_POST['password']);
$email $_POST['email']; //make sure to sanitize and validate this as it will be inserted into a query
$sql mysql_query("SELECT * FROM users WHERE email = '$email' AND password = '$password' limit 1"); 
Also, an sha1 hash is 40 characters long. Make sure the password field in the database is set to varchar(40)


my query is as follows:

PHP Code:
$query "INSERT INTO users (first_name, last_name, email, password, register_date) VALUES ('$fn', '$ln', '$e', SHA1('$password'), NOW() )";
$result = @mysqli_query ($dbc$q); 
Are you saying that the SHA1('$password') in this SQL query is sending the $password as a string and not as the variable I created earlier in my script?

Last edited by Frank Drebin; 09-02-2010 at 09:44 AM..
Frank Drebin is offline
Reply With Quote
View Public Profile
 
Old 09-02-2010, 10:01 AM Re: Trouble with login script
NullPointer's Avatar
Will Code for Food

Posts: 2,774
Name: Matt
Location: Irvine, CA
Trades: 0
My mistake, I was thinking you were using the php function sha1, not the mysql function SHA1. The query is correct as far as I can tell.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-04-2010, 10:03 AM Re: Trouble with login script
Novice Talker

Posts: 12
Name: chris
Trades: 0
Quote:
Originally Posted by Frank Drebin View Post
my query is as follows:
Are you saying that the SHA1('$password') in this SQL query is sending the $password as a string and not as the variable I created earlier in my script?

Thats what hes saying yes, if you wanted a string you would use sha1('some string data') for a variable use sha1($variable) note no ' '
cdjhosting is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Trouble with login script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.52972 seconds with 12 queries