How to Check If a Cookie Exists

How do I check if a cookie exists?

You can call the function getCookie with the name of the cookie you want, then check to see if it is = null.

function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}

function doSomething() {
var myCookie = getCookie("MyCookie");

if (myCookie == null) {
// do cookie doesn't exist stuff;
}
else {
// do cookie exists stuff
}
}

Check if cookie exists not working

Original response:

Unless you have a cookie with the key "samplename=itsvalue", your code will always evaluate to true. If the key is "samplename" and the value is "itsvalue", you should rewrite the check like this:

if (document.cookie.indexOf('samplename') == -1 ) {
alert("cookie");
}

This will tell you that the cookie does not exist.

To see if it does exist:

if (document.cookie.indexOf('samplename') > -1 ) {
alert("cookie exists");
}

Updating to better address this question:

What you are looking for in that check will always evaluate to true and throw the alert. Add the following function to your js and call it to check if your cookie exists.

function getCookie(name) {
var cookie = document.cookie;
var prefix = name + "=";
var begin = cookie.indexOf("; " + prefix);
if (begin == -1) {
begin = cookie.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = cookie.length;
}
}
return unescape(cookie.substring(begin + prefix.length, end));
}

You can then check your cookies with the following:

var myCookie = getCookie("samplename");

if (myCookie == null) {
alert("cookie does not exist");
} else {
alert("cookie exists");
}

Check if a PHP cookie exists and if not set its value

Answer

You can't according to the PHP manual:

Once the cookies have been set, they can be accessed on the next page
load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

This is because cookies are sent in response headers to the browser and the browser must then send them back with the next request. This is why they are only available on the second page load.

Work around

But you can work around it by also setting $_COOKIE when you call setcookie():

if(!isset($_COOKIE['lg'])) {
setcookie('lg', 'ro');
$_COOKIE['lg'] = 'ro';
}
echo $_COOKIE['lg'];

Javascript - Check if cookie exists

You can check the value of cookie if not undefined

    if (typeof(cookie)  === 'undefined'){
CONSOLE.LOG('no cookie');
} else {
CONSOLE.LOG(' cookie exist');
}

How would I test if a cookie is set using php and if it's not set do nothing

Use isset to see if the cookie exists.

if(isset($_COOKIE['cookie'])){
$cookie = $_COOKIE['cookie'];
}
else{
// Cookie is not set
}

Check if Cookie Exists

Response.Cookies contains the cookies that will be sent back to the browser. If you want to know whether a cookie exists, you should probably look into Request.Cookies.

Anyway, to see if a cookie exists, you can check Cookies.Get(string). However, if you use this method on the Response object and the cookie doesn't exist, then that cookie will be created.

See MSDN Reference for HttpCookieCollection.Get Method (String)

jQuery check if Cookie exists, if not create it

I think the bulletproof way is:

if (typeof $.cookie('token') === 'undefined'){
//no cookie
} else {
//have cookie
}

Checking the type of a null, empty or undefined var always returns 'undefined'

Edit:
You can get there even easier:

if (!!$.cookie('token')) {
// have cookie
} else {
// no cookie
}

!! will turn the falsy values to false. Bear in mind that this will turn 0 to false!

Check if a PHP cookie exists not worked

force $_COOKIE['mycookie'] for affect immediatly

<?php
if (!empty($_POST['submits'])) {
setcookie('mycookie', 'value', time()+ 36);
$_COOKIE['mycookie'] = 'value'; // <----
}
?>

<form action="" method="post">
<?php
if (empty($_COOKIE['mycookie'])) {
echo '<input type="submit" value="Up+1" id="submit" name="submits">';
} else {
echo 'NotSet';
}
?>
</form>


Related Topics



Leave a reply



Submit