I am trying to store dynamic variable from external link ?dynamic=a_12_bc
and put into the link look like this http://www.myweb.com?tracker=a_12_bc
First time it always show the dynamic link but when i refresh and click on other page it shows ?tracker=[object][Object]
.
<?php session_start();
echo $_GET['dynamic'];
if( isset($_GET['dynamic']))
{
$cookie_name = "dynamic";
$cookie_value = $_GET['dynamic'];
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
$_SESSION["dynamic"] = $cookie_value;
}
else
{
$_SESSION["dynamic"] = $_COOKIE['dynamic'];
}?>
Button Link
<a href="https://www.myweb.com/?tracker=<?php echo($_SESSION['dynamic']);?>" target="_blank">LOG IN</button></a>
Can you please tell me how to maintain a session on other pages
Check whether the cookie has been set, before assigning it to the session by adding isset() in your code.
if(isset($_GET['dynamic']))
{
$cookie_name = "dynamic";
$cookie_value = $_GET['dynamic'];
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
$_SESSION["dynamic"] = $cookie_value;
}
else if(isset($_COOKIE['dynamic'])) {
$_SESSION["dynamic"] = $_COOKIE['dynamic'];
}
else
{
//something here
}
if the cookie is not set, try adding the domain in setcookie() method,
setcookie( $cookie_name , $cookie_value , time() + (86400 * 30), '/', COOKIE_DOMAIN );
If you need more information you can refer this link https://premium.wpmudev.org/blog/set-get-delete-cookies