Consider using strip_tags on the message before you store it:
// strip out tags
$store_me = strip_tags($_POST["message"]);
// this is your previous technique
$store_me = nl2br(htmlentities($store_me));
I would also add that you should consider adding a Cross-Site Request Forgery (CSRF) Token to your form. I.e., store a randomly generated token in session (or cookie) and also display this token as a hidden input in your form so that it gets submitted with the data.
Then, in the page that handles the POST operation, make sure the POSTed value of the CSRF token matches the one in session (or cookie).
There's a SO post on preventing CSRF here.
EDIT: in response to your extra comment, here's some more info.
You could also remove the entire script with this function, but it won't catch every script. If the script contains the "<" char, for instance.
$str = "
Hello World
* One
* Two
<script>alert('hello')</script>
a whole bunch of data here
blah blah
and OH MY another script
<script type=\"text/javascript\">
alert('hello');
</script>
<script type=\"text/javascript\">
var sneaky = \"<\";
alert('hello');
</script>
";
function remove_script_tags($str) {
return preg_replace('#<[^>]*script[^>]*>[^<]+</[^>]*script[^>]*>#sm', '', $str);
}
echo remove_script_tags($str) . "\n";
You could use this function to detect scripts which should work pretty well at detecting script attempts, but might also detect well-intentioned posts like this very post you are reading right now.
function contains_script_tag($str) {
return preg_match('#<[^>]*script[^>]*>#sm', $str);
}
Both of these scripts might also catch a long, but harmless, post like this:
here is a harmless line 2 < 3
this line casually mentions "script"
oh and another harmless line 4 > 3