 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
10-27-2007, 12:03 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Login Script
Can anybody point me in the direction of a tutorial that shows you how to make a good, secure login script? All the ones I seem to find have people commenting that they are insecure for one reason or another so I would like to see how to do it properly :)
|
|
|
10-27-2007, 02:01 PM
|
#2 (permalink)
|
|
The Contributor
Join Date: Sep 2007
Posts: 31
Thanks: 0
|
I have a lot of experience with PHP/MySQL logins. I'll try to write a tutorial for you later.
|
|
|
|
10-27-2007, 03:13 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
That would be great, thanks a lot :D
|
|
|
10-27-2007, 10:24 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
I'd be tempted to construct my login SQL like so:
Code:
$szUsername = 'Wildhoney';
$szPassword = 'myHardToGuessPassword';
$szSQL = sprintf(" SELECT
@myPassword:= MD5('%s'),
IF(myUsername = '%s', myUsername, NULL) AS myUsername,
IF(myPassword = @myPassword, myPassword, NULL) AS myPassword
FROM
myTable
WHERE
myUsername = '%s'
AND
myPassword = @myPassword)",
$szPassword,
$szUsername,
$szUsername);
Therefore if anyone does happen to manage to inject SQL into your query, it'll simply result in a MySQL error, and they won't actually get anywhere.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
|
The Following User Says Thank You to Wildhoney For This Useful Post:
|
|
10-28-2007, 01:24 PM
|
#5 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
Surely that way is less secure than the usual SQL statement as you'll be transmitting the password as clear text, whereas usually you'd md5 it in PHP and send it encrypted.
You're also repeating $szUsername in your sprintf arguments, tut tut :P
Seriously though, I don't understand how that method is more secure than the standard approach (assuming you filter and escape data correctly, of course)? Just seems like more work for the same outcome.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
10-28-2007, 02:18 PM
|
#6 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
lol see what I mean :p
I just want a nice tutorial I can use that will show me how to do a secure login with no problems :D lol.
|
|
|
10-29-2007, 10:39 AM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Anybody lol? :D
|
|
|
10-29-2007, 12:09 PM
|
#8 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
If you wanted to be really secure then you'd MD5 the password before you sent it to the server using Javascript. Apologies for confusing you JMZ, but I think Daz said he was writing you a little guide :) !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
10-29-2007, 01:06 PM
|
#9 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
Hi Jmz, as a general rule, for a secure application you should always filter input and escape output (you'll hear that tip again and again). Basically, that means that if you're expecting a string from a form, ensure the data you get really is a string. If you're outputting data to a database, make sure you escape it first using mysql_escape_string(). Following these two rules will make your application a lot more secure.
So let's say that you are expecting szUsername and szPassword from $_POST, you could filter these using the built in filter functions:
PHP Code:
$aFilterOptions = array ( 'szEmail' => FILTER_SANITIZE_EMAIL, 'szPassword' => FILTER_SANITIZE_STRING );
$aFiltered = filter_input_array(INPUT_POST, $aFilterOptions);
Then you simply escape the values before using them in your query, such as:
PHP Code:
$szSql = sprint(" SELECT * FROM members WHERE username = '%s' AND password = '%s'", mysql_escape_string($aFiltered['szUsername']), mysql_escape_string($aFiltered['szPassword']))
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
11-17-2007, 01:09 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by Karl
Hi Jmz, as a general rule, for a secure application you should always filter input and escape output (you'll hear that tip again and again). Basically, that means that if you're expecting a string from a form, ensure the data you get really is a string. If you're outputting data to a database, make sure you escape it first using mysql_escape_string(). Following these two rules will make your application a lot more secure.
So let's say that you are expecting szUsername and szPassword from $_POST, you could filter these using the built in filter functions:
PHP Code:
$aFilterOptions = array
(
'szEmail' => FILTER_SANITIZE_EMAIL,
'szPassword' => FILTER_SANITIZE_STRING
);
$aFiltered = filter_input_array(INPUT_POST, $aFilterOptions);
Then you simply escape the values before using them in your query, such as:
PHP Code:
$szSql = sprint(" SELECT
*
FROM
members
WHERE
username = '%s' AND
password = '%s'",
mysql_escape_string($aFiltered['szUsername']),
mysql_escape_string($aFiltered['szPassword']))
|
You didn't use username in the $aFilterOptions.
But anyways, does the fieldname have to the same as the ones used in the array $aFilterOptions ??
So if you have
PHP Code:
$aFilterOptions = array(
'email' => blablabla whatever
);
You have to have like this?
HTML Code:
<form action="ble.php" method="POST">
<input type="text" name="email">
</form>
??
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|