Browser back button restores empty fields

后端 未结 6 1430
小鲜肉
小鲜肉 2020-12-05 09:02

I have a web page x.php (in a password protected area of my web site) which has a form and a button which uses the POST method to send the form dat

6条回答
  •  无人及你
    2020-12-05 09:46

    While trying to further narrow down the problem, I've found the cause of my problem. I was using URLs which were being rewritten by Apache (i.e. I always accessed my page as http://foo.com/page which is mapped by Apache to http://foo.com/page.htm). Using the real URLs solved the problem and made IE7 happy, as long as I specify the proper HTTP header (Cache-Control, Expires, etc.).

    Here is what I do in the PHP code to output headers which seem to make all browsers happy with the cache:

    function emitConditionalGet($timestamp)
    {
        // See also http://www.mnot.net/cache_docs/
        // and code sample http://simonwillison.net/2003/Apr/23/conditionalGet/
    
        $gmdate_exp    = gmdate('D, d M Y H:i:s', time() + 1) . ' GMT';
        $last_modified = gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
        $etag          = '"'.md5($last_modified).'"';
    
        // If the client provided any of the if-modified-since or if-none-match
        // infos, take them into account:
    
        $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
                           ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
        $if_none_match     = isset($_SERVER['HTTP_IF_NONE_MATCH'])
                           ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])     : false;
    
        if (!$if_modified_since && !$if_none_match)
        {
            return;  // the client does not cache anything
        }
    
        if ($if_none_match && $if_none_match != $etag)
        {
            return;  // ETag mismatch: the page changed!
        }
        if ($if_modified_since && $if_modified_since != $last_modified)
        {
            return;  // if-modified-since mismatch: the page changed!
        }
    
        // Nothing changed since last time client visited this page.
    
        header("HTTP/1.0 304 Not Modified");
        header("Last-Modified: $last_modified");
        header("ETag: $etag");
        header("Cache-Control: private, max-age=1, must-revalidate");
        header("Expires: $gmdate_exp");
        header("Pragma: private, cache");
        header("Content-Type: text/html; charset=utf-8");
        exit;
    }
    
    function emitDefaultHeaders($timestamp)
    {
        $gmdate_exp    = gmdate('D, d M Y H:i:s', time() + 1) . ' GMT';
        $last_modified = gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
        $etag          = '"'.md5($last_modified).'"';
    
        header("Last-Modified: $last_modified");
        header("ETag: $etag");
        header("Cache-Control: private, max-age=1, must-revalidate");
        header("Expires: $gmdate_exp");
        header("Pragma: private, cache");
        header("Content-Type: text/html; charset=utf-8");
    }
    
    function getTimestamp()
    {
        // Find out when this page's contents last changed; in a static system,
        // this would be the file time of the backing HTML/PHP page. Add your
        // own logic here:
        return filemtime($SCRIPT_FILENAME);
    }
    
    // ...
    
    $timestamp = getTimestamp();
    emitConditionalGet($timestamp);
    emitDefaultHeaders($timestamp); //previously, this variable was mistyped as "$timestaml"
    

提交回复
热议问题