Necuima Posted August 29, 2013 Share Posted August 29, 2013 Hi, Today someone tried to inject some invalid data into a contacts database table. The data fields are just VARCHARs. Fortunately I had enough checks in there to prevent the insertion. But I hastily put together the following PHP data checks to try and strengthen the defences. Are they sufficient? Is there a better way? Note: there are no particularly sensitive data in this database but I'd like to prevent any 'bad' data being put into the database table. function test_for_invalid_data($data) { // the following are 'needle in haystack' tests - stristr does a case-insensitive check if ((stristr($data, 'href') === FALSE) && (stristr($data, 'http') === FALSE) && (stristr($data, '<a') === FALSE) && (stristr($data, 'link') === FALSE) && (stristr($data, 'url') === FALSE) && (stristr($data, '</a') === FALSE)) { // 'needle' strings not found - good! return TRUE; } else return FALSE; } Any advice will be most appreciated. Necuima. Link to comment Share on other sites More sharing options...
HartleySan Posted August 29, 2013 Share Posted August 29, 2013 I think you're thinking of XSS (cross-site scripting), not SQL injection. To start, I'd recommend doing a YouTube search for "XSS" and "SQL injection", watching a few videos, and then coming back here if you have any questions. Thanks. Link to comment Share on other sites More sharing options...
Larry Posted August 29, 2013 Share Posted August 29, 2013 If you're just looking for characters that might reflect spam or a hack attempt, I'd included "script" and "img". Link to comment Share on other sites More sharing options...
Necuima Posted August 29, 2013 Author Share Posted August 29, 2013 Thank you both for your advice - I will do as you both suggest. I forgot to mention that I also always use Larry's 'escape-data' process. Cheers from Oz. Link to comment Share on other sites More sharing options...
Recommended Posts