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