Php does not POST but outputs data after refreshing

Source: Internet
Author: User
Php POST refresh without post but output data

My php code is as follows:



Network Message Board


// Error_reporting (E_ALL &~ E_NOTICE );
Function write_message ($ file_name, $ message)
{
$ Fp = fopen ($ file_name, "a"); // open the file in append mode.
If (flock ($ fp, LOCK_EX ))
{
Fwrite ($ fp, $ message );
Flock ($ fp, LOCK_UN );
}
Else
{
Echo "file cannot be locked
";
}
Fclose ($ fp); // Close File Resources
}

// Customize a function to traverse and read files
Function read_message ($ file_name)
{
$ Fp = fopen ($ file_name, "r"); // open the file in read-only mode
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 );
}

// Declare a variable to save the file and save the message in this file
$ File_name = "text_date.txt ";
// Determine whether the user submits the button
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 );
}
?>




The main interface is as follows:

After adding a piece of data:

After that, I did not click the message and directly press F5 to refresh. there was no POST, but the same data was displayed after the refresh. For example:

I really don't understand. please answer it ......


Reply to discussion (solution)

If (file_exists ($ file_name ))
{
Read_message ($ file_name );
}
Output as long as the file pointed to by $ file_name exists
There is no other way to control and not output

While reading, you can write data to the text_date.txt file. After you leave a message, if you do not click the Message button, the data you just posted will be written to the file every time you refresh it, and then output.

It is better to use the header for redirection after the form is submitted.

Refreshing the form return page is equivalent to resubmitting!
What browser are you using? Is no prompt displayed when the return page of the refresh form is refreshed?

Google browser is used,
Below is a small test of mine. after submission, refresh will not show the previous situation.



Network Message Board


// Error_reporting (E_ALL &~ E_NOTICE );
Function write_message ($ message)
{
Echo $ message;
}


// Determine whether the user submits the button
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 );
}
?>



Refresh the page after you submit the two codes. they will be submitted again !!
However, you did not write data into the file in section 2nd of the code, so you can see 1st data submissions !!

After obtaining the data, you can jump to it!

function write_message($message){echo $message;echo "《script》location.href='';《script》";exit;}

Do a jump after submission. if you do not do a jump, you will stay there after submission. as long as you refresh the page, the data will be submitted again.

Thank you very much. I understand ...... Thank you ......

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.