/    Sign up×
Community /Pin to ProfileBookmark

Folks,

Why I get echoed:

Cookie ‘guest_visitor’ is set!
Value is: COOKIE_NAME_VISITOR_IP
**Cookie Value ‘COOKIE_NAME_VISITOR_IP’ is not set!**

Problem is last line of echo.

““
$cookie_name = “guest_visitor”;
$cookie_value = “COOKIE_NAME_VISITOR_IP”;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), “/”); // 86400 = 1 day

if(!isset($_COOKIE[$cookie_name])) {
echo “Cookie named ‘” . $cookie_name . “‘ is not set!”;
} else {
echo “Cookie ‘” . $cookie_name . “‘ is set!<br>”;
echo “Value is: ” . $_COOKIE[$cookie_name];
}

echo “<br>”;

if(!isset($_COOKIE[$cookie_value])) {
echo “Cookie Value ‘” . $cookie_value . “‘ is not set!”;
} else {
echo “Cookie ‘” . $cookie_value . “‘ is set!<br>”;
echo “Value is: ” . $_COOKIE[$cookie_value];
}
““

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 19.2020 — setcookie() does not set anything in $_COOKIE. It sends the cookie to the client when HTTP headers get sent by the script. Subsequent requests from that client to the same domain will then send that cookie _back_ to the server, at which point the receiving PHP page will have it set within the $_COOKIE array.
Copy linkTweet thisAlerts:
@developer_webauthorOct 20.2020 — @NogDog#1624393

Ok. I understand all this but still it doesn't explain why I getting mssg Cookie value not set when I get mssg Cookie is set. Either oth should not set or oth should set. Not one set and the either ain't. That is my point. Or, is it because I testing in LocalHost which got no domain or cos I eing oth serer (localhost) and client ?
Copy linkTweet thisAlerts:
@developer_webauthorOct 20.2020 — Folks,

I set cookie so why I get mssg not set ?

<?php

setcookie("Guest_Visitor_Details", "", time() - 3600);//RETYPE

if(ISSET($_SESSION['user_id']))
{
$user_id = $_SESSION['user_id'];
$user_type = 'member';
echo "49.member";
}
elseif(ISSET($_COOKIE['Guest_Visitor_Details']))
{
$user_id = $_COOKIE['Guest_Visitor_Details'];
$user_type = 'guest';
echo "55.guest";
}
else
{
$ip = fetch_ip();

$cookie_name = 'Guest_Visitor_Details';
$cookie_value = $ip;

setcookie("$cookie_name","$ip",time()+1*24*60*60); //TEST RESULT: Line: 64. Cookie gets set.

if(!ISSET($_COOKIE[$cookie_name]))
{
die("Cookie not set!");//TEST RESULT: Line: 68. Cookie got set at line 64 and so why this line 68 triggers here ?
}
else
{
echo "$cookie_name is: " . $_COOKIE[$cookie_name]; //TEST RESULT: //LINE: 72: Cookie should echoe here as following but script-flow doesn't trigger this line: "Guest_Visitor_Details is: 127.0.0.1".
}
Copy linkTweet thisAlerts:
@NogDogOct 20.2020 — Maybe because you are setting the cookie expiration time to an hour ago?
<i>
</i>setcookie("Guest_Visitor_Details", "", time() - 3600);

..._remembering that $_COOKIE will not be set by the setcookie() function,_ only by the receipt of that cookie on subsequent page requests -- and even then the cookie won't be returned if it is expired.
Copy linkTweet thisAlerts:
@developer_webauthorOct 21.2020 — @NogDog#1624431

No. I deleted cookie with that:

setcookie("Guest_Visitor_Details", "", time() - 3600);


Then re-started the cookie in the ELSE because here the ELSE should trigger since no $_SESSION or $_COOKIE is set.

The script-flow should by-pass IF (due to lacking SESSION) and by-pass ELSEIF (due to lacking Cookie) and land into the "ELSE". And inside this ELSE, you can see that, I set the cookie and so I should not later on down the script-flow get mssg that "Cookie not set".

Line 64

setcookie("$cookie_name","$ip",time()+1*24*60*60); //TEST RESULT: Line: 64. Cookie gets set.


And so, down the script flow from this point, I never should get mssg on line 68 that, "cookie not set".

if(!ISSET($_COOKIE[$cookie_name]))
{
die("Cookie not set!");//TEST RESULT: Line: 68. Cookie got set at line 64 and so why this line 68 triggers here ?
}


Still confused anybody ?

If so, fireup xamp/wamp/lamp and test the code ....

<?php

setcookie("Guest_Visitor_Details", "", time() - 3600);//RETYPE

if(ISSET($_SESSION['user_id']))
{
$user_id = $_SESSION['user_id'];
$user_type = 'member';
echo "49.member";
}
elseif(ISSET($_COOKIE['Guest_Visitor_Details']))
{
$user_id = $_COOKIE['Guest_Visitor_Details'];
$user_type = 'guest';
echo "55.guest";
}
else
{
$ip = fetch_ip();

$cookie_name = 'Guest_Visitor_Details';
$cookie_value = $ip;

setcookie("$cookie_name","$ip",time()+1*24*60*60); //TEST RESULT: Line: 64. Cookie gets set.

if(!ISSET($_COOKIE[$cookie_name]))
{
die("Cookie not set!");//TEST RESULT: Line: 68. Cookie got set at line 64 and so why this line 68 triggers here ?
}
else
{
echo "$cookie_name is: " . $_COOKIE[$cookie_name]; //TEST RESULT: //LINE: 72: Cookie should echoe here as following but script-flow doesn't trigger this line: "Guest_Visitor_Details is: 127.0.0.1".
}
Copy linkTweet thisAlerts:
@developer_webauthorOct 22.2020 — @NogDog

Any Ideas NogDog ?
Copy linkTweet thisAlerts:
@developer_webauthorOct 22.2020 — @Sempervivum
>I sure you know about cookies for atleast 15yrs now. Care to give input ?
>
Unfortunately not as far as the server side is concerned. On the client side I use local storage instead of cookies as this is easier to handle and provides more space.
Copy linkTweet thisAlerts:
@developer_webauthorOct 22.2020 — @developer_web#1624515

Mmm.

So, you more of a javascript type man ?
Copy linkTweet thisAlerts:
@tracknutOct 22.2020 — From NogDog, several times:

...remembering that $_COOKIE will not be set by the setcookie() function, only by the receipt of that cookie on subsequent page requests -- and even then the cookie won't be returned if it is expired.
[/quote]
Copy linkTweet thisAlerts:
@developer_webauthorOct 26.2020 — @tracknut#1624518

Was having problem cos I didn't put following above <html> tag.

setcookie()
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...