| | |||||
| ||||||||
| CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| Senior Member Join Date: Apr 2005 Location: Joliet, Illinois
Posts: 1,177
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | [TUTORIAL] PHP - Security with Cookies Another quick tutorial I decided to write not so long after my last one. A question (or comment really) I always here is, "Why do you use cookies? They're so insecure." Even though cookies are clear text and can be easily altered, this is not necessarily true. If your code checks in a somewhat redundant fashion, you should have no security issues at all! Well, let's start with a functions.php file to create the login/logout functions and we'll work our way from there ![]() This is more of an intermediate tutorial so I'll be assuming you know the basics of PHP at the bare minimum. So, what we'll do here (examine the code to figure out exactly) will be calling the cookie and during our "check" function will be checking the cookie redundantly against the database, etc. functions.php PHP Code: index.php PHP Code: To test this script, use this simple database.sql file for the database structure used here: Code: CREATE TABLE users ( `id` BIGINT(10) NOT NULL AUTO_INCREMENT, `name` VARCHAR(32) NOT NULL, `pass` VARCHAR(32) NOT NULL, PRIMARY KEY (`id`) ); so contact me with anymore questions!-RageD |
| |
| | #2 (permalink) |
| NamePros Regular Join Date: Dec 2004 Location: Penang, Malaysia
Posts: 874
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Thanks and rep added.
__________________ █ Hostegic - Premium cPanel Hosting [Softaculous | RvSitebuilder | R1Soft daily backups | LiteSpeed] █ [UK] [Linux - cPanel | Windows - WebsitePanel/Plesk] [Shared Hosting | Reseller Hosting | VPS] |
| |
| | THREAD STARTER #6 (permalink) |
| Senior Member Join Date: Apr 2005 Location: Joliet, Illinois
Posts: 1,177
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Sure, glad to be of some service to the community! Please, if anyone finds any holes let me know so I can patch them Hack away at this script lol it's encouraged. Security means a lot on the web ![]() -RageD |
| |
| | #7 (permalink) |
| NamePros Regular Join Date: Apr 2006
Posts: 363
![]() ![]() ![]() ![]() | Thanks for sharing You'll want to make sure you sanitize the cookie data sent as well as the username and password variables prior to sending to prevent sql injections.I haven't tested it but passing a password variable to this script as: Code: null' OR id='1 Code: SELECT * FROM users WHERE name='admin' AND pass='null' OR id='1' LIMIT 1; ????: NamePros.com http://www.namepros.com/showthread.php?t=508547 Jay
__________________ Canadian Domain Registrar Ready.ca |
| |
| | #8 (permalink) | ||||
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,069
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | I would certainly contest this being secure. You should NEVER put passwords in a cookie even if you MD5 it. If someone sees you have MD5'd something they can find out what you have MD5's quite easily (within a couple of hours) on any modern PC. And working out it is MD5'd is very easy considering the length of an MD5'd string. As you are providing the username and password MD5'd in the cookie any one who manages to cause an MD5 collission can now log into your site when they get the cookie information.
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft | ||||
| |
| | #9 (permalink) |
| Senior Member Join Date: Jun 2007 Location: NamePros.com
Posts: 1,400
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Peter is right. This is far from being secure. A good authentication system NEVER stores passwords or any sensitive information in cookies, whether they are encrypted or not. Also guys remember when members register on your sites you should always add salt to any encrypted passwords. |
| |
| | THREAD STARTER #10 (permalink) | ||||||||
| Senior Member Join Date: Apr 2005 Location: Joliet, Illinois
Posts: 1,177
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
????: NamePros.com http://www.namepros.com/showthread.php?t=508547 -RageD | ||||||||
| |