Php 正在检索用于用户登录的整行-mysqli

Php 正在检索用于用户登录的整行-mysqli,php,mysql,mysqli,Php,Mysql,Mysqli,我有一些用php编写的代码。我编写了一些代码,这样当用户成功登录时,他们的用户名会保存在会话中,然后我就可以回显$\u会话['user\u name']。我想知道是否有人可以帮我写几行代码,这样当用户登录时,我也可以检索该用户的用户id或用户电子邮件,并能够将其回显到另一个页面 这是我到目前为止所拥有的,但它并不是我想要的 $this->db_connection = new mysqli('localhost', 'root', '1', 'test'); //

我有一些用php编写的代码。我编写了一些代码,这样当用户成功登录时,他们的用户名会保存在会话中,然后我就可以回显$\u会话['user\u name']。我想知道是否有人可以帮我写几行代码,这样当用户登录时,我也可以检索该用户的用户id或用户电子邮件,并能够将其回显到另一个页面

这是我到目前为止所拥有的,但它并不是我想要的

  $this->db_connection = new mysqli('localhost', 'root', '1', 'test');


         // create a database connection, using the constants from config/db.php (which we loaded in index.php)
           if ($this->db_connection->connect_errno) {
         echo "Connection Failed " . $this->db_connection->connect_errno . "";
         }



         // if no connection errors (= working database connection)
         if (!$this->db_connection->connect_errno) {

             // escape the POST stuff
             $this->user_name = $this->db_connection->real_escape_string($_POST['user_name']);
             $this->user_password = $this->db_connection->real_escape_string($_POST['user_password']);


             // database query, getting all the info of the selected user
             $sql = "SELECT user_name, user_password, user_email
                     FROM members
                     WHERE user_name = '{$this->user_name}' AND user_password = '{$this->user_password}'";
             $query = $this->db_connection->query($sql);
            $result = $query->fetch_object();



            // if the username exists and if the password is a correct match
             if (($query->num_rows == 1) && ($this->user_password === $result->user_password)) {

         while ($row = mysqli_fetch_assoc($query)) {
  echo $row['user_email'];
    }
          $_SESSION['user_name'] = $result->user_name;
          $_SESSION['user_logged_in'] = 1;

          $_SESSION['user_login_status'] = 1;


          setcookie("_time", "cookie_value", time() + 3600);


        //redirect to this page if the user has logged in successfully
        header("Location: testing.php");


                 } 
            }
我曾尝试将while循环放在函数的不同部分,但仍然不起作用

session_start();
$_SESSION['user_name'] = $result->user_name;
$_SESSION['user_email'] = $result->user_email;
或者干脆

$_SESSION = $result
如果您已完成
fetch\u assoc
instad of
fetch\u object

我建议您使用一个身份验证密钥并将其存储在会话中,这比用户登录要好,因为用户登录非常不安全

/**
 * Returns an encrypted & utf8-encoded
 */
function encrypt($pure_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
    return $encrypted_string;
}

/**
 * Returns decrypted original string
 */
function decrypt($encrypted_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
    return $decrypted_string;
}
这是用于加密和解密的。 当用户登录时,修改sql并向encrypt添加一个随机值。将其也存储在会话中。检查是否记录时,从数据库中选择带有用户名的密钥,并将其与当前会话密钥进行比较

if ($result->key != $_SESSION["auth_key"]) {
// do whatever
}

将启动会话放在每页的顶部
session_start()我在文件的u构造函数中启动了会话。我添加了$_会话['user\u email']=$result->user\u email;现在很好,谢谢。如何创建身份验证密钥?你能发布一个关于身份验证密钥和如何使用它们的链接吗?@user3671179是的,我需要2分钟。我有了它就告诉你