为什么会这样“如果"?语句被触发? [英] Why does this "if" statement get triggered?
问题描述
过去5个小时,我一直在努力研究为什么如果被触发...
I've been working for the past 5 hours on why does this if get triggered...
让我向您展示代码并向您解释:
Let me show you the code and explain you :
<?php
require_once "ConnectDB.php";
$link2 = $link;
$key = $posthwid = "";
$err = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST["key"])){
$err = "Thanks for the ip (" .$_SERVER['REMOTE_ADDR']. "), have a good day! (1)";
}
else{
$key = trim($_POST["key"]);
}
$hwid = $_POST["hwid"];
if(empty($err)){
$sql = "SELECT hwid, idkey, length, created_at FROM money WHERE idkey = '" .$key. "'";
$row = mysqli_query($link, $sql);
if(mysqli_num_rows($row) < 2){
while($result = mysqli_fetch_assoc($row)) {
if($result["idkey"] == $key)
{
$err = "key";
if($result["hwid"] == "")
{
$err = "nohwid";
$sql2 = "UPDATE IceCold SET hwid = '" .$hwid. "' WHERE idkey = '" .$key. "'";
if(mysqli_query($link2, $sql2)){
$hwid = $result["hwid"];
mysqli_close($link2);
echo "debug";
}
else {
$err = "Oops! Something went wrong. Contact the support.";
}
}
if ($hwid !== $result["hwid"]) {
$err = "Contact the support";
}
elseif($_SESSION["admin"] == true) {
//Do special stuff
}
else {
///do other checks
if($created_at > $date){
$err = $hwid;
} else {
$err = "The key date is too old, buy a new one.";
}
}
}
else{
$err = "The key you entered was not valid.";
}
} mysqli_close($link);
} else {
$err = "multiple entry, contact support";
}
}
} else {
$err = "Thanks for the ip (" .$_SERVER['REMOTE_ADDR']. "), have a good day! (3)";
}
echo $err;
?>
因此,基本上,我有一个名为$link
的mysqli_connect连接数据库文件,并且正在为我的程序设计一个Liscence API.我的程序将发送一个带有"idkey"和"hwid"的请求,并且正在等待该hwid返回.我在sql数据库中有一个仅注册了密钥的条目,并且试图通过生成带有id和随机hwid的POST请求来使程序wotk出现,但未成功.如果怪异地移动变量,那是因为调试.
So basically, I have this Connect DB file with a mysqli_connect called $link
and I'm designing a liscence API for my program. My program will send a request with the "idkey" and "hwid" and is waiting for the hwid to come back. I have an entry in my sql databse with only a key registered and I've trying to make my program wotk by generating POST request with the id and a random hwid but I've found no success. If variables are weirdly moved around, It's because of the debugging.
现在,在我当前的设置下,我得到了我不知道为什么?!"支持联系回复.如果我能够获得此遮篷,则请求和键是正确的. 这可能是一个愚蠢的错误,但我无法理解...
Right now, with my current setup, I get the Contact the support response which I don't understand why?!? The request and the key are correct if I'm able to get this awnser. It's probably a stupid mistake but I jsut can't figure it out...
预先感谢您的帮助
编辑:我要引用的if
语句是这样的:
Edit: the if
statement I'm referring to is this:
if($hwid !== $result["hwid"])
我修复的代码中有错别字,但这不是问题, 至于elseif,这将破坏代码的执行顺序并破坏其背后的逻辑(如果有道理的话).
There was a typo in the code that I fixed but it wasn't the issue, as for the elseif, that would destroy the order of execution of the code and destroy the logic behind it(If that made sense).
奇怪的是,经过一些测试,我发现我发送的第二个SQL请求不想执行($ sql2),并且httpd日志中没有错误...您可以执行两个请求吗?我尝试创建$ link2,但它没有任何改变
Weirdly, after some tests, I found out that the second SQL request I send doesn't want to be executed ($sql2) and there is no error in httpd logs... Can you execute two requests? I tried to create $link2 but it doesn't change anything
EDIT :找到的解决方案
if($result["hwid"] == "")
{
$sql2 = "UPDATE money SET hwid = '" .$_POST["hwid"]. "' WHERE idkey = '" .$key. "'";
if(mysqli_query($link2, $sql2)) {
$newhwid = $_POST["hwid"];
mysqli_close($link2);
}
else {
$err = "Oops! Something went wrong. Contact the support.";
}
}
elseif ($_POST["hwid"] != $result["hwid"]) {
$err = "Contact the support";
}
if($_POST["hwid"] == $newhwid || $_POST["hwid"] == $result["hwid"] ) {
/// do other checks
}
推荐答案
该条件之前的条件if($row['hwid'] = "")
是赋值.此代码将$row['hwid']
的值更改为空字符串,导致其后的条件为true.我假设您打算在$row['hwid']
为空的情况下将==
写入 test .否则,将其写为if
语句是没有意义的.
The condition before that one, if($row['hwid'] = "")
, is an assignment. This code is changing the value of $row['hwid']
to an empty string, causing the condition after it to be true. I assume you meant to write ==
to test if $row['hwid']
is empty; otherwise it doesn't make sense to write this as an if
statement.
顺便说一下,尚不清楚此if
语句是否不应该是else if
.这里的其余分支是else if
(或elseif
,在PHP中是相同的),因此您应该考虑是否也错过了该分支上的else
.
By the way, it's not clear whether this if
statement shouldn't be an else if
. The rest of the branches here are else if
(or elseif
, which is the same in PHP), so you should consider whether you have missed out an else
on this one too.
这篇关于为什么会这样“如果"?语句被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!