Display formatted user input in PHP_PHP Tutorial
我们将讨论没有经过过滤的输出的危险,给出一个安全的显示格式化输出的方法。 没有过滤输出的危险 This is my comment. <script language="javascript: alert(Do something bad here!)">. 这是一个好的解决方案,如果你的用户只关注没有格式的文本内容。但是,如果你给出一些可以格式化的能力,它将更好一些。 Formatting with Custom Markup Tags 用户自己的标记作格式化 你可以提供特殊的标记给用户使用,例如,你可以允许使用[b]...[/b]加重显示,[i]...[/i]斜体显示,这样做简单的查找替换操作就可以了: $output = str_replace("[b]", "<b>", $output); $output = str_replace("[i]", "<i>", $output); 再作的好一点,我们可以允许用户键入一些链接。例如,用户将允许输入[link="url"]...[/link],我们将转换为<a href="">...</a>语句 这时,我们不能使用一个简单的查找替换,应该使用正则表达式进行替换: $output = ereg_replace([link="([[:graph:]]+)"], <a href="1">, $output); ereg_replace()的执行就是: 查找出现[link="..."]的字符串,使用<a href="..."> 替换它 [[:graph:]]的含义是任何非空字符,有关正则表达式请看相关的文章。 在outputlib.php的format_output()函数提供这些标记的转换,总体上的原则是:中国网管联盟bitsCN.com 调用htmlspecialchars()将HTML标记转换成特殊编码,将不该显示的HTML标记过滤掉,然后,将一系列我们自定义的标记转换相应的HTML标记。 请参看下面的源代码: <?php /**************************************************************************** 一些注意的地方: 记住替换自定义标记生成HTML标记字符串是在调用htmlspecialchars()函数之后,而不是在这个调用之前,否则你的艰苦的工作在调用htmlspecialchars()后将付之东流。网管bitscn_com 在经过转换之后,查找HTML代码将是替换过的,如双引号"将成为" nl2br()函数将回车换行符转换为<br>标记,也要在htmlspecialchars()之后。 当转换[links=""] 到 <a href="">, 你必须确认提交者不会插入javascript脚本,一个简单的方法去更改[link="javascript 到 [link=" javascript, 这种方式将不替换,只是将原本的代码显示出来。 outputlib.php 在浏览器中调用test.php,可以看到format_output() 的使用情况 正常的HTML标记不能被使用,用下列的特殊标记替换它: - this is [b]bold[/b] [p]Paragraph These are just a few tags, of course, you can add more tags according to your needs. Network Management Alliance bitsCN@com Conclusion Conclusion This discussion provides methods for safely displaying user input, which can be used in the following programs Message Board
如果你仅仅获得用户的输入然后显示它,你可能会破坏你的输出页面,如一些人能恶意地在他们提交的输入框中嵌入javascript脚本:
这样,即使用户不是恶意的,也会破坏你的一些HTML的语句,如一个表格突然中断,或是页面显示不完整。
只显示无格式的文本
这是一个最简单的解决方案,你只是将用户提交的信息显示为无格式的文本。使用htmlspecialchars()函数,将转化全部的字符为HTML的编码。
如<b>将转变为,这可以保证不会有意想不到的HTML标记在不适当的时候输出。
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));
/* new paragraph */
$output = str_replace([p], <p>, $output);
/* bold */
$output = str_replace([b], <b>, $output);
$output = str_replace([/b], </b>, $output);
/* italics */
$output = str_replace([i], <i>, $output);
$output = str_replace([/i], </i>, $output);网管bitscn_com
/* preformatted */
$output = str_replace([pre], <pre>, $output);
$output = str_replace([/pre], </pre>, $output);
/* indented blocks (blockquote) */
$output = str_replace([indent], <blockquote>, $output);
$output = str_replace([/indent], </blockquote>, $output);
/* anchors */
$output = ereg_replace([anchor="([[:graph:]]+)"], <a name="1"></a>, $output);
/* links, note we try to prevent javascript in links */
$output = str_replace([link="javascript, [link=" javascript, $output);
$output = ereg_replace([link="([[:graph:]]+)"], <a href="1">, $output);
$output = str_replace([/link], </a>, $output);
return nl2br($output);
}
?>
- this is [i]italics[/i]
- this is [link="http://www.phpbuilder.com"]a link[/link]
- this is [anchor="test"]an anchor, and a [link="#test"]link[/link] to the anchor
[pre]Pre-formatted[/pre]
[indent]Interleaved text[/indent]
User Suggestions
System Announcement
BBS System

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment