PHP displays user input in a pattern

Source: Internet
Author: User
You can download the files attached to this document on this page, or download this document in the character processing section of the file download to describe how to securely display structured user input. We will discuss the danger of output without filtering, and giveYou can download the files attached to this document on this page, or download this document in the character processing section of the file download to describe how to securely display structured user input. We will discuss the danger of output without filtering, and provide a safe way to display output in a pattern.

No risk of filtering output

If you only get the user's input and display it, you may damage your output page. for example, some people may maliciously embed javascript scripts in the input box they submit:

This is my comment.
<Script language = 'javascript:
Alert ('Do something bad here! ')'>.

In this way, the user is not malicious and may damage some HTML statements, such as a table suddenly interrupted or the page display is incomplete.


Only displays unformatted text

This is the most simple solution. you only need to display the information submitted by the user as a text without layout. Use the htmlspecialchars () function to convert all characters into HTML encoding.

For example, <B> This ensures that no unexpected HTML tags are output when they are not appropriate.
This is a good solution. if your users only pay attention to text content without a pattern. However, it will be better if you give some talent that can be structured.
Formatting with Custom Markup Tags
User-defined tagging

You can provide a special tag to the user application. for example, you can promise the application [B]... [/B] aggravated display, [I]... [/I] italic display. in this case, you can simply change the search and control: $ output = str_replace ('[B]', '<B>', $ output );
$ Output = str_replace ('[I]', '<I>', $ output );

In addition, we can use the user key to enter some links. For example, the user will promise to input [link = 'URL']... [/link], and we will convert it to the <a href = ''>... </a> statement.

At this time, we cannot apply a simple search replacement, and we should apply a regular expression for replacement:
$ Output = ereg_replace ('\ [link =' ([: graph:]) '\]', '<a href =' \ 1 '> ', $ output );

The fulfillment of ereg_replace () is:
Search for the string that appears [link = '...'] and replace it with <a href = '...'>.
[[: Graph:] indicates any non-null characters. For more information about regular expressions, see related articles.


The format_output () function in outputlib. php provides the conversion of these tags. the general principle is:
Call htmlspecialchars () to convert the HTML tag to a special encoding, and filter out the HTML tag that should not be displayed,
Then, convert a series of custom tags into corresponding HTML tags.
See the following source code:
<? Php


Function format_output ($ output ){
/*************************************** *************************************
* Takes a raw string ($ output) and formats it for output using a special
* Stripped down markup that is similar to HTML
**************************************** ************************************/

$ Output = htmlspecialchars (stripslashes ($ output ));

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.