Jump to content

easiest and best way to prevent javascript in a POST field


LanceT

Recommended Posts

you've lost me, please explain a bit clearer on what your trying to achieve.

 

I have a text field where I'm going to allow user input and send it to the database. I want to prevent XSS so If they enter javascript, it will just show the javascript in text instead of executing it.

Link to comment
Share on other sites

I looked at strip_tags and I'm a bit confused. I want to be able to let my users use basically all HTML except for anything that is javascript. If I use strip_tags I would have to write a bunch of code in the syntax to account for all the code that I want to allow the user to have. Is there like an opposite version of strip_tags?

 

string strip_tags ( string $str [, string $allowable_tags] )

 

I want code that is

 

string strip_tags ( string $str [, string $NOTallowable_tags] )

 

Is there anything like this?

Link to comment
Share on other sites

Well, for that preg_replace is your best bet:

 

$html = preg_replace ("/<script/?>/", "", $html);

 

That basically says, strip out anything that has a '<' and 'script' and maybe a '/' followed by a '>'.

Regular expressions rule!

 

A function to do the strip_tags:

 

function strip_bad_tags($html) {
  $allowed = "<h1><h2><h3><h4><div><p><b><i><u><br><img><a><table><tr><td><th><ol><li><ul><span><strong>";
  return strip_tags($html, $allowed);
}

 

Just change the allowed tags to the tags you want to allow and you should be sorted.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.