Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Design Forum > Web Design & Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Web Design & Programming Discussion of web design, and server-side & client-side scripting

Reply
 
LinkBack Thread Tools
Old 06-22-2009, 11:05 AM   #1 (permalink)
Design Team Member
 
Redcore's Avatar
 
Join Date: Aug 2007
Location: Jamestown, CA
Posts: 713
OS: Linux Mint 7

My System

PHP: Sanitizing Forms

Security is always a big deal and something we don't bring up very often on this forum as this is largely a help forum, not a tutorial site...and it's incumbent on each developer to ensure their code is safe rather than the moderators/helpers here holding their hand through everything. That said, some guidance is always nice too :) There are tons of articles out there, so I'm not going to go crazy with examples - and this isn't designed to be an "end-all" security post, but rather a few nifty articles and a start of discussion for others as well.

One big deal is sanitizing ANYTHING that is passed from a user on your site. I'm not a security expert as I don't deal as much with security things these days since I largely work on an intranet system for my office, but there are fundamental things everyone should do regardless. PHP5 has a great new function called "filter_var" and there are tons of filter flags you can put into it to do a myriad of things. Here is a great article on TutsPlus regarding this function:
http://net.tutsplus.com/tutorials/ph...h-php-filters/

If you have the access rights to do so, you should turn off magic_quotes (addslashes/removeslashes manually is *probably* best - extra work, but extra control) as well as register_globals. If you like, you may also want to turn errors off that way "villains" can't see what is wrong with your code if things aren't working properly and don't have clues on how to use that against you. This is probably easier to do if you have a secondary server that you can test on. I don't run things that way currently, so I have errors on (although turned down quite a bit).

SQL injection is one of the worst things that can happen to your site. It not only affects your site, but also the integrity of the data in your database. If you use MySQL, you can use the mysqli_real_escape_string function to escape special characters and therefore sanitize the form element:

PHP Code:
$formfield mysqli_real_escape_string($_POST['formfield']); 
For work, we exclusively use MS SQL, so I filter all user vars with PHP's filter_var...
PHP Code:
function runFilter($var)
    **
    
$var str_replace("'""''"$var);
    
$var filter_var($varFILTER_SANITIZE_STRINGFILTER_SANITIZE_MAGIC_QUOTES);
    return 
$var;
    }

$_POST array_map('runFilter'$_POST); 
I include this with all form handler pages. It's a relatively new thing I've put in, so it may be kinda weird for some. I should have commented it more - I'm not sure why I did the string replace - I'm sure there's a reason though.

Another thing you can do - especially with comment/contact forms, is utilizing captcha. Here's a popular library:
http://recaptcha.net/plugins/php/

Some developers flat out do not like captcha, don't want anything to do with it, and regularly encourage others not to bother with it. My opinion is that if it keeps even 40% of bots out, it's worth it - so long as it doesn't destroy the user's experience on the site either.

That's about all I have for now. I'm more interested in others contributing more rather than trying to put a ton of stuff up... :P
__________________
Free Resources
PC Protection - Comodo Firewall | AVG Anti-Virus | WinPatrol | Ad-Aware | Spybot S&D | SpywareBlaster |
Web Design/Programming - KompoZer
(Editor) | Paint.NET (Graphic) | GIMP+GIMPShop (Graphic) | FileZilla (FTP Client) | Free Hosting |
Redcore is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 06-23-2009, 08:45 AM   #2 (permalink)
Registered User
 
neonjuice's Avatar
 
Join Date: Dec 2008
Location: Minnesota
Posts: 113
OS: Vista Ultimate 64 bit SP1. Linux Ubuntu, XP Pro


Re: PHP: Sanitizing Forms

Thank you very much. I am still learning PHP inside out and this really helps. Thank you.
neonjuice is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


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




All times are GMT -7. The time now is 12:44 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85