WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2007, 10:34 AM
edhan's Avatar
edhan edhan is offline
WebProWorld Veteran
 

Join Date: Aug 2003
Location: Singapore
Posts: 509
edhan RepRank 1
Question Check data before insert database

Hi

I hope I am asking this question in the right forum.

I am having a text description input by user to insert into database but I want to avoid symbol like </>. So how do I check using php to reject when the user input these in the text description?

Appreciate any help or advice.

Thanks!
__________________
Receiving & Giving To Society
Thai Buddha Amulets & Tibetan dZi Beads | Ads Revenue Sharing | Shopping Online
Reply With Quote
  #2 (permalink)  
Old 11-07-2007, 04:26 PM
imvain2 imvain2 is offline
WebProWorld Pro
 

Join Date: Apr 2004
Posts: 266
imvain2 RepRank 0
Default Re: Check data before insert database

Depending on what you really want to do, you have a few options.

You could always use strip_tags to remove those tags.

You could simply do a string replace and replace < with the &ltX and the > with &gtX (I didn't know if the forum would show my character entities correctly or not so replace the X with a semi colon.

Or if you really want to reject, simply use the strpos to detect the exact symbols, if they are found, redirect the user with a message telling them they entered bad characters or if the text is clean then add to the database.
Reply With Quote
  #3 (permalink)  
Old 11-07-2007, 04:57 PM
niggles niggles is offline
WebProWorld Member
 

Join Date: Oct 2005
Posts: 31
niggles RepRank 1
Default Re: Check data before insert database

If you're doing it for security reasons then it gets a lot harder as there's numerous ways of encoding those characters which will bypass most filtering techniques - check out some XSS tutorials to see just how many there are *sigh*.

I agree with imvain2 that strip_tags is probably the easiest way of stripping the tags out.

To be really safe though you could do a regex which only allows Alpha and Numeric characters to remain.

Cheers,
Niggles
__________________
-------------------------------------------------
World Music World - bringing the World's Folk Music Cultures Together
http://www.worldmusicworld.com/
-------------------------------------------------
Reply With Quote
  #4 (permalink)  
Old 11-07-2007, 09:54 PM
edhan's Avatar
edhan edhan is offline
WebProWorld Veteran
 

Join Date: Aug 2003
Location: Singapore
Posts: 509
edhan RepRank 1
Default Re: Check data before insert database

Quote:
Originally Posted by imvain2 View Post
Depending on what you really want to do, you have a few options.

You could always use strip_tags to remove those tags.

You could simply do a string replace and replace < with the &ltX and the > with &gtX (I didn't know if the forum would show my character entities correctly or not so replace the X with a semi colon.

Or if you really want to reject, simply use the strpos to detect the exact symbols, if they are found, redirect the user with a message telling them they entered bad characters or if the text is clean then add to the database.
Yes, I am interested in knowing how to use the strpos to detect the exact symbols and redirect user to a message that they have entered bad characters and to re-enter them again. As I am not very familiar of how to write this, appreciate if you can give me the sample code where I can use to implement this checking. Sorry if I am asking too much as I am totally blurred on this.

Thanks!
__________________
Receiving & Giving To Society
Thai Buddha Amulets & Tibetan dZi Beads | Ads Revenue Sharing | Shopping Online
Reply With Quote
  #5 (permalink)  
Old 11-07-2007, 10:07 PM
imvain2 imvain2 is offline
WebProWorld Pro
 

Join Date: Apr 2004
Posts: 266
imvain2 RepRank 0
Default Re: Check data before insert database

I agree with niggles, you may want remove the tags, or if you need to detect then I would do a mix of solutions due to the XSS.

Please note that I haven't tested this code out, I just changed around the info from PHP: strpos - Manual.

Code:
$haystack = $_POST["textinput"];
$haystack = str_replace("<", "&lt;", $haystack);
$haystack = str_replace(">", "&gt;", $haystack);

$needle   = '&lt;/&gt;';
$pos = strpos($haystack, $needle);

if ($pos === false) {
    //add to db
} else {
   echo "Please remove those tags and try again";
}
Reply With Quote
  #6 (permalink)  
Old 11-07-2007, 10:18 PM
edhan's Avatar
edhan edhan is offline
WebProWorld Veteran
 

Join Date: Aug 2003
Location: Singapore
Posts: 509
edhan RepRank 1
Default Re: Check data before insert database

Hi imvain2

Thanks for the code, I will try it to see if it works and let you know.
__________________
Receiving & Giving To Society
Thai Buddha Amulets & Tibetan dZi Beads | Ads Revenue Sharing | Shopping Online
Reply With Quote
  #7 (permalink)  
Old 11-07-2007, 10:43 PM
edhan's Avatar
edhan edhan is offline
WebProWorld Veteran
 

Join Date: Aug 2003
Location: Singapore
Posts: 509
edhan RepRank 1
Default Re: Check data before insert database

Hi imvain2

The code works! I need to add a slash for for haystack to make it works.

Thanks a million!

Code:
$haystack = $_POST["textinput"];
$haystack = str_replace("</", "&lt;/", $haystack);
$haystack = str_replace(">", "&gt;", $haystack);

$needle   = '&lt;/&gt;';
$pos = strpos($haystack, $needle);

if ($pos === false) {
    //add to db
} else {
   echo "Please remove those tags and try again";
}
__________________
Receiving & Giving To Society
Thai Buddha Amulets & Tibetan dZi Beads | Ads Revenue Sharing | Shopping Online
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum


Thread Tools
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searchable Optin Database - Truly Targeted Data optinsearchdb Marketing Strategies Discussion Forum 0 02-14-2006 05:48 PM
Posting Form Data to an Access (or SQL) Database chrison600 Database Discussion Forum 2 07-21-2005 06:39 PM
Getting MS Access data into MySQL database Dragonsi Database Discussion Forum 1 04-06-2005 12:52 AM
Retriving matching list of data from two database thousandfive Database Discussion Forum 1 04-30-2004 11:31 AM
insert data into database jilshi Database Discussion Forum 2 04-30-2004 09:24 AM


Search Engine Friendly URLs by vBSEO 3.0.0