Author Topic: Avoid "hacking" on text field forms? .. NO html allowed or anything else?  (Read 786 times)

rrgerman

  • Regular Member
  • **
  • Posts: 29
  • Karma: 0
    • View Profile
On my website, user can edit their profile through a form that allows them to enter their age, name, location and information about them....

I was wondering how can i avoid javascript, html or any type of coding and just make them plain text?

Because i was visiting a site one day and a pop up message appeared with a dumb text, and then i researched and found out that this person used  <script type="text/javascript">alert("blah");</script>

I dont want to happen on my website because that is just a simple alert box but people with more knowledge could do more harm than that

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 618
  • Karma: 9
    • View Profile
Re: Avoid "hacking" on text field forms? .. NO html allowed or anything else?
« Reply #1 on: September 27, 2011, 02:02:39 PM »
Hi there,

In the php that deals with the form submission, clean the $_POST variables with addslashes() and strip_tags() before storing them in your database

rrgerman

  • Regular Member
  • **
  • Posts: 29
  • Karma: 0
    • View Profile
Re: Avoid "hacking" on text field forms? .. NO html allowed or anything else?
« Reply #2 on: September 27, 2011, 04:11:35 PM »
Thank you very much! And last question. When it's a text area, multi-line. How can I make it display with rows in case the user hits tab and writes a description with paragraphs? Right now it just displays it as a whole chunk all together

rrgerman

  • Regular Member
  • **
  • Posts: 29
  • Karma: 0
    • View Profile
Re: Avoid "hacking" on text field forms? .. NO html allowed or anything else?
« Reply #3 on: September 27, 2011, 04:12:43 PM »
I man, when the user hits ENTER

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 618
  • Karma: 9
    • View Profile
Re: Avoid "hacking" on text field forms? .. NO html allowed or anything else?
« Reply #4 on: September 27, 2011, 06:01:15 PM »
You can use a php function nl2br:

PHP Code: [Select]
echo nl2br($_POST['textarea']);

rrgerman

  • Regular Member
  • **
  • Posts: 29
  • Karma: 0
    • View Profile
Re: Avoid "hacking" on text field forms? .. NO html allowed or anything else?
« Reply #5 on: September 27, 2011, 06:11:01 PM »
AWESOME!,  ;D ;D ;D ;D ;D

You rock! thanks!