我的php函数有点问题。我花了很多时间找到错误,但没有成功。由于我非常新的更多"复杂" php编程,我可能会错过这个错误。
简短说明。
我已经创建了一个函数(用header.php编写),用于将用户活动插入到我的数据库中。代码如下
function userLog($type, $information){
include_once 'inc/dbconnect.inc.php';
$types = array("signup" => "",
"login" => "",
"logout_manual" => "",
"password_change" => "",
"password_reset_sendMail" => "Sent to: '".isset($information['mail']) ? $information['mail'] : "" ."'",
"password_reset_setNew" => "",
"mail_change_sendMail" => "Sent to: '".isset($information['mail']) ? $information['mail'] : ""."'",
"mail_change_verify" => "Changed from ".isset($information['old_mail']) ? $information['old_mail'] : ""." to ".isset($information['mail']) ? $information['mail'] : "");
$user_uid = $_SESSION['u_uid'];
$action = $type;
$description = $types[$type];
$date = date("Y-m-d H:i:s");
$browser = $_SERVER[HTTP_USER_AGENT];
$sql = "INSERT INTO userlogs (userlog_user_uid, userlog_action, userlog_action_description, userlog_date, userlog_browser) VALUES ('$user_uid', '$action', '$description', '$date', '$browser')";
mysqli_query($conn, $sql);
}
现在我想在登录脚本中调用该函数,如果用户已成功登录的话。 它看起来像这样:
if (isset($_POST['login_submit'])) {
include_once 'inc/dbconnect.inc.php';
$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
$_SESSION['inputs'] = array("uid" => $_POST['uid']);
//Check if inputs are empty
if (empty($uid) || empty($pwd)) {
$_SESSION['error'] = array("Bitte alle Felder ausfüllen!");
header("Location: ".$_SERVER['HTTP_REFERER']);
exit();
} else {
$sql = "SELECT * FROM users WHERE user_uid='$uid' OR user_mail='$uid'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck < 1) {
$_SESSION['error'] = array("Benutzername/E-Mail und/oder Passwort sind falsch!");
header("Location: ".$_SERVER['HTTP_REFERER']);
exit();
} else {
while ($row = mysqli_fetch_assoc($result)) {
//De-hashing the password
$hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
if ($hashedPwdCheck == false) {
$_SESSION['error'] = array("Benutzername/E-Mail und/oder Passwort sind falsch!");
header("Location: ".$_SERVER['HTTP_REFERER']);
exit();
} elseif ($hashedPwdCheck == true) {
//Log in the user here
$_SESSION['u_id'] = $row['user_id'];
$_SESSION['u_uid'] = $row['user_uid'];
$_SESSION['u_email'] = $row['user_email'];
$information = array();
userLog("login", $information);
header("Location: ".$root."index/index");
exit();
} else {
$_SESSION['error'] = array("Benutzername/E-Mail und/oder Passwort sind falsch!");
header("Location: ".$_SERVER['HTTP_REFERER']);
exit();
}
}
}
}
} else {
header("Location: ".$root."account/login");
exit();
}
这是&#34;奇怪的&#34;事情。如果我在logout form-action脚本中调用它,我的功能正在运行。
if (isset($_POST['logout_submit'])) {
$information = array();
userLog("logout_manual", $information);
session_unset();
session_destroy();
header("Location: ".$root);
exit();
}
所以我的功能实际上运行正常。但它只是从注销脚本中工作。 如果我想在我的登录脚本和我的注册脚本中调用它,它不起作用。 我也尝试在登录脚本本身而不是header.php中声明该函数,但它也没有用。 所以我认为根据这些信息,我的登录/注册脚本中存在错误,但我无法找到它。
也许有些人有个主意。