Incorrect Cookie Value While Reading In PHP - php

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?

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.

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.

Related

Cant set session variable to stable value on reloading the page

I'm making a php app. I used sessions for splitting users
for example:
<?php
session_start();
?>
<?php
$_SESSION["color"] = random_int(1, 2);
?>
I want to became a not changeable and unique value but on refresh it would change its value.
For example:
1,2,1,1,2,1,2,2,1
please help.
PHP session variables are not immutable. You will need to check for the value being set already before setting it to something else. Notice that isset will only work for variables that are not null, array_key_exists maybe more suitable depending on what data is being stored.
if (!isset($_SESSION["color"])) {
$_SESSION["color"] = random_int(1, 2);
}

warning:Cannot use a scalar value as an array

I have a very basic code.That involves sessions and array.
$abc=array();
$abc['name']=$db_name; //When i echo this one.It does echo the name i.e. 'Tilak'
$_SESSION['userpasswordmatch']=true;
$_SESSION['userpasswordmatch']['name']=$abc['name'];
echo $_SESSION['userpasswordmatch']['name'];
Now when i try to run the code.It shows me a warning stating cannot use a scalar value as an array as well as the echo part in the above code doesn't show anything.
Question :
1)Why am i seeing this error and what is the way to resolve this?
2)How to echo the above session.
Note :
1)$db_name is the name of the username that i get from the database.It works fine and i get the correct value in $db_name.
Update:
Same thing happens even if i do this :
$_SESSION['userpasswordmatch']=true;
$_SESSION['userpasswordmatch']['name']=$db_name;
echo $_SESSION['userpasswordmatch']['name'];
First you are defining $_SESSION['userpasswordmatch']=true;. true is a scalar value. Then $_SESSION['userpasswordmatch']['name']=$db_name; where you are treating $_SESSION['userpasswordmatch'] as an array.
You can simply do $_SESSION['userpasswordmatch']['name']=$db_name; and echo it.
No need for setting it to true. This would work -
$abc=array();
$abc['name']=$db_name; //When i echo this one.It does echo the name i.e. 'Tilak'
$_SESSION['userpasswordmatch']['name']=$abc['name'];
echo $_SESSION['userpasswordmatch']['name'];
Update
No need to define a blank array. When you are setting $_SESSION['userpasswordmatch']['name'] = $db_name;, $_SESSION['userpasswordmatch'] is already defined as an array.
$_SESSION['userpasswordmatch']['check']= true; // use this for that check

Why if i change to INPUT_GET this code will always execute the ''else'' piece of code

$num = $_POST['num'];
if(filter_input(INPUT_POST,'num', FILTER_VALIDATE_INT, array("options"=>array("min_range"=>5, "max_range"=>20)))===false) {
echo "Write a valid number between 5 and 20";
} else {
echo 'Great, your number is: '.$num;
}
there are many verbs you can use to send information between your website and your app, perhaps the most commonly used are GET and POST, GET requests are the ones that you can see on the URL, usually they come after a ? symbol, POST requests on the other hand are not shown on the URL, but the data is sent "hidden", in this case you can see you're using:
$num = $_POST['num']
if it is working is because in your HTML page, or on the same PHP page you have something like
<form action="mypage.php" method="post">
so, you can get that info via _POST, not _GET, the method/verb used must match on both sides.
If you try to get the value on _GET it won't be available, so the filter will fail check on http://php.net/filter_input, it clearly states:
or NULL if the variable_name variable is not set
you are performing a === comparison that won't return false, but NULL in this case, and therefor the only available code to be executed is the else part!

Remember me cookie with code igniter

I'm trying to figure out how to add in a remember me functionality into my script with code igniter. Here's what I"m attempting to do with the checkbox. I'm getting a syntax error for the if statment. Am I not using it correctly?
<?php echo form_label(form_checkbox( 'remember', 1, if (isset($this->input->cookie('xtrcook'))) { TRUE } ) . ' Auto-login in future.', 'remember' ); ?>
You have a syntax error because you're trying to pass this as an argument:
if (isset($this->input->cookie('xtrcook'))) { TRUE }
That's not how expressions work in PHP. You want this to be TRUE is the xtrcook cookie is set, so just pass in this:
isset($this->input->cookie('xtrcook'))
Actually, $this->input->cookie() returns either the cookie value or FALSE, and both are always "set" so isset() is not appropriate here, you can use ! empty() or !== FALSE instead:
$this->input->cookie('xtrcook') !== FALSE // evals to TRUE if the cookie exists
The whole thing:
<?php echo form_label(form_checkbox('remember', 1, $this->input->cookie('xtrcook') !== FALSE).' Auto-login in future.', 'remember'); ?>
It is a bit complicated still, might be easier to pass on the form_label() function and just write this:
<label>
<?php echo form_checkbox('remember', 1, $this->input->cookie('xtrcook') !== FALSE); ?>
Auto-login in future
</label>
I don't think so, this looks like a view, and PHP is a server-side language, meaning that once it renders the page, it's done - it won't respond to any clicks on checkboxes... You could make an AJAX call to maintain your structure, but I suggest is to check for your remember variable on the page you submit the form to, and then set the cookie if needed.

setcookie does not work

I have a simple file called index.php. I need to pass it a querystring that will be stored inside a never-expiring cookie. The file looks exactly like this :
<?php
if (isset($_GET['referrer_id']))
{
$querystringWithJunk = $_GET['referrer_id'];
$querystringArray = explode('/', $querystringWithJunk);
setcookie("referrer_id", $querystringArray[0], time() + 60*60*24*365*100);
}?>
However, no cookie is set. What is inside referrer_id is a simple integer (in the tests I made, it's 1). Function setCoookie return true and everything seems to works fine but no cookie is set. Am I doing something wrong?
Time is bigger than int, so I think result is negative, and then cookie is set into past, what means, it is deleted. Set time to 3 years instead of 100.

Categories