/    Sign up×
Community /Pin to ProfileBookmark

Need help with cookies

Hello

I want to set cookies.
But I’ve got a problem.

This code doesn’t work:

[code=php]
<?php
if (isset($_COOKIE[“myCookie”, true])) {
echo ‘
<script>alert(“You already have a cookie”);</script>
‘;
} else {
setcookie(“myCookie” , true);
echo ‘
<script>alert(“New cookie is set”);</script>
‘;
}
?>
[/code]

It always alert “New cookie is set”.
I think I’ve got a problem with checking/setting the cookie.
And I really can’t find the problem.

Can someone help me?

Thanks for reading,
Niely

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@dclampMay 10.2014 — Why do you have ", true" in your cookie $_COOKIE[] array? Are you trying to check to see if $_COOKIE['myCookie'] == true? You need to use two different statements to check if it true and if its set. you cant do it in one go.

[code=php]
if ($_COOKIE['myCookie'] == true) {
//your statements
}

// OR

if (isset($_COOKIE['myCookie']) {
//your statements
}
[/code]
Copy linkTweet thisAlerts:
@ginerjmMay 10.2014 — Why are you using JS to display a message that you can easily do with php?

And yes - your use of cookie is wrong. Read up in the php manual how to check for a cookie and especially how to set a cookie - since your attempt at setting one is pretty lame.

Here's a link to what's in the manual - a handy place to look for anything about php

http://us3.php.net/manual/en/function.setcookie.php
×

Success!

Help @Nielyboyken 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 5.15,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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