Generally you need to avoid two attacks: sql injection and html injection.
sql injection is basically the idea, that somebody highjacks your database query, which can happen if you don't escape special characters, mostly the ' which is used to encapsulate strings in mysql. to make sure that's taken care of, use placeholders:
my $sth_insert = $dbh->prepare("INSERT INTO my_table (field1, field2) VALUES (?, ?)");
$sth_insert->execute( $value_for_field1, $value_for_field2 );
and DBI will take care of it for you.
html / code injection should, imho not be defended against by deleting or replacing certain characters in the messages before you enter them into the database, but rather to replace them when you output them. I'd use HTML::Entities. Also, most templating systems have an easy way to escape that.
Setting a Cookie will not defend you against a malicious person that is acting out the attacks himself, but it will help protect against form attacks on your users, where people unknowingly submit a form on your website. there are other measures as well -- how far you go really depends on how much trouble you expect and how many people you think are out to hurt you / your users ;)