我正在尝试创建会话以使用user_id转到他的个人资料页面。使用会话电子邮件(您需要登录)我想获取登录人员的user_id。此时他不会给我user_id。我认为我做的会议存在问题
为此,我在user.class-file
中创建了这个函数public function getUserByEmail($email)
{
$db = new Db();
$select = "SELECT user_id FROM tblusers WHERE email = '" . $_SESSION["email"] . "';";
$result = $db->conn->query($select);
while ($row=mysqli_fetch_assoc($result))
{
return $row['user_id'];
}
}
这是登录功能
public function Login()
{
$salt = "ab4p73wo5n3ig247xb1w9r";
$db = new Db();
$select = "SELECT * FROM tblusers WHERE email = '" . $db->conn->real_escape_string($this->Email) .
"' AND password = '" . $db->conn->real_escape_string(md5($this->Password . $salt)) . "';";
$result = $db->conn->query($select);
if($result->num_rows == 1)
{
// logged in, naam session ophalen en je schermt springt door naar challenge.php
$_SESSION["loggedin"] = true;
$_SESSION["name"] = $this->Name;
$_SESSION["surname"] = $this->Surname;
$_SESSION["email"] = $this->Email;
header("Location: challenge.php");
}
else
{
throw new Exception("Please enter correct username and password");
}
}
这是我的前端页面中的代码。当我使用!sset($ _ SESSION)时,他总是重定向到index.php页面。当我留下那段代码时,他告诉我电子邮件未定义。
<?php
include_once("classes/User.class.php");
session_start();
if (!isset($_SESSION["email"])) {
header("Location: index.php");
}
$user = new User();
$email = $_SESSION["email"];
$number = $user->getUserByEmail($email);
?>