php防注入留言板(simple)
新手学php,试手案例便是留言板。以前未连接数据库时,我是直接将用户输入的留言写入到一个txt,然后再从txt读取显示(~.~别鄙视)。 最近学习了php访问MySQL数据库的一些知识,重写了一下留言板,功能比较简单,当学习记录。 1.首先是提交留言的表单guessbo
新手学php,试手案例便是留言板。以前未连接数据库时,我是直接将用户输入的留言写入到一个txt,然后再从txt读取显示(~.~别鄙视)。
最近学习了php访问MySQL数据库的一些知识,重写了一下留言板,功能比较简单,当学习记录。
1.首先是提交留言的表单guessbook.php,提交至post.php。
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> ?>
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>?>
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>
User | Content | Time |
---|---|---|
echo $row[1] ?> | echo nl2br($row[3]) ?> | echo $row[4] ?> |
5.基本结果: 经CSS 修饰后的结果如下:
http://www.phptogether.com/archives/10907

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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