简体   繁体   English

如何显示当前登录用户的数据

[英]How to display data from currently logged in user

I'm newbie in php coding.我是 php 编码的新手。 Just start it a few weeks ago.几周前就开始吧。 I am making login, register and profile pages.我正在制作登录、注册和个人资料页面。 I have a problem to display username that currently logged in. I tried all reference or tutorial and other forums.我在显示当前登录的用户名时遇到问题。我尝试了所有参考或教程以及其他论坛。 but still not working.但仍然无法正常工作。 Someone, please help me to solve.有人请帮我解决。

Here is my auth.php这是我的auth.php

<?php
include ("config.php");
session_start();

if(!isset($_SESSION["user"])) header("Location: login.php");

?>

and the login.phplogin.php

<?php 

require_once("config.php");

if(isset($_POST['login'])){

    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);

    $sql = "SELECT * FROM users WHERE username=:username OR email=:email";
    $stmt = $db->prepare($sql);

    $params = array(
        ":username" => $username,
        ":email" => $username
    );

    $stmt->execute($params);

    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    // Registered
    if($user){
        // verifikasi password
        if(password_verify($password, $user["password"])){
            // Make Session
            session_start();
            $_SESSION["user"] = $user;
            //success and redirect
            header("Location: index.php");

        }
    }
}
?>
<!DoctypeHTML>
<html>
<head>
</head>
<body>
</body>
</html>

Thank you谢谢

[PS = Sorry for my english] [PS = 对不起我的英语]

In index.php , write these lines.index.php ,写下这些行。

<?php 
  if(isset($_SESSION['user'])){
    echo $_SESSION['user']->fieldName; // here you need to retrieve fields
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅显示当前登录用户的数据库数据? - How to show data from database only from currently logged user? 使用PHP,MySql在当前(已登录)用户的仪表板上插入并显示数据 - insert and display data on a dashboard for currently (logged in)user using PHP, MySql 如何显示当前登录 CodeIgniter 的数据用户 - How can I show data user currently logged in in CodeIgniter 显示来自不同表的具有相同ID的登录用户的数据 - Display data for logged in user with same id from different table 如何从单独的PHP文件访问当前登录的Wordpress用户? - How can I access the currently logged in Wordpress user from a separate PHP file? 如何将region_id从当前登录的用户分配到实体表 - Symfony2 - How to assign a region_id, from the currently logged in user, to a entities table - Symfony2 Laravel 如何根据当前登录用户自定义用户配置文件 url - Laravel how to customize user profile url based on currently logged in user 如何基于当前记录的用户凭证从MYSQL数据库检索信息 - How to retrieve information from a MYSQL DB based on currently logged user credentials 如何只显示当前登录用户的清单-Laravel - How can you only show the checklists from the currently logged in user - Laravel 如何在Vue组件中获取当前登录用户的用户对象 - How to get User Object of Currently Logged User in Vue Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM