简体   繁体   English

检查是否存在 PHP cookie 无效

[英]Check if a PHP cookie exists not worked

I am using this code to set a cookie in Wordpress我正在使用此代码在Wordpress中设置cookie

functions.php功能。php

function wp_test_req($post_id) { 
    if(isset($_POST['submits'])) {
        setcookie( 'mycookie', 'value', time()+ 36, '/');
    }
} 
add_action('init', 'wp_test_req');

HTML HTML

and with this code then see if cookie exists然后使用此代码查看cookie是否存在

<form action="" method="post">
  <?php 
    if (!isset($_COOKIE['mycookie']))
    { echo '<input type="submit" value="Up+1" id="submit" name="submits">';}
    else { echo 'NotSet';}
  ?>
</form>

problem is if (!isset($_COOKIE['mycookie'])) not work.问题是if (!isset($_COOKIE['mycookie']))不起作用。

In fact, the problem is that nothing happens in the first click, but changes are made in the second click其实问题是第一次点击什么都没有,但是第二次点击就变了

Where is wrong?哪里错了?

Thanks for any help谢谢你的帮助

force $_COOKIE['mycookie'] for affect immediatly强制 $_COOKIE['mycookie'] 立即生效

<?php
if (!empty($_POST['submits'])) {
    setcookie('mycookie', 'value', time()+ 36);
    $_COOKIE['mycookie'] = 'value'; // <----
}
?>

<form action="" method="post">
  <?php
    if (empty($_COOKIE['mycookie'])) {
        echo '<input type="submit" value="Up+1" id="submit" name="submits">';
    } else {
        echo 'NotSet';
    }
  ?>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM