Reputation: 17
I'm setting a cookie to stop certain things loading up on my page if I'm an admin user, and to do this I'm creating a cookie in php, reading it, and then echoing out the value via php on my page.
So my full code snippets are:
<?php
setcookie("preview", "true", time() - 3600); // Kills Existing
setcookie("preview", "true", time() + 3600); // Sets New
?>
-
<?php
if(isset($_COOKIE["preview"])){
$admin_preview = ($_COOKIE["preview"]);
}
else{
$admin_preview = "false";
}
?>
-
<?php
echo $admin_preview;
?>
So when I echo out $admin_preview
, I'm expecting true
to be the value, but instead I'm getting 1
.
I'm using Firefox 62.0
so I can't view the actual cookie value, but I've obviously done something wrong. Any ideas where or how?
Upvotes: 1
Views: 586
Reputation: 84
Do this instead :
$admin_preview = var_export($_COOKIE["preview"], true);
This will state that the value of the "preview" cookie is to be used as a string instead of a boolean. In php boolean words (true/false) translate to 1 and 0 in that order, you have to explicitly state that the value is to be used as a string if that's what you wish for.
Upvotes: 2
Reputation: 678
In PHP 1 means true, you can check it like if($admin_preview) //which will accept it as true,
also you can use filter var.
var_Dump(filter_var("TRUE", FILTER_VALIDATE_BOOLEAN));
//or
var_Dump(filter_var(1, FILTER_VALIDATE_BOOLEAN));
for more information about boolean in php you can check http://php.net/manual/tr/language.types.boolean.php
also you are not doing it wrong dude.
Upvotes: 0