php 刷新后 并没有POST 却输出数据
php post 刷新
我的php代码如下://error_reporting(E_ALL & ~E_NOTICE);
function write_message($file_name, $message)
{
$fp = fopen($file_name, "a"); //以追加模式打开文件
if(flock($fp, LOCK_EX))
{
fwrite($fp, $message);
flock($fp, LOCK_UN);
}
else
{
echo "不能锁定文件
";
}
fclose($fp); //关闭文件资源
}
//自定义一个遍历读取文件的函数
function read_message($file_name)
{
$fp = fopen($file_name, "r"); //以只读模式打开文件
flock($fp, LOCK_SH);
$buffer = "";
while(!feof($fp))
{
$buffer .= fread($fp, 1024);
}
$data = explode("", $buffer);
unset($data[count($data)-1]);
foreach($data as $line)
{
$arr = explode("||", $line);
list($user_name, $title, $message) = $arr;
if($user_name != "" && $title != "" && $message != "")
{
echo $user_name.'说 ';
echo " ".$title.' ';
echo $message."
";
}
}
flock($fp, LOCK_UN);
fclose($fp);
}
//声明一个变量保存文件,在这个文件中保存留言信息
$file_name = "text_date.txt";
//判断用户是否提交按钮
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['user_name']) && !empty($_POST['title']) && !empty($_POST['mess']))
{
$message = $_POST['user_name']."||".$_POST['title']."||".$_POST['mess']."";
write_message($file_name, $message);
}
if (file_exists($file_name))
{
read_message($file_name);
}
?>
主界面如下:
添加了一条数据以后:
之后我不点击留言,直接按F5刷新,并没有POST,却在刷新后出现了相同的数据。如下图:
真的不明白啊,求解答啊……
回复讨论(解决方案)
if (file_exists($file_name))
{
read_message($file_name);
}
只要 $file_name 指向的文件存在就输出
并没有其他方式控制不输出
可在读的同时,却向 text_date.txt文件中写数据。当你留言后,不点留言按钮,就是每刷新一次,就会向文件中写入刚才你留言的数据,然后再输出。
表单提交后用header做一个重定向比较好。
刷新表单返回页相当于重新提交!
你用的是什么浏览器?刷新表单返回页都没有提示的吗?
用的是google浏览器,
下面是我的一个小测试,提交之后,刷新就不会出现先前的情况。
//error_reporting(E_ALL & ~E_NOTICE);
function write_message($message)
{
echo $message;
}
//判断用户是否提交按钮
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['user_name']) && !empty($_POST['title']) && !empty($_POST['mess']))
{
$message = $_POST['user_name']."||".$_POST['title']."||".$_POST['mess']."";
write_message($message);
}
?>
你这2段代码 提交后再刷新页面,都是会重复提交的!!
只不过第2段代码你没有将数据写入文件,所以你看到的都是第1次提交的数据!!
获取数据后可以跳转一下!
function write_message($message){echo $message;echo "<script>location.href='';</script>";exit;}
提交后做个跳转吧,没做跳转,一直停留在提交后那里,只要你刷新,数据就会重复提交。
太感谢你们了,我明白了……真心谢谢大伙……

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











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

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

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.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
