Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-09-2005, 06:56 PM   PM User | #1
ClubCosmic
Regular Coder

 
Join Date: May 2004
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
ClubCosmic is an unknown quantity at this point
PHP Form Validation (Discussion)

Hi everyone,

Although I use methods for form validations, I was wondering how do you go about validating textareas? Is it possible to prevent sql injection attacks when your form contains textareas for user comments?

Hope this makes a good thread, perhaps we can all learn something.

c.c.
ClubCosmic is offline   Reply With Quote
Old 07-09-2005, 07:24 PM   PM User | #2
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
I don't see how textareas are any different to other form elements that allow for user input. Generally I find addslashes() is fine for my requirements
__________________
~ Bazzy
delinear is offline   Reply With Quote
Old 07-09-2005, 07:43 PM   PM User | #3
ClubCosmic
Regular Coder

 
Join Date: May 2004
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
ClubCosmic is an unknown quantity at this point
How does using addslashes() benefit you when you're validating text in your forms?

Last edited by ClubCosmic; 07-09-2005 at 08:39 PM..
ClubCosmic is offline   Reply With Quote
Old 07-09-2005, 10:43 PM   PM User | #4
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
I meant regarding sql injection attacks. As for validation... well, it depends what the data is that I'm validating and what criteria that data has to meet.
__________________
~ Bazzy
delinear is offline   Reply With Quote
Old 07-09-2005, 11:10 PM   PM User | #5
ClubCosmic
Regular Coder

 
Join Date: May 2004
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
ClubCosmic is an unknown quantity at this point
I'm just using it to echo user comments

do you think validating this sort of info nessecary?

Last edited by ClubCosmic; 07-09-2005 at 11:14 PM..
ClubCosmic is offline   Reply With Quote
Old 07-09-2005, 11:14 PM   PM User | #6
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
Well in that case you probably don't need any validation more advanced than checking that some text was entered? I just use trim() (to make sure they didn't enter just whitespaces) and empty() to do that:
PHP Code:
$_POST['textarea'] = (isset($_POST['textarea']) ? trim($_POST['textarea']) : '');
// this will set $_POST['textarea'] to empty if it's unset or if only whitespaces were entered by the user

if(empty($_POST['textarea'])) {
    
// this tests if the value is empty, if it is, I return the user to the form and flag the textarea as requiring text

__________________
~ Bazzy
delinear is offline   Reply With Quote
Old 07-09-2005, 11:20 PM   PM User | #7
ClubCosmic
Regular Coder

 
Join Date: May 2004
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
ClubCosmic is an unknown quantity at this point
thank you, even though user comments are optional i wanted to make sure i wasnt presenting a loophole for some sort os sql attack.

so when i add user commments i should use addslashes() to prevent sql injection attacks.

a little paranoia is healthy sometimes.

Last edited by ClubCosmic; 07-09-2005 at 11:23 PM..
ClubCosmic is offline   Reply With Quote
Old 07-10-2005, 12:24 AM   PM User | #8
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
If you're saving them in a database, then yes, addslashes() will escape any dangerous characters for you and when you come to display it back in the browser just use stripslashes() so that users don't see ugly escape characters.

If you have magic quotes enabled in PHP then the server will automatically addslashes to all $_POST, $_GET and $_COOKIE data for you though, so it's worth checking if this is enabled first because escaping data twice will just give you headaches.

Paranoia is definitely a good trait where this stuff is concerned
__________________
~ Bazzy
delinear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 05:27 AM.


Advertisement
Log in to turn off these ads.