Trim, stripslashes, htmlspecialchars functions

Source: Internet
Author: User
Validating form data with PHP

The first thing we want to do is pass all the variables through the PHP htmlspecialchars () function.

After we use the Htmlspecialchars () function, if the user tries to submit the following in the text field:

<script>location.href (' http://www.hacked.com ') </script>

-The code will not execute because it will be saved as an escape code, like this:

&lt;script&gt;location.href (' http://www.hacked.com ') &lt;/script&gt;

Now this code appears to be safe on the page or in an e-mail message.

When the user submits the form, there are two things we need to do:

(via the PHP trim () function) removes unnecessary characters from user input data (extra spaces, tabs, line breaks)

Remove backslash (\) from user input data (via PHP stripslashes () function)

Next we create a check function (which is more efficient than writing the code over and over again).

We name the function Test_input ().

Now we can examine each $_post variable through the Test_input () function, which is the script:

Instance

<?php//defines the variable and sets it to a null value $name = $email = $gender = $comment = $website = ""; if ($_server["request_method"] = = "POST") {  $ Name = Test_input ($_post["name"]);  $email = test_input ($_post["email"]);  $website = test_input ($_post["website"]);  $comment = Test_input ($_post["comment"]);  $gender = Test_input ($_post["Gender");} function Test_input ($data) {  $data = trim ($data);  $data = Stripslashes ($data);  $data = Htmlspecialchars ($data);  return $data;}? >
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.