I tried many ways to set a cookie, but when I get the cookie the value's not set. My code is placed before the <!DOCTYPE html>:
<?php
$url = explode('/', $_GET['url']);
$ref = $url[1];
$cookie_name = "refid";
$cookie_value = $ref;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", "", 0);
?>
The $url[1] is set, I can see it in print_r() the problem is getting the cookie from a different page the calling code is:
<?php
if (!isset($_COOKIE['refid'])) {
echo "<br/>Cookie named refid is not set!";
} else {
echo "<br/>Cookie refid is set!<br>";
echo "Value is: " . $_COOKIE['refid'];
}
?>
Please help to resolve my problem.
Add this line:
$_COOKIE[$cookie_name] = $cookie_value;
after you set the cookie:
<?php
$url=$_GET['url'];
$url=explode ('/',$_GET['url']);
$ref=$url[1];
$cookie_name = "refid";
$cookie_value = $ref;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", "", 0);
$_COOKIE[$cookie_name] = $cookie_value;
?>
The setcookie() does not update the current $_COOKIE variable, which will be instantiated when the script loads. The variable will first be updated next time you load the script.
Related
I am trying to delete a cookie by setting that cookie in past time:
$cookie_name = "user";
$cookie_value = "david";
//subtraction from time causes deletion of cookie
setcookie($cookie_name, $cookie_value, time() - (86400 * 30), "/");
With the below code I try to check whether cookie is enabled or not and it returns if case rather than else part, while I already dell that cookie:
//counting number of cookies
if(count($_COOKIE) > 0) {
echo "<br>Cookies are enabled/exists";
} else {
echo "<br>Cookies are disabled/not exists";
}
But the else part is not working when we delete cookie and I don't know why?
The main problem is you just set user cookie time to past date not all the other cookie in super global $_COOKIE array . Try like this way to set for all $_COOKIE value using foreach() to past date and then check count condition.
<?php
$cookie_name = "user";
$cookie_value = "david";
$past_time = time() - 3600;
//use look set all cookie time to past date.
foreach ( $_COOKIE as $key => $value )
{
setcookie( $key, $value, $past_time, '/' );
}
//counting number of cookies
if(count($_COOKIE) > 0) {
echo "<br>Cookies are enabled/exists";
} else {
echo "<br>Cookies are disabled/not exists";
}
?>
DEMO: https://3v4l.org/jvRXW
I am new to PHP and I am having quite the trouble with using Cookies!
So I am trying to have one cookie that will keep count of the number of times the page has been loaded within a lifetime of 1 minute. Once it has reached its lifetime it needs to be unset and the counter should go back to 0.
So far here is what I have:
At the top of the Php file
<?php
session_start();
$_SESSION['user_start'] = time();
$cookie_name = "counter";
$cookie_value = 0;
setcookie($cookie_name, $cookie_value);
?>
<html>
<body>
<?php
if (time() - $_SESSION['user_start'] < 60) {
$counter = $_COOKIE[$cookie_name] +1;
setcookie($cookie_name, $counter);
echo "Value is: " . $_COOKIE[$cookie_name];
}
else{
unset($_SESSION['user_start']);
unset($_COOKIE[$cookie_name]);
}
?>
It's a little odd - but this should do the trick, I'm storing both the counter value and the expiry time in a cookie in JSON format. That way you can just increment the counter whilst keeping the initial expiry time (e.g. 1 minute after the initial request).
<?php
$counter = 0;
$expires = time()+60;
if(isset($_COOKIE['counter'])) {
$data = json_decode($_COOKIE['counter']);
$counter = ++$data->counter;
$expires = $data->expires;
}
$jsonData = json_encode(['counter' => $counter, 'expires' => $expires]);
setcookie("counter", $jsonData, $expires);
echo $counter;
I'm having some trouble firing cookies on the condition of what page the user visits first.
Code below fires a cookie if on pages 2641, 2998, 2949 and no cookie exists. However, how do I do it to fire a different cookie if user is on any other page on the website if no cokkies exist?
Rule: Two cookies cannot exist. Just one or the other.
Any help much appreciated :)
if (is_page([2641,2998,2949]) && !isset($_COOKIE['ppc_campaign']) && !isset($_COOKIE['organic'])) {
$ppc_cookie = "ppc_campaign";
$ppc_value = (!empty($_SERVER['HTTPS']))
? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
: "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$path = "/";
setcookie($ppc_cookie, strstr($ppc_value, '?'), time() + (86400 * 28), $path);
$acf_applicationLink = $ppc_value;
}
else {
}
Sounds like this is what you want. Check for cookie existence. If neither exists, check the specific page, otherwise do something else.
if (!(isset($_COOKIE['ppc_campaign']) || isset($_COOKIE['organic']))) {
if (is_page([2641,2998,2949])) {
$ppc_cookie = "ppc_campaign";
$ppc_value = (!empty($_SERVER['HTTPS']))
? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
: "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$path = "/";
setcookie($ppc_cookie, strstr($ppc_value, '?'), time() + (86400 * 28), $path);
$acf_applicationLink = $ppc_value;
}
else {
$organic_cookie = "organic";
$organic_value = "?campaign=_ORGANIC_";
$path = "/";
setcookie($organic_cookie, $organic_value, time() + (86400 * 28), $path);
$acf_applicationLink = $organic_value;
}
}
I am trying to create a login system. At the moment everything seem to be working as expected except I am not able to clear my session ID,
why do session_unset() and session_destroy don't seem to have any effect ?
UPDATE: solved below
INDEX.PHP
session_start();
if (array_key_exists('id', $_COOKIE) && $_COOKIE ['id']) {
$_SESSION['id'] = $_COOKIE['id'];
print("SESSION ID");
print("<br>");
print_r($_SESSION);
print("<br>");
print("COOKIE");
print("<br>");
print_r($_COOKIE);
}
// SET SESSION
function setSession($setSessionData) {
$_SESSION['id'] = $setSessionData[0];
if ($setSessionData[1] == 'yes') {
setcookie('id', $setSessionData[0], time() + 60*60*24*365, '/' );
}
};
// CLEAR SESSION
function unSetSession() {
session_unset();
setcookie("id", "", time() - 60*60*24*365, '/');
session_destroy();
}
SOLVED:
Had to initialise the session in my function called via Ajax; so the logout function is like so:
function unSetSession() {
session_start();
$_SESSION = array();
setcookie("id", "", time() - 60*60*24*365, '/');
session_destroy();
}
I am having a PHP code problem. As you see, I get an id from another page. I want to save these id's in array based on cookies. I was able to do that, but I have a problem with it.
When I set the timeout (for example to 20 seconds) and refresh page: every 20 second it works fine, but if I refresh it in under 20 seconds it gives me the error below.
Fatal error: [] operator not supported for strings
I do not know why; could you please help?
<?php
$ID = is_numeric($_GET['ID']) ? $_GET['ID'] : 1;
$cookie_name = "favoritepost";
if ( isset($_COOKIE[$cookie_name]) ) {
$kookie = $_COOKIE[$cookie_name];
} else {
$kookie = array();
}
if ( ! in_array($ID, $kookie) ) {
$kookie[] = $ID;
}
setcookie($cookie_name, serialize($kookie), time() + (20), "/"); // 86400 = 1 day
?>
<html>
As per my last answer https://stackoverflow.com/a/38307347/2310830
<?php
$ID = is_numeric($_GET['ID']) ? $_GET['ID'] : 1;
$cookie_name = "favoritepost";
if ( isset($_COOKIE[$cookie_name]) ) {
$kookie = unserialize($_COOKIE[$cookie_name]);
} else {
$kookie = array();
}
if ( ! in_array($ID, $kookie) ) {
$kookie[] = $ID;
}
setcookie($cookie_name, serialize($kookie), time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
The array is serialized when it is stored as a cookie. So when you retrieve it, it is a string, not an array. You need to unserialize it before you add more to it:
$kookie = unserialize($_COOKIE[$cookie_name]);