Booleans not working correctly [closed] - php

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
Programming a function to see if a user is 'active' or not.
function user_active($username) {
$username = sanitize($username);
$query = mysqli_query($_POST['x'], "SELECT * FROM users");
$row = mysqli_fetch_array($query);
if ($row['username'] === $username and $row['active'] === 1) {
return true;
} else {
return false;
}
}
and then....
if (user_active($username) === false) {
$errors[] = 'You have not activated your account. Please check your
email to activate your account.';
It only works when I test against $username but when I include $row['active'] === 1 it stops working.

=== checks for an identical match, meaning
$x === 1
will check if $x is an integer with the value of 1.
== checks for equality in the value only, so
$x == 1
will check if $x has a value of 1, whether it is a string or integer or something else, like a boolean true.
Try changing your statement to
$row['active'] == 1
or, if you know your variable is a string,
$row['active'] === '1'
Reference:
$a == $b....Equal....TRUE if $a is equal to $b after type juggling.
$a === $b....Identical....TRUE if $a is equal to $b, and they are of the same type.
http://php.net/manual/en/language.operators.comparison.php

Try this
The difference between == and === is
'===' check for equality between left side and right side with datatype too when comparing using '===' if data type is not equal then it returns false.
function user_active($username) {
$username = sanitize($username);
$query = mysqli_query($_POST['x'], "SELECT * FROM users");
$row = mysqli_fetch_array($query);
if ($row['username'] === $username and $row['active'] == 1) {
return true;
} else {
return false;
}
}

Related

PHP booleans var error [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 7 years ago.
I wrote a login script, but it doesn't work:
In this script I can't log in
if($_SESSION['logged'] == TRUE){
echo "logged in";
}
if($_POST['pass'] == "blabla"){
$_SESSION['logged'] = TRUE;
}
if($_GET['logout']){
$_SESSION['logged'] = FALSE;
}
Your first two lines are a comparison:
$var1 === TRUE;
$var2 == TRUE;
You want them to be a declaration
$var1 = TRUE;
$var2 = TRUE;
This is not legal syntax for setting a variable
var1 === TRUE;
And nor is this
var2 == TRUE;
Use = to set a variable to a value.
The === and == are comparison tests not value assignments.
This is also not going to do a test
if($var1 = TRUE){echo "3";}
it will set $var1 to be true, ditto the other 2 times you try this line for $var2 and $var3

If/Else return TRUE when not true in PHP [duplicate]

This question already has answers here:
This code is working fine if ($(this).val() == "Spine") but not ($(this).val() == "Spine"||"Brian")
(6 answers)
Closed 7 years ago.
I'm catching the name of an user logged on $userrow['name'] and then I'm using if/else to compare to 2 strings
$user1 = "admin";
$user2 = "Gil";
if ($userrow['name'] == $user1 || $user2 ) {
echo "true";
}else{
echo "false";
}
in this case my $userrow['name'] its = "Andres" and obviously Andres != Gil || Admin
What I'm doing wrong?
More maintainable is this logic:
$users = [$user1, $user2];
if (in_array($userrow['name'], $users)) {
// Username exists
}
Your logic is incorrect. I will try in english to say what your if statement does:
If $userrow['name'] equals the value held in the variable $user1, or if the value held in the variable $user2 is not falsey ...
In $user2 you have the value "Gil". Since "Gil" is not falsey (in other words, it is not false or null or zero), then it is considered to be true when evaluated within the context of an if statement. I think this is what you want to say instead:
if ($userrow['name'] == $user1 || $userrow['name'] == $user2 ) {
You are doing the first check right, but checking $user2 gives you true because if("Admin") is true.
Change
if ($userrow['name'] == $user1 || $user2 )
To
if ($userrow['name'] == $user1 || $userrow['name'] == $user2 )

Identical Strings in PHP not matching [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 7 years ago.
I'm trying to compare two strings (one from my database and another supplied by the user) and see if they match! The problem I'm having is that they don't seem to match - even though the strings seems to be exactly identical?
My PHP code is below:
public function Verify($pdo, $id, $token) {
$prepsql = $pdo->prepare("SELECT * FROM Profiles WHERE id = '$id' LIMIT 1");
$prepsql->execute();
$currentrow = $prepsql->fetch();
$current = preg_replace("/[^a-zA-Z0-9]+/", "", $currentrow["token"]);
echo '<p>'.var_dump($current).'</p>';
echo '<p>'.var_dump($token).'</p>';
$token = preg_replace("/[^a-zA-Z0-9]+/", "", $token);
if ($current == null || $current = "") {
return false;
} else {
if (strcmp($token, $current) == 0) {
return true;
} else {
return false;
}
}
}
And here is the webpage output:
string(244) "CAAW4HRuZBuB4BACA7GffOAwLHgmLgMMLGQxDAw8IJDCwahZAh0S4wZAcP8Q9DmMwsDpBq7jFcH1EzUIsZBbhKov12utoYFQns0HhgB5xKLeDqtZBRqavaNjNSn7KAcObZAEcavQCRbGlVKZBArfDEHskBSR8qAoU543DVTZCOyHm5oYNDVafwHl0bAkc4jyIhh2YHEPaNpWGC0FhezsSidOgLjnfFq8CeLVxHH0nUZBMLgAZDZD"
<p></p>string(244) "CAAW4HRuZBuB4BACA7GffOAwLHgmLgMMLGQxDAw8IJDCwahZAh0S4wZAcP8Q9DmMwsDpBq7jFcH1EzUIsZBbhKov12utoYFQns0HhgB5xKLeDqtZBRqavaNjNSn7KAcObZAEcavQCRbGlVKZBArfDEHskBSR8qAoU543DVTZCOyHm5oYNDVafwHl0bAkc4jyIhh2YHEPaNpWGC0FhezsSidOgLjnfFq8CeLVxHH0nUZBMLgAZDZD"
<p></p><p>Not authenticated</p>
Not authenticated just means that this function is returning false...
What on earth am I doing wrong? As per advice given on other similar Stack Overflow answers, I've used the regex function to basically only keep alphanumeric characters but that has made no difference? It isn't a trimming issue either as that didn't work!
Any help would be much appreciated!
Thanks!
Here is your problem:
if ($current == null || $current = "") {
// ^ now `$current` is "", an empty string
You assign a new value to $current, an empty string.
You probably want something like:
if ($current == null || $current == "") {
// ^^ now you are comparing

Losing array during variable assignment as part of a condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm attempting to troubleshoot some code and something is happening that I can't make any sense of... I have a $forum object which contains a threadExists method, which returns an associative array of any results found or false otherwise.
The following will print the array as expected:
if (!$test = $forum->threadExists($thread_id)) {
// do something
}
echo '<pre>';
var_dump($test);
echo '</pre>';
exit;
However; by adding a condition the screen will simply print bool(true):
if (!$test = $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id) {
// do something
}
echo '<pre>';
var_dump($test);
echo '</pre>';
exit;
Why is the array lost?
I'm using PHP 5.4.12.
operator precedence causes it to be interpreted like this
if (!($test = ($forum->threadExists($thread_id) || $test['topic_id'] != $topic_id))) {
// do something
}
more clearly,
$test = $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id;
if (!$test) {
// do something
}
you can force the correct behavior with parenthesis
if (!($test = $forum->threadExists($thread_id)) || $test['topic_id'] != $topic_id) {
// do something
}
personally, I would write it like the following, because I hate code that is even slightly tricky to read
$test = $forum->threadExists($thread_id);
if (!$test || $test['topic_id'] != $topic_id) {
// do something
}
Read it like this:
if(!$test = $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id)
Assign $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id to $test
Negate and check the value of $test
And since $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id evaluates to true, so you get true assigned to $test.
Fix is:
if((!$test = $forum->threadExists($thread_id))||($test['topic_id'] != $topic_id))
Parenthesis issue. You're assigning to $test the value of the compound condition, so it will have a boolean value based on whether either side of it resolves to true. Try:
if (!($test = $forum->threadExists($thread_id)) || $test['topic_id'] != $topic_id) {

Why does this statement always return false? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I need to find out why it says "Old Version" every time even when $a = true. If $a is true, it should do the if block and none of the others.
<?php
$launcherv = "13";
$gamev = "1326382442000";
$sessid = math.rand(1, 1000000000000000);
$ticket = math.rand(1, 1000000000000);
$user = "";
$password = "";
$version = "";
$a = false;
$b = false;
$c = false;
if ($version == $launcherv){
$a = true;
} else {
$a = false;
}
if ($user == ""){
$b = false;
} else {
$b = 'true';
}
if ($password == ""){
$c = false;
} else {
$c = true;
}
if ($a && $b && $c){
echo ($gamev.":".$ticket.":".$user.":".$sessid);
}
elseif(!$a){
echo "Old Version";
}
elseif(!$b){
echo "Bad Login";
}
elseif(!$c){
echo "Bad Login";
}
?>
if ($version == $launcherv){
$a = true;
} else {
$a = false;
}
Ok, so $version = "";, and $launcherv = "13";, so $a = false.
if ($a && $b && $c){
echo ($gamev.":".$ticket.":".$user.":".$sessid);
}
elseif(!$a){
echo "Old Version";
}
Because $a = false, it goes to the elseif, which evaluates to true (!false == true), and echo "Old Version"; is run.
You have set $a to false which returns the bool false.
When you check if $a doesn't equal true, it always doesn't return true, because $a never gets set to true. This is because $version = ""; and $launcherv = "13"; so that will not be true and it will skip to the else which sets $a to false again.
In the other question when you were told that you could do this, they told you that you could set $a to $version >= $launcherv which if evaluated would return true or false depending on whether the statement was true or false. Right after that though, they said...
The short variables are likely to confuse future readers. Therefore, you should write:
if ($version >= $launcherv) {
So, judging from the link you posted in the comments above:
rerepo.tk/mcv.php?user=test&password=test&version=13
I see that you're trying to pass in variables through the URL using register_globals
The problem here is that while you pass in the variables initially from the url, you redefine the values of those variables in your script to "" in your code. So initially, it was for example
$version = 13
from the assignment in your URL, but after you called
$version = ""
this overwrote what you assigned $version in the URL, since it was run afterwards.
Because of this, your value of version is always "", causing your script to constantly get the value of $a to be false, which has been explained in detail by Josh.
So basically, to fix this problem, all you have to do is remove this part of your code:
$user = "";
$password = "";
$version = "";
I tested this on my own computer, and it worked. However, in the case that this doesn't solve your problem, it is likely "register_globals" is turned off on your server.
Edit: If you are now using $_POST to pass the variables, then assign your variables as follows:
$user = $_POST["user"];
$password = $_POST["password"];
$version = $_POST["version"];
This is assuming that the variables you're posting is named the same way as when you had it passed in the URL.

Categories