TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-27-2007, 12:03 PM   #1 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default 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 :)
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-27-2007, 02:01 PM   #2 (permalink)
daz
The Contributor
Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
daz is on a distinguished road
Default

I have a lot of experience with PHP/MySQL logins. I'll try to write a tutorial for you later.
daz is offline  
Reply With Quote
Old 10-27-2007, 03:13 PM   #3 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

That would be great, thanks a lot :D
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-27-2007, 10:24 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
ReSpawN (12-02-2007)
Old 10-28-2007, 01:24 PM   #5 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

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.
Karl is offline  
Reply With Quote
Old 10-28-2007, 02:18 PM   #6 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

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.
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-29-2007, 10:39 AM   #7 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Anybody lol? :D
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-29-2007, 12:09 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-29-2007, 01:06 PM   #9 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

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.
Karl is offline  
Reply With Quote
Old 10-31-2007, 12:10 PM   #10 (permalink)
daz
The Contributor
Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
daz is on a distinguished road
Default

Hi Jmz,

Unfortunately I couldn't write a tutorial but you can try this script:

http://www.talkphp.com/showthread.php?p=3536
daz is offline  
Reply With Quote
Old 11-17-2007, 01:09 PM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Karl View Post
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>
??
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


All times are GMT. The time now is 10:18 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design