I'm having an issue with a login script. The rest of it works fine, but there is something odd happening here. The issue is that even though $IPCHK is returning true the elseif function does not execute. It only executes when I set $IPCHK to jibberish. Any help would be great. Thanks in advance
if ($Numrows == 0)
{
if ($Fail >= 3)
{
$Connection = connectToDb();
//return true, false,pending
$IPCHK = checkIP();
$IPCHK = true; //forcing it to be true and still broke
//If no ip id there
if($IPCHK == false)
{
$IP = getIP();
$Query = "INSERT INTO ip VALUES ('','$IP',Now())";
mysqli_query($Connection, $Query)
or die(error(mysqli_error($Connection)));
echo "You have failed to login too many times";
echo "<br />Please <a href='login.php'>try again</a> later.";
$Lock = true;
}
//If ip is there but timer is not up
elseif ($IPCHK == 'pending')
{
echo "You have failed to login too many times";
echo "<br />Please <a href='login.php'>try again</a> later.";
$Lock = true;
}
//Timers Up
elseif ($IPCHK == true) //here does not execute when it returns true
{
$_SESSION['FailedLogin'] = 0;
$Lock = false;
}
else
{
error("End of if check");
}
}
else
{
$Fail = 3 - $Fail;
$_SESSION['FailedLogin'] = $_SESSION['FailedLogin'] + 1;
$Error = $Error."<br />You have ".$Fail." attempts remaining";
}
}
In your Condition you have
elseif ($IPCHK == 'pending')
then
elseif ($IPCHK == true)
the second else will never execute because $IPCHK == 'pending' means also that $IPCHK == true
if you want to execute your second else you have to change the seconde condition to somethig like this
elseif($IPCHK == 'done')
or simply use === like this
elseif($IPCHK === 'pending')
then
elseif($IPCHK === true)
Lamari Alaa is correct, and the relevant documentation entry on type juggling can help understand why.
The following script outputs: boolean = string:
$test = true;
if( $test == 'pending' ) {
echo 'boolean = string';
} else if ( $test ) {
echo 'boolean != string';
}
This is because the string 'pending' is being coerced into a boolean value before being compared to the boolean value true. Since it evaluates as true, the first condition is taken. Consider replacing == 'pending' to === 'pending'
Related
I have this code I have set up that is supposed to read from JSON and output each array. Except when I use my foreach loop, I have an unreachable if statement. It's supposed to reach it if "type" is "rawbr".
I have confirmed that this has nothing to do with foreach by placing the same message in a row.
I wish to output this:
UnknownUser3: hey hxor? [To you]
Welcome to chat!
Here is my code:
innerchat.php:
<?php
session_start();
function tf($oz){
if($oz == 0){
return false;
} else if($oz == 1){
return true;
}
}
if(isset($_GET["room"]) && file_exists("data/".$_GET["room"].".json")){
$jsonF = file_get_contents("data/".$_GET["room"].".json");
$jsonD = json_decode($jsonF, true);
echo count($jsonD["msg"]);
// echo $jsonD["msg"][1]["type"];
foreach($jsonD["msg"] as $key => $message){
if($message["visibility"] !== "all"){
if(isset($_SESSION["ts_user"]) && $_SESSION["ts_user"] == $message["visibility"] && $message["type"] != "rawbr"){
echo "<font color='".$message["color"]."'><b><u>".$message["from"].":</u></b></font> ".htmlspecialchars($message["cont"])." [To you]<br />";
} else if($message["type"] === "message" && $message["visibility"] === "all"){
echo "<font color='".$message["color"]."'><b><u>".$message["from"].":</u></b></font> ".htmlspecialchars($message["cont"])." [normal message]<br />";
} else if($message["type"] === "rawbr" && $message["visibility"] === "all"){
echo $message["cont"]."<br />";
}
}
}
}
kb6k.json (the room we're working with)
{"name":"KillerBot 6000","desc":"A room with very harsh moderation. Proceed with caution!","max":600,"color":"#e0e0e0","whispersenabled":true,"forbiddenCommands":["/milk", "/bal"],"msg":[{"cont":"hey, hxor?","time":1,"color":"black","type":"message","visibility":"HxOr1337","from":"UnknownUser1"},{"cont":"Welcome to the chat!","time":0,"type":"message","color":"black","visibility":"HxOr1337","from":"Test"}]}
I know it couldn't possibly do anything to do with the JSON itself, since the other values are nearly identical apart from "visibility"
Ok, so I figured out that I put those if statements in $message["visibility"] !== "all"
The code:
<?php
session_start();
if(isset($_GET["room"]) && file_exists("data/".$_GET["room"].".json")){
$jsonF = file_get_contents("data/".$_GET["room"].".json");
$jsonD = json_decode($jsonF, true);
// echo $jsonD["msg"][1]["type"];
foreach($jsonD["msg"] as $key => $message){
if($message["visibility"] !== "all"){
if(isset($_SESSION["ts_user"]) && $_SESSION["ts_user"] == $message["visibility"] && $message["type"] != "rawbr"){
echo "<font color='".$message["color"]."'><b><u>".$message["from"].":</u></b></font> ".htmlspecialchars($message["cont"])." [To you]<br />";
}
} else {
if($message["type"] === "message" && $message["visibility"] === "all"){
echo "<font color='".$message["color"]."'><b><u>".$message["from"].":</u></b></font> ".htmlspecialchars($message["cont"])." [normal message]<br />";
} else if($message["type"] === "rawbr" && $message["visibility"] === "all"){
echo $message["cont"]."<br />";
}
}
}
}
my table field status is NULL[default], or it is 0, or 1. then i assign to PHP var $status. when value is NULL i want to display no icon, when value is 0, display a gray check image, when value is 1, display a green check image.
trouble is, NULL value shows a gray check image, 0 does not show a check image. somehow NULL and 0 are alike but only in one direction. what i mean is, regardless of how i conditionally test if var is null, not null, null but not zero, they get interpreted wrongly. it is confusing. there must be a simple straight foward way to keep NULL and 0 separate and distinct. i grab the value:
$status = $Card['status']; //from above array.
if ($status == 1) {
$status = '1';
} else if ($status == 0) {
$status = '0';
} else if ($status === NULL) {
$status = 'NULL';
}
then to display the images either gray, green, or none at all i am trying this:
if ($status == '1') {
echo "<img src='../images/status_check_green.png' />";
} else if ($status == '0') {
echo "<img src='../images/porc_check_gray.png' />";
} else if ($status == 'NULL') {
echo "<img src='' />";
}
}
i know i do not need the '' around the values, but i am trying to literalize everything to force valid comparisons. likely no need for someone to try unraveling my code; but to elucidate how to keep NULL and 0 separate. it's like i am missing something fundamental here. btw, when i stuff a js var with the PHP var, it gets the correct value; they just don't follow the comparison like i need them to.
ideas?
HI your issue is procedure order or not type checking the 0 ( depending if you want to catch false )
if ($status == 1) {
$status = '1';
} else if ($status == 0) {
$status = '0'; //<--- this runs on null because (null == 0) is true
} else if ($status === NULL) {
$status = 'NULL'; //<--- this block is un-reachable
}
Because your not type checking with === of 0 null will return true for that condition.
See this sandbox with and example using $status = null;
http://sandbox.onlinephpfunctions.com/code/1f3dd9d83d0026aa0f682b61bed2ba858ae285aa
Outputs:
'0'
If you change it to this
if ($status == 1) {
$status = '1';
} else if ($status === 0) {
$status = '0';
} else if ($status === NULL) {
$status = 'NULL';
}
As you can see here using the same setting for $status
http://sandbox.onlinephpfunctions.com/code/d86b0c60c06338d2d6ee1c1fa9d3fa7e08a22663
Outputs
'NULL'
The other way to fix it would be to switch them so the more specific one is first.
if ($status == 1) {
$status = '1';
} else if ($status === NULL) {
$status = 'NULL';
} else if ($status == 0) { //I would prefer if(!$status){ but I'm lazy
$status = '0';
}
Then 0 would catch false as well as 0 but not null as the block above it will catch it first. You can see this last one here
http://sandbox.onlinephpfunctions.com/code/3028f5826dcede29b14dd8cfc03618ea5830c12c
Which also outputs
'NULL'
Cheers!
So I made page with unban request and in users table I save if user already sent request or not so I don't have multiple unban requests from one user.Now when I check if user sent request it's not working.In database it stands 0 and it's still showing me error pop out.
Here is code, thanks for help in advance
if(isset($_POST['btn-unban_req']))
{
if($unban_sent = 0)//THIS IS WHERE I CHECK
{
//MY THIGNS HERE
if($connection ->query($unbanquery) === TRUE)
{
//MY THIGNS HERE
if ($connection->query($sentquery) === TRUE)
{
}
else
{
echo $connection->error;
}
}
else
{
}
}
else // AND I GET THIS ERROR EVEN IF IT STANDS 0 IN DATABASE
{
echo "Unban already sent!";
}
}
You are not comparing the values, this is assigning the values:
if($unban_sent = 0) // assigning values
This should be:
if($unban_sent == 0) // comparing values
Basic Example:
Lets say,
1 = 1 its assigning
1 == 1 its checking the condition will return TRUE.
For more help: PHP Comparison Operators
You're using this code which is wrong.
$unban_sent = 0
$unban_sent = 0 means to assign 0 to $unban_sent
It should be:
$unban_sent == 0
$unban_sent == 0 means $unban_sent is equal to 0
== is for comparison, = is for assignment, and === is for identical or same type.
More information at http://php.net/manual/en/language.operators.comparison.php.
In line 3 you missed a = for comparision. Instead, you set $unban_set to 0
Try it with this code:
if(isset($_POST['btn-unban_req']))
{
if($unban_sent == 0) //<- Now you are checking it here
{
//MY THIGNS HERE
if($connection ->query($unbanquery) === TRUE)
{
//MY THIGNS HERE
if ($connection->query($sentquery) === TRUE)
{
}
else
{
echo $connection->error;
}
}
else
{
}
}
else // AND I GET THIS ERROR EVEN IF IT STANDS 0 IN DATABASE
{
echo "Unban already sent!";
}
}
I am trying to verify a Minecraft username is paid or not.
By typing in the username at the end of the URL, it returns true or false.
$input = 'Notch';
function checkPlayer($player) {
$mcURL = 'http://www.minecraft.net/haspaid.jsp?user=';
$auth = file_get_contents($mcURL . $player);
if ($auth === true) {
echo $player. ' is valid';
} else {
echo $player. ' is not valid';
}
}
checkPlayer($input);
But it doesn't return true. By going to the page http://www.minecraft.net/haspaid.jsp?user=Notch, it does return true. How do I properly check? I think file_get_contents is the wrong function to use for this matter. I'm not sure though.
change this line :
if ($auth === true) {
with
if (trim($auth) == "true") {
I have this function that will check if a user already exists in the DB or not:
function checkTwitterAccount($user_id) {
$accountExists = false;
$a = "SELECT * FROM twitterAccounts WHERE user_id='".$user_id."'";
$ar=mysql_query($a) or die("Error selecting twitter account: ".mysql_error());
$ac = mysql_num_rows($ar);
if ($ac > 0) {
$accountExists = true;
}
return $accountExists;
}
Now when I call this function how do I actually know if the user is or is not in the DB?
I mean how do I call this function and how do I check the result?
Is the below right?
If (checkTwitterAccount($user_id) = true) {
DO THIS
}else{
DO THAT
}
Please help me I am new to it.
Thanks
if (checkTwitterAccount($user_id)) { //true
//do something
} else { //false
//do something
}
if (checkTwitterAccount($user_id) == true) {
//do this
}
else {
//do that
}
You have to use == rather than = as the = operand sets the value to true in the code you wrote, whereas the == operand compares the returned value to true.
Since your returning a true or false value you can simply use:
If (checkTwitterAccount($user_id)) {
//DO THIS
}else{
//DO THAT
}
Note: that your original line:
If (checkTwitterAccount($user_id) = true) {
would result in an assignment error because a single "=" means assign a value which can't be done to a function. You wanted:
If (checkTwitterAccount($user_id) == true) {
because "==" compares a value. Further, == only compares the value so for example 0 is the compares positively with false, any number compares positively with true, if you want to also compare type you us "===" like this:
0 == false //true
0 === false //false
false == false //true
false === false //true
1 == true //true
1 === true //false
true == true //true
true === true //true
function checkTwitterAccount($user_id) {
$user_id = intval($user_id);
$a = "SELECT `user_id` FROM `twitterAccounts` WHERE `user_id` = '".mysql_real_escape_string($user_id)."'";
$ar = mysql_query($a) or die("Error selecting twitter account: ".mysql_error());
$ac = mysql_num_rows($ar);
return ($ac > 0);
}
if(checkTwitterAccount($someid)) {
// Exists...
} else {
// No such ID in the DB
}
Note that comparison operator is == not = (which is assign).
So you could do:
if(checkTwitterAccount($someid) == true) {
However, it isn't necessary here.
Also remember to sanitize the data in the query.
if (checkTwitterAccount($user_id) == true){
do something if its true
} else {
do something if its flase
}
should work.. given that you provide argument to that function which seems to be
the int.. or id number from id column from users table in the db.
Basically you have a HTML Form that takes in a username and checks the database
for that users id number in users table in the database. Once it has this number it will
pass it on to the function checkTwitterAccount($user_id) if that function returns True that means guessing by the name of the function that the user has a twitter account else he does not have one.
you could do:
if (checkTwitterAccount($user_id) == true){
echo "This user has a twitter account";
} else {
echo "This user does not have a twitter account";
}
You can shorten the orig. function.
function checkTwitterAccount($user_id) {
$a = "SELECT * FROM twitterAccounts WHERE user_id='" . $user_id . "'";
$ar = mysql_query($a) or die("Error selecting twitter account: " . mysql_error());
return mysql_num_rows($ar) > 0; // boolean (will be true or false)
}
Then use the answer from max_. (See comparison operators)