如何在Word Press中创建和显示会话?

时间:2016-07-13 作者:Devilism

我是word press的新手,但知道所有必需的PHP技能。我正在使用表单在一个页面中创建会话,然后在另一个页面中访问这些会话。我已经在没有word press的情况下完成了这项工作。我需要的是有人告诉我如何在word press中做到这一点。我必须在word press中更改哪些文件,我应该在哪个文件中输入哪些代码?

The main part is i am using google-map-api to calculate the distance between two locations

以下是我目前的代码:
22.php file :

<?php

 session_start();
$pickup = \'\';
$pickupadd = \'\';
$dropoff = \'\';
$dropoffadd = \'\';
$km = \'\';
$_SESSION[\'pickup\'] = $pickup;
$_SESSION[\'pickupadd\'] = $pickupadd;
$_SESSION[\'dropoff\'] = $dropoff;
$_SESSION[\'dropoffadd\'] = $dropoffadd;
$_SESSION[\'km\'] = $km;
?>


<form class="uk-form" action="1111.php" method="get"><label style="color:  
#0095da;"> Pick UP</label>
<input id="pickup" name="pickup" class="controls" style="width: 100%;  
border-bottom: 1px solid #0095da; border-left: 1px solid #0095da; height: 
30px;" type="text" placeholder="Enter a Pick UP location" />
<input id="pickup_address" name="pickupadd" class="controls" style="width:  
100%; border-bottom: 1px solid #0095da; border-left: 1px solid #0095da; 
height: 30px; margin-top: 10px;" type="text" placeholder="House#, Street, 
etc." /><label style="color: #0095da;"> Drop OFF</label>
<input id="dropoff" name="dropoff" class="controls" style="width: 100%; 
border-bottom: 1px solid #0095da; border-left: 1px solid #0095da; height: 
30px;" type="text" placeholder="Enter a Drop OFF location" />
<input id="dropoff_address" name="dropoffadd" class="controls" style="width: 
100%; border-bottom: 1px solid #0095da; border-left: 1px solid #0095da; 
height: 30px; margin-top: 10px;" type="text" placeholder="House#, Street, 
etc." /><label style="color: #0095da;"> Pickup Time</label>
<input id="km" name="km" class="controls" type="hidden" value="" />
<input type="submit" />

</form>
1111.php file :

 <?php

session_start();
$pickup = $_SESSION[\'pickup\'] ;
$pickupadd = $_SESSION[\'pickupadd\'] ;
$dropoff = $_SESSION[\'dropoff\'] ;
$dropoffadd = $_SESSION[\'dropoffadd\'] ;
$km = $_SESSION[\'km\'] ;
if (isset($_GET["pickup"]) || isset($_GET["pickupadd"]) ||    
isset($_GET["dropoff"]) || isset($_GET["dropoffadd"]) || isset($_GET["km"])  
) {
$_SESSION["pickup"] = $_GET["pickup"];
$_SESSION["pickupadd"] = $_GET["pickupadd"];
$_SESSION["dropoff"] = $_GET["dropoff"];
$_SESSION["dropoffadd"] = $_GET["dropoffadd"];
$_SESSION["km"] = $_GET["km"];
}

?>

<body>
<?php 

    echo $_SESSION["pickup"]; 
    echo $_SESSION["pickupadd"]; 
    echo $_SESSION["dropoff"]; 
    echo $_SESSION["dropoffadd"]; 
    echo $_SESSION["km"]; 

?>
</body>

2 个回复
SO网友:Andy Macaulay-Brook

TomC是对的,但在此基础上,我要做的就是。我主要将其与全局对象一起使用,将其序列化到会话中以保存,并从要使用的会话中取消序列化。这里我刚刚使用了一个变量数组。这样,您就不必担心在运行过程中保存到会话中,并且会话的使用也可以轻松地扩展到其他数据中。

我的主要用途是购物篮和订单,所以proper sanitisation, validation and nonces are important, 但为了保持示例的干净性,将它们放在这里。看见https://developer.wordpress.org/plugins/security/nonces/https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data

假设所有这些都进入主题,那么您的初始化代码与中的TomC类似functions.php

add_action( \'init\', \'setup_session\' );

function setup_session() {

    session_start();
    global $map_data;

    if (isset($_SESSION[\'map_data\'])) {
        $map_data = unserialize($_SESSION[\'map_data\']);
    } else {
        $map_data = array();    
    }

    process_get();
    /* chain it after session setup, but could also hook into
       init with a lower priority so that $_GET always writes
       over older session data, or put your own action here to
       give multiple functions access to your sessions
    */
}

add_action( \'shutdown\', \'save_session\' ); 

function save_session() {

    global $map_data;
    if (isset($map_data)) {
        $_SESSION[\'map_data\'] = serialize($map_data);
    }

}

function process_get() {

    // do modify this to:
    // check nonce
    // sanitise
    // validate

`   global $map_data;

    if ( isset($_GET["pickup"]) || isset($_GET["pickupadd"]) ||  isset($_GET["dropoff"]) || isset($_GET["dropoffadd"]) || isset($_GET["km"])  
 ) {
    $map_data["pickup"] = $_GET["pickup"];
    $map_data["pickupadd"] = $_GET["pickupadd"];
    $map_data["dropoff"] = $_GET["dropoff"];
    $map_data["dropoffadd"] = $_GET["dropoffadd"];
    $map_data["km"] = $_GET["km"];
}
// if any of these $_GET vars is set, replace the whole array


}
您也可以这样认为,只要您在PHP发送头之前启动会话,这就可以进入模板文件。在HTML标记正常工作之前,在一个写得很好的主题中(并且假设您没有运行任何写得很差的插件)。在这方面,挂接到WP操作更加健壮,但是将$\\u GET处理放在模板中可以让您轻松地将其保持在一个页面上,而不是在所有页面上运行它。

您可以更进一步,尽管这可能比您的情况需要付出更多的努力,并使用自定义会话代码:https://pippinsplugins.com/storing-session-data-in-wordpress-without-_session/

SO网友:TomC

您希望使用init钩子启动会话,例如:

function start_session() {
    if(!session_id()) {
        session_start();
    }
}

add_action(\'init\', \'start_session\', 1);

function end_session() {
    session_destroy ();
}

add_action(\'wp_logout\', \'end_session\');
add_action(\'wp_login\', \'end_session\');
这将包含在函数中。php文件。

我建议您在此处查看更多信息:

https://codex.wordpress.org/Plugin_API/Hooks