0

你好,我有这段代码,我认为函数 evalLoggedUser 有问题......因为现在函数显示错误的输出(返回 false 而不是 true),当我更改函数内部的 if 语句并这样做时:

if($numrows == 0) {
    return true;
}

它像我希望的那样工作......但我想像这样工作,并且 if 语句就像 if($numrows > 0)。我一整天都在搜索并试图弄清楚这一点,但什么也没有……我试图回显 $numrows var,但什么也没有回显……我也回显了 db 错误,一切都很好。任何帮助,将不胜感激。提前致谢。

<?php
session_start();
include_once("../db_includes/db_conx.php");
$user_ok = false;
$log_id = "";
$log_username = "";
$log_password = "";
// User Verify function
function evalLoggedUser($db_conx,$id,$u,$p){
    $sql = "SELECT ip FROM users WHERE id='$id' AND username='$u' AND password='$p' AND activated='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){  
        return true;
    }
}
if(isset($_SESSION["userid"]) && isset($_SESSION["username"]) && isset($_SESSION["password"])) {
$log_id = preg_replace('#[^0-9]#', '', $_SESSION['userid']);
$log_username = preg_replace('#[^a-z0-9]#i', '', $_SESSION['username']);
$log_password = preg_replace('#[^a-z0-9]#i', '', $_SESSION['password']);
// Verify the user
$user_ok = evalLoggedUser($db_conx,$log_id,$log_username,$log_password);
} else if(isset($_COOKIE["id"]) && isset($_COOKIE["user"]) && isset($_COOKIE["pass"])){
$_SESSION['userid'] = preg_replace('#[^0-9]#', '', $_COOKIE['id']);
    $_SESSION['username'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['user']);
    $_SESSION['password'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['pass']);
$log_id = $_SESSION['userid'];
$log_username = $_SESSION['username'];
$log_password = $_SESSION['password'];
// Verify the user
$user_ok = evalLoggedUser($db_conx,$log_id,$log_username,$log_password);
if($user_ok == true){
// Update their lastlogin datetime field
$sql = "UPDATE users SET lastlogin=now() WHERE id='$log_id' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
}
}
?>
4

2 回答 2

0

我找到了解决方案...当我包含 db_conx.php 时,似乎没有识别 var $db_conx...所以我没有包含此文件,而是将连接写入我在上面向您展示的同一个文件夹中并且它可以工作现在完美:

<?php
session_start();
include_once("../db_includes/db_conx.php");
$user_ok = false;
$log_id = "";
$log_username = "";
$log_password = "";
// User Verify function
function evalLoggedUser($db_conx,$id,$u,$p){
    $sql = "SELECT ip FROM users WHERE id='$id' AND username='$u' AND password='$p' AND activated='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){  
        return true;
    }
}

<?php
session_start();
$db_conx = mysqli_connect("xxxxxxxx","xxxxxxxx","xxxxxxxx","xxxxxxxx");
// Evaluate the connection
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
}
$user_ok = false;
$log_id = "";
$log_username = "";
$log_password = "";
// User Verify function
function evalLoggedUser($db_conx,$id,$u,$p){
    $sql = "SELECT ip FROM users WHERE id='$id' AND username='$u' AND password='$p' AND activated='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){  
        return true;
    }
}
于 2013-09-24T10:47:56.667 回答
-1

太多未知数无法确定,所以试试这个。将 SQL 语句后面的函数中的代码替换为:

if (($result = $db_conx->query($sql)) === false) {
   $errmsg .= '<p><b>User Eval:</b> ' . $db_conx->error . '</p><p>' . $sql . '</p>';
   echo $errmsg;
} elseif (!$result->num_rows) {
   //no records, so user cannot log in
} else {
    //return a true value in here
}

一旦你运行它一次,你应该能够准确地看到哪里出了问题

于 2013-09-24T04:00:30.203 回答