无法使用 REGEX 获取 PHP 脚本名称

Unable to get PHP script name using REGEX

我遇到了很大的挑战。我有我的功能 where(),它跟踪用户在网站上访问的位置,获取脚本文件名并提供描述并插入数据库。

脚本一直运行良好,直到最近,在最近从 MySQL 5.6.405.6.47

我不确定这是否与它有关,但几天后我发现它不再起作用了。

我们的职能:

function where($scriptname = "index", $userid, $update=1){
    if (!is_valid_id($userid))
        die;

    if (preg_match("/details.php/i", $scriptname))
        $where = "Browsing File Details (ID $_GET[id])";
    elseif (preg_match("/files.php/i", $scriptname))
        $where = "Browsing Files";
    elseif (preg_match("/account-info.php/i", $scriptname))
        $where = "Browsing Account Info (ID $_GET[id])";
    elseif (preg_match("/upload.php/i", $scriptname))
        $where = "Uploading File";
    elseif (preg_match("/account.php/i", $scriptname))
        $where = "Browsing User Control Panel";
    elseif (preg_match("/search.php/i", $scriptname))
        $where = "Searching For Files";
    elseif (preg_match("/forums.php/i", $scriptname))
        $where = "Viewing Forums";
    elseif (preg_match("/index.php/i", $scriptname))
        $where = "Browsing Homepage";
    elseif (preg_match("/mailbox.php/i", $scriptname))
        $where = "Viewing Messages";
    elseif (preg_match("/comments.php/i", $scriptname))
        $where = "Viewing Comments";
    elseif (preg_match("/recover.php/i", $scriptname))
        $where = "Recovering Account";
    elseif (preg_match("/bookmarks.php/i", $scriptname))
        $where = "Viewing Bookmarks";
    elseif (preg_match("/getfile.php/i", $scriptname))
        $where = "Downloaded File (ID $_GET[id])";
    elseif (preg_match("/faq.php/i", $scriptname))
        $where = "Reading FAQ Page";
    elseif (preg_match("/friends.php/i", $scriptname))
        $where = "Viewing Friends";
    elseif (preg_match("/admin.php/i", $scriptname))
        $where = "Managing Admin Panel";
    else
        $where = "Unknown Location";

    if ($update) {
        // Worked until a few days ago. No site changes were made prior for quite some time. 
        //$query = sprintf("UPDATE users SET page=".sqlesc($where)." WHERE id ='%s'", mysql_real_escape_string($userid)); 
        // Now using line below, which does insert into row if I use my own variable. 
        $query = "UPDATE users SET last_access='" . get_date_time() . "', page=" . sqlesc($where) . " WHERE id=" . $userid;
        $result = SQL_Query_exec($query);
    }
        return $where;
}

现在,我已尝试使用以下代码将问题范围缩小到导致问题的原因。目前,上面的函数只插入

Unknown Location

到数据库,无论查看哪个页面。

我把它归结为:

$stringtest = "This inserts into database!";
$query = "UPDATE users SET last_access='" . get_date_time() . "', page=" . sqlesc($stringtest) . " WHERE id=" . $userid;

效果很好,但是它没有满足任何 $where 条件。

我试过各种正则表达式来匹配文件名都没有用。

知道我做错了什么吗?

提前致谢!

我注意到 $scriptname 的默认值是 "index"。我认为这个引用 "index.php"?而您的匹配模式也在寻找文件扩展名。 (顺便说一句,未转义的 . 并不意味着字面上的“.”。它是 'any one character' 的 RegEx 规则。所以是的,它会起作用,但有点意外。)

您要传递什么样的值?在比较 运行 之前尝试 die/dumping 该函数中 $scriptname 的值,作为完整性检查以验证是否收到了预期类型的​​输入。可能是您看到了合法的(如果只是意外的)失败。

对于 SQL 部分使用准备语句,如上所述。

假设,$scriptname 包含文件名,没有扩展名,也不是绝对路径,并且根据提供的代码,我认为那里不需要 preg_match()。只需 switch ... case 即可。

更好的是,如果你想要一种带有描述的可管理位置列表(无论如何你每次都会重复使用它们),你可以像下面那样使用关联数组(可以由一个单独的函数返回)并按照如下:

$locations = array(
  'index' => 'Browsing Homepage', 
  'forums' => 'Browsing Homepage,
  //  and so on for all of your files
);

// And to get the description - assuming case matters, otherwise use strtolower()
$desc = isset($locations[$scriptname]) ? $locations[$scriptname] : 'Unknown Location';