Php PDO登录获取用户名
当他们访问Admin.php时,如何回显用户名?我试着喜欢包括文件和其他会议等,但它不工作。 User.php:Php PDO登录获取用户名,php,session,login,pdo,Php,Session,Login,Pdo,当他们访问Admin.php时,如何回显用户名?我试着喜欢包括文件和其他会议等,但它不工作。 User.php: <?php session_start(); include_once("anslutning.php"); class User{ private $db; public function __construct(){ $this->db = new Anslutning(); $this->db = $t
<?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
<?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>
斯威比行政
所以我的问题是。。我如何让它在管理员php上回显用户名?谢谢 你需要看的东西很少
$row=$st->fetch(PDO::fetch_ASSOC)代码>在$st->execute()下面代码>
放置
$\u会话['username]=$row['username']代码>在$\u会话['inloggad']=“true”下代码>
这假定“username”是数据库中用户名字段的名称
快乐编码 你在哪里创建User类的对象?我想你需要在Admin.php上包含/要求User.php,创建User类的对象,然后从User类调用函数来显示你需要显示的信息。我不是,这两个文件是我得到的。我写的每一个代码都在那里。我只是按照一个教程添加了一些额外的东西,我是PDO新手等等。你能帮我写些什么吗?查看有关对象/类的本教程是否有帮助: