Home php教程 php手册 php防注入留言板(simple)

php防注入留言板(simple)

Jun 06, 2016 pm 07:47 PM
php simple newbie Case injection message board

新手学php,试手案例便是留言板。以前未连接数据库时,我是直接将用户输入的留言写入到一个txt,然后再从txt读取显示(~.~别鄙视)。 最近学习了php访问MySQL数据库的一些知识,重写了一下留言板,功能比较简单,当学习记录。 1.首先是提交留言的表单guessbo

      新手学php,试手案例便是留言板。以前未连接数据库时,我是直接将用户输入的留言写入到一个txt,然后再从txt读取显示(~.~别鄙视)。

      最近学习了php访问MySQL数据库的一些知识,重写了一下留言板,功能比较简单,当学习记录。

  1.首先是提交留言的表单guessbook.php,提交至post.php。

Copy after login
class="button" value="提交"> class="button" value="重置">

  2.数据库的连接config.php

<span>php
    </span><span>$con</span> = <span>mysql_connect</span>("localhost","user","passwd"<span>);
    </span><span>if</span>(!<span>$con</span><span>)
        </span><span>die</span>('Could not connect: ' . <span>mysql_error</span><span>());

    </span><span>mysql_query</span>("set names utf8"); <span>//</span><span>以utf8读取数据</span>
    <span>mysql_select_db</span>("dbname",<span>$con</span>); <span>//选择</span><span>数据库</span>
?>
Copy after login

  3.对用户留言进行验证post.php

  在我们把数据插入数据库时,为了让我们输入的数据在显示时仍然保持最初的形式,就必须对数据进行一些处理。比如如果不转义直接显示,比如js代码,就会直接在你的页面中运行。

     这里用到了htmlspecialchars函数。不过htmlspecialchars不会转义换行符\r\n号,也就是说如果你插入的数据中包含了换行,当你进行转义显示时,该函数不会显示换行。所以在显示数据时需要用到nl2br函数

<span>php
    </span><span>header</span>("content-Type: text/html; charset=utf-8"<span>);
    </span><span>include</span>("config.php");                <span>//</span><span>数据库连接</span>
    <span>$name</span>= <span>$_POST</span>['name'<span>];
    </span><span>$email</span>= <span>$_POST</span>['email'<span>];
    </span><span>$patch</span> = <span>$_POST</span>['content'];           <span>//</span><span>获取游客提交信息</span>
    <span>if</span> (<span>$name</span>=="" || <span>$email</span>=="")          <span>//</span><span>游客未填写用户名和邮箱处理</span>
<span>    {
        </span><span>echo</span> "<script>alert('请检查用户名和邮箱输入!');location.href='./guessbook.php';</script>"<span>;
    }
    </span><span>else</span><span>
    {  
        </span><span>//</span><span> 对游客留言进行处理 </span>
        <span>$content</span> = <span>str_replace</span>("","<br>",<span>htmlspecialchars</span>(<span>$patch</span><span>));
        </span><span>//</span><span>插入数据库语句</span>
        <span>$sql</span> = "insert into content (name,email,content,time) values ('<span>$name</span>','<span>$email</span>','<span>$content</span>',now())"<span>;
        </span><span>mysql_query</span>(<span>$sql</span><span>);
        </span><span>//</span><span>插入成功,返回信息</span>
        <span>echo</span> "<script>alert('提交成功!返回查看!');location.href='./guessbook.php';</script>"<span>;
    }    
</span>?>
Copy after login

  4.展示用户留言列表,可在guessbook.php留言表单下面展示。

<span>include</span>("config.php"<span>);                //连接数据库
</span><span>$sql</span>="select * from content"<span>;         //数据库查询语句,content为该用户留言的表。
</span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span>,<span>$con</span><span>);

</span>
Copy after login
class="table table-responsive">

php while ($row=mysql_fetch_array($result)) { ?>

php } ?>
User Content Time
echo $row[1] ?> echo nl2br($row[3]) ?> echo $row[4] ?>

  5.基本结果: 经CSS 修饰后的结果如下:

php防注入留言板(simple)

http://www.phptogether.com/archives/10907

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

See all articles