简体   繁体   中英

Session variable won't pass to the next page

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 {.......}

For sessions to work cookie should be enabled, because the Sessions works through a basic sessionId that it's stored also in and with COOKIE .

In other words, you can't benefit from using $_SESSION if the user has COOKIE disabled.

You could learn more form here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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