【问题标题】:If statement does not seem to work如果语句似乎不起作用
【发布时间】:2018-10-03 15:16:34
【问题描述】:

我的逻辑或托管服务器可能有问题,因为当我在本地尝试它时,它可以完美运行!但是,当我上传它时,无论申请人电子邮件激活的值是什么,它总是执行第二条语句??

这快把我逼疯了,请帮忙!

<?php
 // Santize the provided inputs
$applicant_email = filter_var(stripAndCleanHTML($_GET['applicant_email']), FILTER_SANITIZE_EMAIL); # santize the email
$applicant_token = stripAndCleanHTML($_GET['applicant_token']); # santize the token

/**************** Find the applicant that has the same email *******************/

  $database_connection = Database::database_connect();

  $find_email_query = $database_connection->prepare('SELECT * FROM applicants WHERE applicant_email = :applicant_email && applicant_token = :applicant_token LIMIT 1');

  $find_email_query->execute(['applicant_email' => $applicant_email, 'applicant_token' => $applicant_token]);

  if ($find_email_query->errorCode() > 0) {

    if (DEBUG === true) {

        echo 'There was an issue in searching for the email Big Boss: <br>';
        print_r($find_email_query->errorInfo());
        die();

    } else {

        header('location:../404.shtml', true, 404);
        die();

    }

  }

  $applicants = $find_email_query->fetchAll();

  foreach ($applicants as $applicant) {

    $applicant_username         =   (string) stripAndCleanHTML($applicant['applicant_username']);
    $applicant_password         =   (string) stripAndCleanHTML($applicant['applicant_password']);
    $applicant_name             =   (string) stripAndCleanHTML($applicant['applicant_name']);
    $applicant_phone            =   (string) stripAndCleanHTML($applicant['applicant_phone']);
    $applicant_birthdate        =   (string) stripAndCleanHTML($applicant['applicant_birthdate']);
    $applicant_city             =   (string) stripAndCleanHTML($applicant['applicant_city']);
    $applicant_country          =   (string) stripAndCleanHTML($applicant['applicant_country']);
    $applicant_major            =   (string) stripAndCleanHTML($applicant['applicant_major']);
    $applicant_major_type       =   (string) stripAndCleanHTML($applicant['applicant_major_type']);
    $applicant_exp_years        =   (string) stripAndCleanHTML($applicant['applicant_exp_years']);
    $applicant_cv               =   (string) stripAndCleanHTML($applicant['applicant_cv']);

    $applicant_email_activated  =   (int) stripAndCleanHTML($applicant['applicant_email_activated']);

  }

 if ($applicant_email_activated === 1) {

  include '../../includes/job_app/email_has_been_activated.inc.php';

 } elseif ($applicant_email_activated === 0) {

   include '../../includes/job_app/email_confirmed.php';

 }

 ?>

这是我用来清理值的函数:

function stripAndCleanHTML($to_clean)
{
    return htmlspecialchars(strip_tags(stripslashes(trim($to_clean))));
}

这是数据库类:

class Database
{

    private const DB_HOST     =   'domain.com';
    private const DB_NAME     =   'ats';
    private const DB_CHARSET  =   'utf8';
    private const DB_USER     =   'public_user';
    private const DB_PASS     =   '1F#kaH$!q5r2as';

    public static function database_connect()
    {

        try {

            // setting DSN (Data Source Name)
            $dsn = 'mysql:host=' . Database::DB_HOST . ';' . 'dbname=' . Database::DB_NAME . ';' . 'charset=' . Database::DB_CHARSET;

            // creating a PDO (PHP Data Object) instance
            $pdo = new PDO($dsn, Database::DB_USER, Database::DB_PASS);
            $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

            return $pdo;

        } catch (Exception $e) {

            if (DEBUG === true) {

                echo $e->getMessage().'<br>';
                die();

            } else {

                die();

            }
        }

        return $db_info;

    }
}

【问题讨论】:

  • 如果没有看到stripAndCleanHTML() 的代码并知道$applicant['applicant_email_activated'] 的值,就无法回答这个问题
  • var_dump($applicant_email_activated) 显示什么?
  • 我认为问题出在 stripAndCleanHTML 中,可能是版本问题
  • @Chopi 我不建议这样做,== 触发类型杂耍,这可能会产生各种意外行为。我总是推荐 === (必要时使用明确的演员表)而不是 == 除非 == 是唯一明智的选择

标签: php if-statement boolean php-7.2


【解决方案1】:

在我删除 (int) 并将压缩数字放入单引号后它确实有效!疯了吧!!?

我猜托管公司的服务器以一种特殊的方式处理 PHP!或者也许我已经用很多废话来充实应用程序,正如你们中的一些人会同意的那样,尽管如此,我已经做到了,我可以回家睡觉了,因为我知道我的婴儿应用程序安全无恙!

非常感谢您的提示和指导,祝您有美好的一天!并且不要忘记要很棒。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2013-07-23
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多