传递 PHP 会话值 [英] Passing PHP Session Value

查看:38
本文介绍了传递 PHP 会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 PHP 会话将值从一个 WordPress 页面传递到另一个页面.我在两个页面上都有一个下拉列表,除了选项值之外,它们是相同的.我希望页面加载时第二页上的选定选项反映在第一页上提交的所选值.

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!

第 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>

第 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>

推荐答案

WordPress 核心不使用 PHP 会话.WordPress 平台是完全无状态的,不提供任何支持用于保持用户登录的 cookie 之外的会话的使用

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 核心不使用 PHP 会话,但有时您的用例、插件或主题需要它们.

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

您可以使用 WordPress Native PHP Sessions 插件来实现 PHP 的原生会话处理程序,由 WordPress 数据库支持.

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

参见 在 WordPress 中使用 PHP 会话

这篇关于传递 PHP 会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆