传递PHP会话值

I'm attempting to pass a value from one WordPress page to another through a PHP Session. I have a dropdown list on both pages, which are identical except for the option values. I would like the selected option on the 2nd page at page load to reflect the chosen value submitted on the first page.

I've looked into a lot of questions but haven't been able to find exactly what I need. Any help would be greatly appreciated!

Page 1:

<?php
    if(!isset($_SESSION)) { 
        session_start(); 
        echo $_SESSION['state'];
        $var_value = $_SESSION['state'];
    } 
?>

<form>
    <input type=hidden name="retURL" value="page2.php">
    <select id="state" name="state">
        <option>State 1</option>
        <option>State 2</option>
        <option>State 3</option>
    </select>
    <input type="submit" name="submit" value="Submit">
</form>

Page 2:

<?php
    session_start();
    if(isset($_POST['state'])){
        $_SESSION['state'] = $_POST['state'];
    }
?>

<select>
    <option <?php if($var_value == 'State 1') echo 'selected'; ?> value="http://customizedlink">State 1</option>
    <option <?php if($var_value == 'State 2') echo 'selected'; ?> value="http://customizedlink">State 2</option>
    <option <?php if($var_value == 'State 3') echo 'selected'; ?> value="http://customizedlink">State 3</option>
</select>

You've forgotten to tell us how you're getting from Page 1 to Page 2. But, let's assume that when you SELECT in Page 1, Page 1 is called again. In that case, you need to move your $_SESSION['state'] = $_POST['state']; assignment from Page 2 to Page 1. It must occur before you move to Page 2.

Also, Page 2 is using something called $var_value which isn't set anywhere in your examples. Did you mean to execute $var_value = $_SESSION['state']; somewhere above the SELECT in Page 2?

If none of this leads you to an answer, please provide more information about the context of your code.


Try this. Your Page 1 FORM element, when submitted, calls Page 1. Therefore...

<?php
    if(!isset($_SESSION)) { 
        session_start(); 
        if( isset($_POST['state']) ){
            $_SESSION['state'] = $_POST['state'];
        }
        echo "Page 1: ".$_SESSION['state'];
    } 
?>

<form method="post">
    <input type=hidden name="retURL" value="page2.php">
    <select id="state" name="state">
        <option>State 1</option>
        <option>State 2</option>
        <option>State 3</option>
    </select>
    <input type="submit" name="submit" value="Submit">
</form>

Page 2

<?php
    session_start();
    echo "Page 2: ".$_SESSION['state'];
    $var_value = $_SESSION['state'];
?>

<select>
    <option <?php if($var_value == 'State 1') echo 'selected'; ?> value="http://customizedlink">State 1</option>
    <option <?php if($var_value == 'State 2') echo 'selected'; ?> value="http://customizedlink">State 2</option>
    <option <?php if($var_value == 'State 3') echo 'selected'; ?> value="http://customizedlink">State 3</option>
</select>

You should see the correct value for $_SESSION['state'] in Page 2 (after running Page 1) IF I have understood the intent of your code correctly.

WordPress core does not use PHP sessions. The WordPress platform is totally stateless and provides no support for the use of sessions outside of the cookie that keeps a user logged in.

WordPress core does not use PHP sessions, but sometimes they are required by your use-case, a plugin or theme.

You can use WordPress Native PHP Sessions plugin that implements PHP’s native session handlers, backed by the WordPress database.

see Using the PHP Session in WordPress