简体   繁体   中英

$_POST'value can only be used for one time?

post-data.php cantain a form to post user'name into show-post-data.php .
post-data.php

<form method="post" action="show-post-data.php">
user:<input type="text" name="user"></input>
        <input type="submit" name="submit" value="submit">
</form>

Only one command var_dump($_POST); in show-post-data.php

<?php
var_dump($_POST);
?>

1.click 127.0.0.1/post-data.php in browser.
2.input tom and click submit button.
3.click 127.0.0.1/show-post-data.php in browser.
We get output as below:

array(2) { ["user"]=> string(3) "tom" ["submit"]=> string(6) "submit" }

4.click 127.0.0.1/show-post-data.php in browser for second time.

Nothing as output. $_POST is empty now.
$_POST'value can only be used for one time?

This is because of the HTTP protocol is stateless, which means that the information that you send in a request will be only available for that request (unless you store it in the server).

When you send information in a POST, that arrives to the server (to your PHP code) in the variable $_POST , you can use in in that request. In the next page load, that variable will be empty unless you send new information again. That is the correct behavior.

$_POST value can only be used for one time?

NO , It can be used several time but it should be only available in the page to where it is posted.

If you posted the value to a specific URL, the posted variable can only be accessed in that specified URL for that time. Not in other URL's or for other time.

If you need to used in other sections/pages, you have to save the value or have to store in session or cookie.

if(isset($_POST) && count($_POST)) { 
  $_SESSION['post'] = $_POST; 
}
if(isset($_SESSION['post']) && count($_SESSION['post'])) { 
  $_POST = $_SESSION['post']; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM