0

I am passing a variable in the url, example: www.example.com/mypage?gp=32442 Note: This is a WordPress site.

After the gp variable has been saved, I'm comparing it to the current page id and if they are equal i'm setting the cookie and the session and redirecting to the same page only without the "?gp=32442" string at the end.

The cookies are working correctly, but I also added the session variable in case there are people with cookies disabled.

When I test with cookies disabled, the session variable is empty after the redirection. I tested without the redirect and the session variable is correctly added.

What can I do to keep the session variable after the redirect?

 session_start();

    if(isset($_GET["gp"])){ 
            $url_post_id = $_GET["gp"];
        }

    $current_id = get_the_ID();

    // $pageURL is the url without the "?gp=32442" at the end

    if($current_id == $url_post_id){                    
        setcookie("gpcid", $url_post_id, time() + (60 * 60 * 24 * 14));                 
        $_SESSION['gpsid'] = $url_post_id;
        header('Location: '.$pageURL.'');
    }

    if($_COOKIE["gpcid"] == $current_id || $_SESSION['gpsid'] == $current_id  ){
        the_content();
       } else {.......}
4

1 回答 1

1

为了使会话工作 cookie 应该被启用,因为 Sessions 通过一个基本的工作sessionId,它也存储在COOKIE.

换句话说,$_SESSION如果用户已COOKIE禁用,您将无法从使用中受益。

你可以在这里这里了解更多形式

于 2013-10-20T13:39:05.413 回答