我有一个页面名称authentication.php
一旦点击登录它会在同一页面显示会话信息,但是当我尝试在userprofile.php
等其他页面中显示相同的已记录用户信息时,它赢了&# 39; t显示任何东西
<?php
if ($_POST) {
$logdb = new PDO('mysql:host=localhost;dbname=mydbname', 'root', '12345');
$logdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $logdb->prepare("SELECT * FROM users WHERE username=:username AND password=:password");
$stmt->bindParam(":username", $_POST['username']);
$stmt->bindParam(":password", $_POST['password']);
$stmt->execute();
$atributes = $stmt->fetch(PDO::FETCH_OBJ);
if ($atributes) {
session_start();
//$_SESSION["loggedIn"] = true;
$_SESSION['username'] = $_POST['username'];
$_SESSION["ID"] = $atributes->ID;
$_SESSION["country"] = $atributes->country;
} else {
echo 'Login failed!';
}
} else {
echo '<form name="login" action="" method="POST">
Username: <br />
<input type="text" name="username"/><br />
Password: <br />
<input type="password" name="password"/><br />
<button type="submit">Login</button>
<a href="register.php">Register</a></form>';
}
?>
<?php
echo ($_SESSION["ID"]);
echo ($_SESSION["username"]);
echo $atributes->country;
?>
以下是我在新页面中使用的内容,用于显示已记录的用户信息,但未获得任何结果,也没有错误,请您帮助我吗?
<?php
session_start();
if(!isset($_SESSION['username'])) {
$_SESSION["ID"] = $atributes->ID;
$_SESSION["username"] = $atributes->username;
$_SESSION["country"] = $atributes->country;
} ?>
<?php
echo ($_SESSION["ID"]);
echo ($_SESSION["username"]);
echo $atributes->country;
?>
同样在我页面的标题处,我有一个像这样的导航栏
<?php if(!isset($_SESSION['username'])) {
include($root . 'includings/unloggedU.php');
}else{
include($root . 'includings/loggedU.php');
} ?>
答案 0 :(得分:0)
在第一页设置$_SESSION['country'] = $atributes->country;
,因为代码中当前没有设置会话变量。
在第二页上,您应该可以访问所有会话变量 - 您可以单独测试这些变量以查看它们是否已设置。
<?php
session_start(); // always put this at the top of the page see Ryan's comments
echo $_SESSION["ID"] . "<br />";
echo $_SESSION["username"] . "<br />";
echo $_SESSION["country"] . "<br />";
?>