【发布时间】:2014-01-14 06:14:36
【问题描述】:
我正在为我的网站在 php 中创建一个简单的登录功能,现在我之前尝试了所有方法并且效果很好,然后我决定通过将我的所有功能组合在一个文件中来重新组织我的文件,我的数据库设置和连接在另一个文件中文件和我的会话配置(用于在我的本地主机上运行)在另一个文件中。
对我来说这样做的目的只是为了让我的代码保持干净、有条理,并且在将来对我来说易于理解。
这是我的登录页面:
<?php
include('session-config.php');
include('dbconnection.php');
include('functions.php');
include('password_hash_lib/password.php');
if (isset($_POST['data']))
{
$data = $_POST['data'];
$auth = json_decode($data);
$user_email = $auth->Email;
$user_pass = $auth->Password;
authenticate($user_email, $user_pass);
}
function authenticate($Email, $Password)
{
$HashedPassword = password_hash($Password, PASSWORD_DEFAULT);
$sql = "SELECT * FROM app_users WHERE user_email='$Email'";
$result = $db->query($sql);
$User = $result->fetch_object();
if ($User->user_email == '')
{
header("Location: login-page.html?msg=failed");
}
if (password_verify($Password, $User->user_password_hash))
{
$_SESSION["user_auth_details"] = $User->user_id . "+" . $User->user_email . '+' . $User->user_name . "+" . $User->user_display_image . "+" . $User->user_display_name;
header("Location:" . $_SESSION['page_url']);
}
else {
header("Location: login-page.html?msg=failed");
}
}
?>
这是我的数据库连接文件:
<?php
$db = new mysqli("xxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx", "xxxxxxxxxxxxx", "xxxxxxxxxxx");
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
?>
如您所见,我在 login.php 中包含了 dbconnection.php 文件,但每当我尝试调用 authenticate() 函数时,都会返回此错误:
注意:未定义变量:第 27 行 C:\xampp\htdocs\xxxxxxxx\login.php 中的 db
致命错误:在第 27 行对 C:\xampp\htdocs\xxxxxxx\login.php 中的非对象调用成员函数 query()
现在我对此有点困惑,因为我在我的 dbconnection.php 文件中定义了$db,并且我已经在我的 login.php 页面中包含了该文件,我希望它可以工作还是我错了?
【问题讨论】:
-
您的代码易受 SQL 注入攻击。
-
你还没有实例化$db
-
您从
authenticate()内部调用$db,因此需要将其声明为全局;global $db; -
我想知道我的代码以何种方式易受 sql 注入攻击?
-
“请问我想知道我的代码在哪些方面容易受到 sql 注入的影响?” --- 你可以在这里阅读 => stackoverflow.com/questions/60174/…