PHP booleans var error [duplicate] - php

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

Related

Why does this && not trigger as false? [duplicate]

This question already has answers here:
whats the difference in parentheses in IF statements?
(9 answers)
Closed 2 years ago.
I have this simple if statement but I do not get the results I expect. If all three vars match then I get not supported as expected. However I expect that as soon as I change one of the vars to a value that is not in the if statement, e.g. $Main = "SomethingElse", for the if statement to not match and therefor echo supported. However, supported is only returned if all three vars do not match the if statement.
Why does this happen?
$Main = "Main_Other";
$Backup = "Back_None";
$online = "no";
if ($online == "no" && $Main == "Main_Other" && $Backup == "Back_Other" || $Backup == "Back_None") {
echo "not support";
} else {
echo "supported";
}
In your example the if statement will always return true if the value of $backup is set to Back_None.
Try using below code. Here it will check $backup value first using || operator and then it will check the result with && operator
$Main = "Main_Other";
$Backup = "Back_None";
$online = "no";
if ($online == "no" && $Main == "Main_Other" && ($Backup == "Back_Other" || $Backup == "Back_None")) {
echo "not support";
} else {
echo "supported";
}

If statement working only until 1st else if [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I am trying to show a different navigation bar depending on a users authority. Only problem is that when i log on to the system it shows the first else if, regardless of the authority of the user. To ensure that the problem is in the loop i have tried switching the else ifs and the same happened. the code is in an external php file and i call the function in the top of each page. any suggestions ?
function checkAuth() {
session_start();
if(!isset($_SESSION['role'])) {
require_once('menu.php');
} else if ($_SESSION['role'] = "registered") {
require_once('regnav.php');
} else if ($_SESSION['role'] = "admin") {
echo "FDGFGFD";
require_once('adminnav.php');
}
}
Your issue is with this part: $_SESSION['role'] = "registered". The single = means you are assigning the value "registered" to variable $_SESSION['role'].
If you are evaluating to check something, you need to use == i.e. $_SESSION['role'] == "registered"
You'll have the same issue with the second elseif
You need to use a double = sign for any condition check. For any condition check in if or else if, you have to use == in the middle of the variables.
If you use only = that means it assigning the value in the $_SESSION['role']. Also you can use === for checking the value as well as the type of the variable.
Valid function is:
function checkAuth()
{
session_start();
if(!isset($_SESSION['role']))
{
require_once('menu.php');
}
else if ($_SESSION['role'] == "registered"){
require_once('regnav.php');
}
else if ($_SESSION['role'] == "admin"){
echo "FDGFGFD";
require_once('adminnav.php');
}
}
?>

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

Booleans not working correctly [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
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;
}
}

Categories