Sessioin变量仅在页面刷新后的href链接后更新

时间:2019-03-29 19:41:48

标签: php

我正在从3个php脚本中包含的通用表单中收集一些简单的表单数据,并将其存储在会话数组中。我在每个页面上都使用session_start,但是存储在$_SESSION中的数据不会在我使用href链接选择的页面上更新,除非我随后刷新页面,然后数据在那里。

在存储会话数据之后,我使用header('Location: '.$_SERVER['PHP_SELF']);并在当前页面上工作,但是如果单击其他页面直到刷新它们然后获得数据,这些数据就会丢失在其他页面上?

<?php 
require 'includes/functions.php';
startSession();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Page 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>

<ul>
    <li>Page 1</li>
    <li><a href="page2.php">Page 2</a></li>
    <li><a href="page3.php">Page 3</a></li>
</ul>

<?php 
// include the form and related code
require 'includes/form.php'; 
?>

<!-- Greeting -->
<h1>Welcome Stranger to Page 1!</h1>
<p>
Lorem ipsum dolor 
</p>
</body>
</html>

然后我又有2页,但彼此之间都有链接。

functions.php
<?php

function startSession() {
    session_start();
    echo 'Session Started: ID='.session_id();
    echo ' - Session super global array contains: ';
    print_r($_SESSION);
}

function reloadCurrentPage() {
    header('Location: '.$_SERVER['PHP_SELF']);
}

?>

form.php

<?php
// State variables
$form_is_submitted = false;
$errors_detected = false;

// Arrays to gather data
$clean = array();
$errors = array();

if (isset($_SESSION['firstname'])) {
    echo 'Form.php know the first name is: ' . $_SESSION['firstname'];
}

// Validate form if it was submitted
if (isset($_POST['submitdetails'])) {

    $form_is_submitted = true;

    // First name is a required field
    if (isset($_POST['firstname'])) {
        $name_in = trim($_POST['firstname']);
        $length = strlen($name_in);
        if ($length >= 1) {
            $clean['firstname'] = $name_in;
        } else {
            $errors_detected = true;
            $errors[] = 'First name is required';
        }
    } else {
        $errors_detected = true;
        $errors[] = 'First name not submitted';
    }

    // Favourite number must be a whole number greater than 0
    if (isset($_POST['favourite'])) {
        $favourite_in = trim($_POST['favourite']);
        if (ctype_digit($favourite_in) && $favourite_in > 0) {
            $clean['favourite'] = $favourite_in;
        } else {
            $errors_detected = true;
            $errors[] = 'Favourite number should be a whole number greater than 0';
        }
    } else {
        $errors_detected = true;
        $errors[] = 'Number not submitted';
    }

}

// Collect output in a variable, tidier than multiple "echo" calls
$output = '';

// Decide whether to process data or (re)display form
if ($form_is_submitted === true && $errors_detected === false) {

    // Submission was OK, so display thank-you message
    //$output .= '<p>Thanks, we will remember you!</p>';
    //HO12-3 store valid form data in the session super global array
    $_SESSION['firstname'] = $clean['firstname'];
    $_SESSION['favourite'] = $clean['favourite'];
    reloadCurrentPage();

} else {

    // Display error messages, if there are any
    if ($errors_detected === true) {
        $output .= '<p>Sorry, we found some errors with your data:</p>';
        $output .= '<ul>';
        foreach ($errors as $reason) {
            $output .= '<li>'.htmlentities($reason).'</li>';
        }
        $output .= '</ul>';
    }

    // data to redisplay in the form...
    if (isset($clean['firstname'])) {
        $html_firstname = htmlentities($clean['firstname']);
    } else {
        $html_firstname = '';
    }

    // data to redisplay in the form...
    if (isset($clean['favourite'])) {
        $html_favourite = htmlentities($clean['favourite']);
    } else {
        $html_favourite = '';
    }

    // Sanitise the current URL for use in the "action" attribute of form
    $self = htmlentities($_SERVER['PHP_SELF']);

    // display the form, with valid data (if there is any)
    $output .= '<form action="' . $self . '"method="post">
            <fieldset>
                <legend>Your details</legend>

                <div>
                    <label for="fn">What is your first name?</label>
                    <input type="text" name="firstname" id="fn" value="' . $html_firstname . '" />
                </div>

                <div>
                    <label for="fav">What is your favourite number?</label>
                    <input type="text" name="favourite" id="fav" value="' . $html_favourite . '" />
                </div>

                <input type="submit" name="submitdetails" value="Remember Me!" />

            </fieldset>
        </form>';

}

// Echo gathered output from script
echo $output;
?>

如何通过点击链接访问最新的$_SESSION[]数据而不必刷新链接的页面?

0 个答案:

没有答案