PHP: 运行 包含字符串的 mysql_query 语句,但该字符串包含在包含文件中设置的字符串,该文件在设置字符串之前调用

PHP: Run a mysql_query statement containing a string, but the string contains a string set in an included file that is called before the string is set

我的 PHP 主脚本的第一行包含一个 PHP 文件,如下所示:

// Include obfusctated MySQL queries
include "../../secure_storage/mysql_queries.php";

// $user_id stores the cookie as a variable. The cookie contains the user ID
$user_id = $_COOKIE['storerun_user_id'];

// Query the database to get the user's account information
$account_query = mysql_query($account_query_sql); 

包含的 PHP 文件包含这行代码:

$account_query_sql = "SELECT `first_name`, `last_name`, `email`, `password`, `referral_link`, `street_address`, `floor_apartment_number`, `notes`, `ecocash_number` FROM `users` WHERE `user_id` = '$user_id'";

原因是我混淆了 SQL 查询并将它们放在远离服务器根目录的目录中。

问题是包含的文件包含一个包含 $user_id 的 SQL 查询,但字符串仅在包含文件后设置。关于如何使用我的设置并能够 运行 mysql_query 有什么想法吗?我整天都在想办法解决这个问题。当然有办法做到这一点。

Dak,我不担心隐藏您的 sql 查询。但是,如果您确实想这样做,可以在查询数据库语句之前添加一个字符串替换语句,并编辑包含的文件以使用 'search_item',例如:

str_replace('search_item', $user_id, $account_query_sql);

包含的文件会像下面这样定义 $account_query_sql。

$account_query_sql = "SELECT `first_name`, `last_name`, `email`, `password`, `referral_link`, `street_address`, `floor_apartment_number`, `notes`, `ecocash_number` FROM `users` WHERE `user_id` = 'search_item'";

只需更改代码的顺序:

$user_id = $_COOKIE['storerun_user_id'];

include "../../secure_storage/mysql_queries.php";

PHP 无法进行时间旅行,因此您的代码必须以适当的顺序执行这些步骤 - 创建您的变量 在您尝试使用它们之前 ,而不是之后。

请注意,您很容易受到 sql injection attacks