简体   繁体   English

查找cookie并仅在php中重定向到页面一次

[英]Find cookie and redirect to a page just once in php

I have this problem with redirecting to a page, I have a cookie "EmailSubscriber". 我在重定向到页面时遇到此问题,我有一个cookie“ EmailSubscriber”。 so the condition here is if the website loads and finds that cookie then it will redirect to a particular page. 因此这里的条件是,如果网站加载并找到该Cookie,则它将重定向到特定页面。

$rlink = get_option( "subscriber_redirect_link" );

if ( isset($_COOKIE["EmailSubscriber"]) ) {
    wp_redirect( $rlink, 302 );
}

but this code redirects every time I change pages, so I'm stuck on the redirected page, so how can I make this just once? 但是此代码在每次更改页面时都会重定向,因此我停留在重定向的页面上,那么如何才能做到这一点呢? meaning that when I open my browser and it detects the cookie "EmailSubscriber" then it will redirect me to a page and then when I navigate away from that page it won't redirect me again. 这意味着,当我打开浏览器并检测到Cookie“ EmailSubscriber”时,它将把我重定向到一个页面,然后当我离开该页面时,它将不再重定向我。

The reason for the continual redirect is that after the redirect the cookie is still set, and this code runs again. 进行持续重定向的原因是,在重定向之后,仍会设置cookie,并且此代码将再次运行。 Cookies like session variables are persistent between requests. Cookie(如会话变量)在请求之间是持久的。

You need to unset the cookie value before you redirect, also don't fetch the redirect link unless you are going to use it. 重定向之前,您需要取消设置cookie值,也不要获取重定向链接,除非您打算使用它。

if (isset($_COOKIE['EmailSubscriber'])) {
    unset($_COOKIE['EmailSubscriber']);
    $rlink = get_option('subscriber_redirect_link');
    wp_redirect($rlink, 302);
}

That said, using a cookie to trigger a redirect is a rather odd design choice, perhaps you should instead flag the redirect using a get argument, for example: 也就是说,使用cookie触发重定向是一个很奇怪的设计选择,也许您应该改为使用get参数来标记重定向,例如:

https://example.com/script.php?redirect=1

if (isset($_GET['redirect'])) {
    $rlink = get_option('subscriber_redirect_link');
    wp_redirect($rlink, 302);
}

Or use a session variable as this will work even if the headers have already been sent, unlike using a cookie. 或者使用会话变量,因为即使已经发送了标头,它也将起作用,这与使用cookie不同。 It is also safer as the client can not alter the values in the session unlike a cookie. 由于客户端无法像Cookie一样更改会话中的值,因此它也更安全。

if (isset($_SESSION['redirect'])) {
    unset($_SESSION['redirect']);
    $rlink = get_option('subscriber_redirect_link');
    wp_redirect($rlink, 302);      
}

You can use the combination of the cookie and session 您可以结合使用cookie和会话

first start the session (add this) in header.php, add this if the session_start() not declared previously 首先在header.php中启动会话(添加此会话),如果session_start()之前未声明,则添加会话

session_start();

then add this in your page which you want to redirect 然后将此添加到您要重定向的页面中

$rlink = get_option( "subscriber_redirect_link" );

$_SESSION['previous'] = basename($_SERVER['PHP_SELF']);

//checking both values cookie and session
if ( isset($_COOKIE["EmailSubscriber"]) && isset($_SESSION["previous"])) {
    wp_redirect( $rlink, 302 ); //redirect if both set
}

then add this in footer.php 然后在footer.php中添加它

if (isset($_SESSION['previous'])) {
   if (basename($_SERVER['PHP_SELF']) != $_SESSION['previous']) {//this will compare your redirected page with the navigated page
        unset($_SESSION['previous']);//this will unset the session variable
   }
}

Give a value to the cookie TRUE or FALSE for example 例如,将Cookie值设为TRUE或FALSE

TRUE - the redirect link is visited TRUE已访问重定向链接

FALSE - the redirect link is not visited FALSE重定向链接未访问

and on the redirect page change the value of the cookie. 然后在重定向页面上更改Cookie的值。

Modify your code to: 修改您的代码以:

if ( isset($_COOKIE["EmailSubscriber"]) ) {
    if($_COOKIE["EmailSubscriber"] == FALSE) {
        wp_redirect( $rlink, 302 );
    }
}

Then on the redirect page change the cookie value: 然后在重定向页面上更改cookie值:

$_COOKIE["EmailSubscriber"] = TRUE;

When you will enter that site again it will not redirect anymore. 当您再次进入该站点时,它将不再重定向。

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

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