PHP special character escapes

Source: Internet
Author: User
    1. $html = Array ();
    2. $html [' username '] = htmlentities ($clean [' username '], ent_quotes, ' utf-8 ');
    3. echo "

      Welcome back, {$html [' username ']}.

      ";
    4. ?>
Copy Code

Tip the Htmlspecialchars () function is basically the same as the htmlentities () function, whose parameter definitions are identical, except that the escape of htmlentities () is more thorough. by $html[' username ' to output the username to the client, you can ensure that the special characters are not interpreted by the browser incorrectly. If the username contains only letters and numbers, it is not necessary to actually escape, but this embodies the principle of deep defense. Escaping any output is a very good habit and it can dramatically improve the security of your software.

Another common output target is the database. If possible, the data in the SQL statement needs to be escaped using PHP built-in functions.

For MySQL database users, the best escape function is mysql_real_escape_string (). Addslashes () is the last option if you are using a database that does not have PHP built-in escape functions available.

For example, the correct escape technique for MySQL database:

    1. $mysql = Array ();
    2. $mysql [' username '] = mysql_real_escape_string ($clean [' username ']);
    3. $sql = "SELECT *
    4. From profile
    5. where username = ' {$mysql [' username ']} ';
    6. $result = mysql_query ($sql);
    7. ?>
Copy Code

PHP Filter Parameters Special characters anti-injection PHP filter illegal and special string method PHP instance: Special Character Handler example replace PHP code with special characters in super long text

  • 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.