使用会话中的信息

时间:2014-03-05 11:19:02

标签: php session login

我正在尝试创建会话以使用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);
?>

0 个答案:

没有答案