简体   繁体   English

PDO登录获取用户名

[英]PDO Login get Username

How do i echo out the Username when they got access to Admin.php? 当他们可以访问Admin.php时,我该如何回显用户名? I've tried to like include the files and made others sessions and such but it doesn't work. 我试着喜欢包含文件并进行其他会话,但这种方法不起作用。 User.php: user.php的:

<?php 
session_start();
include_once("anslutning.php");


class User{

    private $db;

    public function __construct(){
        $this->db = new Anslutning();
        $this->db = $this->db->dbConnect();

        }
    public function Login($username, $password){
        if(!empty($username) && !empty($password)) {
            $st = $this->db->prepare("select * from users where username =? and password=?");
            $st->bindParam(1, $username);
            $st->bindParam(2, $password);
            $st->execute();

            if($st->rowCount() == 1) {
                header("Location: admin.php");
                $_SESSION['inloggad']="true";

                }else {
                    echo "Fel Användarnamn eller Lösenord!";
                    }


            }else {
                echo "Var vänlig och skriv in ett Användarnamn och Lösenord!";
                }



        }





    }



?>

Admin.php admin.php的

<?php
session_start();
if($_SESSION['inloggad']!="true") {
    header ("Location: loggain.php");
    }
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
    <?php include "../include/head.php" ?>
    <title>Swebby | Admin</title>
</head>
<body>
    <div class="container">
        <a href="loggaut.php">Logga UT</a>
    </div>
</body>
</html>

So my question is.. How do i make it echo out the username on the Admin,php? 所以我的问题是..我如何使其在Admin,php上回显用户名? Thanks!! 谢谢!!

There are few things you need to look at. 您只需要看几件事。

  1. Place this $row = $st->fetch(PDO::FETCH_ASSOC); 放置这个$row = $st->fetch(PDO::FETCH_ASSOC); underneath $st->execute(); $st->execute();
  2. Create a session for your username when the user logs in. 用户登录时,为您的用户名创建一个会话。
    Place $_SESSION['username'] = $row['username']; 放置$_SESSION['username'] = $row['username']; underneath $_SESSION['inloggad']="true"; $_SESSION['inloggad']="true";
    This assumes "username" is the name of the username field in your database 假定“用户名”是数据库中用户名字段的名称

Then you can echo the username like this <?php echo $_SESSION['username'] ?> 然后,您可以像这样回显用户名<?php echo $_SESSION['username'] ?>

Happy Coding ! 编码愉快!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM