Reputation: 57
I am trying to prevent xss injection. So before I submit a form, a javascript function is called
function validatefield(id) {
var description = document.getElementById(id).value;
description = description.replace(/[\"\'][\s]*javascript:(.*)[\"\']/gi, "");
description = description.replace(/script(.*)/gi, "");
description = description.replace(/eval\((.*)\)/gi, "");
document.getElementById(id).value=description;
}
I am wonderng if there's a way to do the same in php before inserting into the mysql? if they get around of the validatefield function.
Thanks
Upvotes: 1
Views: 3219
Reputation: 284816
Generally speaking, you can use preg_replace for regex replacements in PHP. But there are a few problems with your design
Generally speaking, if you want to allow some form of HTML, a whitelist is a better approach. HTML Purifier is a popular tool for implementing this in PHP.
Upvotes: 3
Reputation: 1356
You are looking for preg_replace
.
$description = preg_replace('regex pattern', 'regex replacement', $description);
Upvotes: 5