Php - Check if cookie value is equal to [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Anyone know how the get the following code to work?
Thanks!
if(isset($_COOKIE['first_name'] == "invisible" )) {
echo 'YES!';
}
else {
echo 'YES!';
}

You should have gotten the following error message with what you posted for code: (if your system was already set to catch errors). See my footnote.
Parse error: syntax error, unexpected '==' (T_IS_EQUAL), expecting ',' or ')' in...
Plus, you're using 2x YES!, so one of them has to be "NO!", being the latter.
You need to check if the cookie is set AND equals to.
if(isset($_COOKIE['first_name']) && $_COOKIE['first_name']== "invisible" ) {
echo 'YES!';
}
else {
echo 'NO!';
}
Your original code's brackets were malformed and were the cause of the parse error:
if(isset($_COOKIE['first_name'] == "invisible" ))
^ missing ^ misplaced
Had you placed the bracket in its right spot, and for example:
$_COOKIE['first_name'] = "John";
if(isset($_COOKIE['first_name']) == "invisible" ){
echo 'YES!';
}
else {
echo 'NO!';
}
It would have echo'd "YES" (for being set), since the cookie is indeed set, however it would have been a false positive to check if it equaled to "invisible".
So, you must use two seperate conditions.
Footnote:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.

Yes I know it. Just Change it like this
if (isset($_COOKIE['first_name'])) {
if ($_COOKIE['first_name'] == "invisible") {
echo 'YES!';
}
else {
echo 'YES!';
}
}

Related

if($_GET['info'] ==...) not checking [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wrote a little check to display succes messages whenever a form is validated succesfully. However the check I use doesn't work. it doesn't matter what $_GET['info'] is, it just shows all messages unless $_GET['info] is empty. so even when info = loggin it shows all three messages. any help?
if(!empty($_GET['info'])){
if($_GET['info'] == "succes-paid"){
echo "<p class='succes'>De bestelling/inschrijving is succesvol verlopen.</p>";
}
if($_GET['info'] == "succes-register"){
echo "<p class='succes'>U werd succesvol geregistreerd.</p>";
}
if($_GET['info'] == "login"){
echo "<p class='succes'>U werd succesvol ingelogd.</p>";
}
}
You have a semi-colon after each of your if statements. The semi-colon means "finish the statement". Therefore your program thinks that you are done with the if and everything in your braces is treated as a block separate from your if statement. That is why those blocks are always executing.
It's because of the semicolon right after the condition of the if.
That line is actually two statements. The first is the if with condition, followed by an 'empty' statement, ended by a semi-colon. The second is a compound statement (a block of statements within a set of { .. }. The compound statement doesn't have a condition at all, so it is always executed.
if($_GET['info'] == "succes-paid") ; {echo 'x'}
condition with no statement -----^ ^ ^--------^-- compound statement without condition
\_The semi-colon that separates them.
And you have this situation three times, hence three lines of output.

$_GET["id"] don't respond [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am a beginner in php , and i want make a test with GET .
I want display , for example "ok" on my php page if my id parameter is 1 but i have always 1 when i change the id parameter to another value .
Details :
when i make this url :
http://localhost:81/test/testajax.php?id=2
expected result :
not ok
obtained result :
ok
testajax.php
<?php
if($_GET["id"] = 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
One equal sign (=) sets the value of a variable. $foo = "bar"; would set $foo to store bar.
You want to use two equal signs (==), which is a comparison operator. ($foo == "bar") would check to see if $foo is equal to bar.
You can check the different types of operators at http://php.net/manual/en/language.operators.php
May be you should go through this basics before you start.
you should put two equal signs to compare.
if($_GET["id"]==1)
correct code:
<?php
if($_GET["id"] == 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
As you are setting the variable, the if statement is always equating to true
Just thought it was worth Noting this as that is the logical reason for your issue

PHP IF AND statement won't execute else section [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to use an IF AND statement in PHP to test for
The submit button being pressed and
That my $testemail varable is equal to one, ie valid email in a PHP script.
If the Email is good , I get:
Thank you, 1 // ( this is correct) and email is sent
If the email is bad I get:
Thank you, 0. // ( this is not correct ) and email is still trying to be sent with bad email address.
Because the script is outputting the 0 or 1 properly, I am certain it has tested the email and correctly deciding if it is good or bad ( hence the '0' and the '1') but the script never goes to the else statement. I can't seem to get the IF AND statement right. I have tried "1" and '1'.
Very confused. Thanks in advance.
if(!filter_var($emailtmp, FILTER_VALIDATE_EMAIL))
{
$testemail = "0";
}
else
{
$testemail = "1";
}
global $testemail;
var_dump($testemail);
if (isset($_POST['submit']) && ($testemail = 1) )
{
//Everything is good, proceed
sendEmail(preg_replace(array('/<td>.*href="\?remove.*<\/td>/i', '/<th.*
<\/th>/i'), '', $_SESSION['SHOPPING_CART_HTML']));
$result_message = "Thank You. $testemail ";
}
elseif (isset($_POST['submit']))
{
//Missing Required Information
$result_message = "You must use a Valid email addresse, $testemail ";
}
($testemail = 1)
should be
($testemail === 1)
Having just ($testemail = 1) simply sets the value of testemail to 1 (which for the purposes of the if, is true since its successful). == or better yet the more exact === is what you want for checking if the left & right values match.
The problem is you are assigning the value 1 to $testemail instead of comparing it.

PHP if x then image, else other image (so close) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
It's a basic question, but I've googled all sorts of variations of it & nothing that quite answers me. I want to display one of two images. One if the criteria is met & one if it is not met. This code works. If criteria is met, it does display the image. But there is no alternative
<? if(stripslashes($getfeedbackQryRow['CompanyID'])=='344'){?><img src='img1.png' alt='Todays Bite'><? }?>
Then I put this together, but it's still wrong
<?PHP
if ($getfeedbackQryRow['CompanyID']) == '344') {
print ("<IMG SRC =/img1.png>");
}
else {
print ("<IMG SRC =img2.png>");
}
?>
This one looks like it should be right, but it throws an error... It's probably a syntax issue. Does anyone know what I'm doing wrong?
You have an extra ")"
if ($getfeedbackQryRow['CompanyID']) == '344') {
Remove the ")" here ['CompanyID'])
if ($getfeedbackQryRow['CompanyID']) == '344') {
// ^------- this is causing your error
You should do something simple like this:
$image_name = $getfeedbackQryRow['CompanyId'] == '344' ? 'img1.png' : 'img2.png';
echo '<img src="' . $image_name . '">';

Parse error: syntax error, unexpected ':' in C:\wamp\www\xyz\contact-form.php on line 4 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm getting the error:
Parse error: syntax error, unexpected ':' in C:\wamp\www\xyz\contact-form.php on line 4
On a contact form that I'm trying to create for my portfolio site. I'm creating a simple check for the form submission and if it doesn't exist, it'll re-direct you back to to the index.
My code is:
if (!isset($_POST["save"]) || $_POST["save"] != ”contact”) {
header(“Location: index.php”);
exit;
}
Line 4 is the header() call, and I can't see what I've done wrong. I expect it's a small syntax error, any ideas?
The quotes aren't right, I'm assuming you copied it from a site, should be " not “
You're using funny quotes: (also known as smart quotes and curly quotes)
if (!isset($_POST["save"]) || $_POST["save"] != ”contact”) {
header(“Location: index.php”);
should be:
if (!isset($_POST["save"]) || $_POST["save"] != "contact") {
header("Location: index.php");

Categories