admin管理员组文章数量:1130349
I'm trying to set up, when a specific page/url is visited that a cookie is set and saved.
So far I've tried this:
add_action('init', 'set_cookie', 1);
function set_cookie(){
if ( $currentURL == '/' ) :
if ( ! isset( $_COOKIE['opt_in'] ) ) :
setcookie( 'opt_in', has_opt_in, time()+31556926);
endif;
endif;
}
Now, when I visit the specific URL, cookie is detected, but when I move on to another page, cookie is gone.
How can the cookie be saved?
I'm pretty new to this, please help me understand.
Thank you!
I'm trying to set up, when a specific page/url is visited that a cookie is set and saved.
So far I've tried this:
add_action('init', 'set_cookie', 1);
function set_cookie(){
if ( $currentURL == 'https://25dni.si/delovanje-uma/' ) :
if ( ! isset( $_COOKIE['opt_in'] ) ) :
setcookie( 'opt_in', has_opt_in, time()+31556926);
endif;
endif;
}
Now, when I visit the specific URL, cookie is detected, but when I move on to another page, cookie is gone.
How can the cookie be saved?
I'm pretty new to this, please help me understand.
Thank you!
Share Improve this question asked Nov 29, 2018 at 15:08 AlexAlex 213 bronze badges2 Answers
Reset to default 1I've used this and it works:
add_action('init', 'optin_cookie', 1);
function optin_cookie(){
$currentURL = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ( $currentURL == 'https://25dni.si/delovanje-uma/' ) :
setcookie( 'opt_in', has_opt_in, time()+31556926, '/');
elseif ( $currentURL != 'https://25dni.si/' ) :
if ( ! isset( $_COOKIE['opt_in'] ) ) :
endif;
endif;
}
I'm sure there is a much more elegant way to do it, but this got it working for me ...
I'm surprised it works at all since $currentURL is not defined in the function nor declared as a global.
Look at the documentation for setcookie(). You did not declare a path for the cookie so try this:
setcookie('opt_in','opt_in', time()+31556926, '/');
That will set the cookie for the entire domain, if that's what you want to do.
I'm trying to set up, when a specific page/url is visited that a cookie is set and saved.
So far I've tried this:
add_action('init', 'set_cookie', 1);
function set_cookie(){
if ( $currentURL == '/' ) :
if ( ! isset( $_COOKIE['opt_in'] ) ) :
setcookie( 'opt_in', has_opt_in, time()+31556926);
endif;
endif;
}
Now, when I visit the specific URL, cookie is detected, but when I move on to another page, cookie is gone.
How can the cookie be saved?
I'm pretty new to this, please help me understand.
Thank you!
I'm trying to set up, when a specific page/url is visited that a cookie is set and saved.
So far I've tried this:
add_action('init', 'set_cookie', 1);
function set_cookie(){
if ( $currentURL == 'https://25dni.si/delovanje-uma/' ) :
if ( ! isset( $_COOKIE['opt_in'] ) ) :
setcookie( 'opt_in', has_opt_in, time()+31556926);
endif;
endif;
}
Now, when I visit the specific URL, cookie is detected, but when I move on to another page, cookie is gone.
How can the cookie be saved?
I'm pretty new to this, please help me understand.
Thank you!
Share Improve this question asked Nov 29, 2018 at 15:08 AlexAlex 213 bronze badges2 Answers
Reset to default 1I've used this and it works:
add_action('init', 'optin_cookie', 1);
function optin_cookie(){
$currentURL = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ( $currentURL == 'https://25dni.si/delovanje-uma/' ) :
setcookie( 'opt_in', has_opt_in, time()+31556926, '/');
elseif ( $currentURL != 'https://25dni.si/' ) :
if ( ! isset( $_COOKIE['opt_in'] ) ) :
endif;
endif;
}
I'm sure there is a much more elegant way to do it, but this got it working for me ...
I'm surprised it works at all since $currentURL is not defined in the function nor declared as a global.
Look at the documentation for setcookie(). You did not declare a path for the cookie so try this:
setcookie('opt_in','opt_in', time()+31556926, '/');
That will set the cookie for the entire domain, if that's what you want to do.
本文标签: phpSetting a cookie upon specific URL visit
版权声明:本文标题:php - Setting a cookie upon specific URL visit 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749136798a2321496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论