$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Set a cookie inside the 404.php theme page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Set a cookie inside the 404.php theme page

matteradmin8PV0评论

I'm trying to set a cookie with these code but the cookie is not created

setcookie( 'name', 'carl', 0, '/names', '', isset($_SERVER["HTTPS"]), true);

I was looking on the internet a possible solution and I found and try this solution

add_action( 'init', 'wpse8170_init' );
function wpse8170_init() {
    if ( !headers_sent() ) {
        setcookie( 'name', 'carl', 0, '/names', '', isset($_SERVER["HTTPS"]), true);
    }
}

But it didn't work. Please help, I just need a way to create a cookie inside my 404.php theme page and then I will make a redirection to another page of my own website, as the cookie is just to keep the variable for the next page.

I'm trying to set a cookie with these code but the cookie is not created

setcookie( 'name', 'carl', 0, '/names', 'https://my.webdomain', isset($_SERVER["HTTPS"]), true);

I was looking on the internet a possible solution and I found and try this solution

add_action( 'init', 'wpse8170_init' );
function wpse8170_init() {
    if ( !headers_sent() ) {
        setcookie( 'name', 'carl', 0, '/names', 'https://my.webdomain', isset($_SERVER["HTTPS"]), true);
    }
}

But it didn't work. Please help, I just need a way to create a cookie inside my 404.php theme page and then I will make a redirection to another page of my own website, as the cookie is just to keep the variable for the next page.

Share Improve this question asked Jan 13, 2019 at 4:03 Lester VargasLester Vargas 1035 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I think the template files load too late, try setting it in the functions.php file.

add_action( 'init', 'wpse325439_init' );
function wpse325439_init() {
    if ( !headers_sent() && is_404() ) {
        setcookie( 'name', 'carl', 0, '/names/', 'my.webdomain', isset($_SERVER["HTTPS"]), true);
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far