PHP - setcookie

问题描述 投票:1回答:2

我有插件,在我的主要课程中是这样的:

function test_handle_post(){
    //code
    add_action( 'init', 'add_Cookie' );
}


function add_Cookie() {
    $productname = get_name();
    $filename = $_FILES['upload_file']['name'];
    setcookie('jeden','dwa');
}


function get_name( $context = 'view' ) {
    return $this->get_prop( 'name', $context );
}

并且setcookie()不起作用,因为不添加cookie。为什么?请帮我。我用这些问题搜索了很多页面而没有任何内容。

php wordpress cookies
2个回答
1
投票

试试这个

setcookie("jeden", "dwa", time()+3600) or die('unable to create cookie');

0
投票
Setcookie will set persistent cookie with following syntax -

setcookie(name,value,expire,path,domain,secure,httponly);
// here name  is only the mandatory all remainings are optional
// now you did not specify the value of expires parameter and 
// by default it's value is 0 means the cookie will expire at the end of the session (when the browser closes)

// following code set cookie for 30 days (60*60*24 = 86400 seconds in a day) and path is current url
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
© www.soinside.com 2019 - 2024. All rights reserved.