PHP-用户登录后显示信息时出现问题
几天前,我已经实现了这一点,但在用户登录到他们的帐户后,我似乎无法在屏幕上显示会话ID。我有两个PHP,我一直在工作,我已经检查了这两个错误使用检查在线和短什么。因此,根据下面的内容,一旦用户登录,它将在站点标题中显示他们的会话ID,但即使我正确登录(我已检查它正在使用我的数据库登录),它所做的一切都会显示“您未登录!” Login.inc.phpPHP-用户登录后显示信息时出现问题,php,html,mysql,Php,Html,Mysql,几天前,我已经实现了这一点,但在用户登录到他们的帐户后,我似乎无法在屏幕上显示会话ID。我有两个PHP,我一直在工作,我已经检查了这两个错误使用检查在线和短什么。因此,根据下面的内容,一旦用户登录,它将在站点标题中显示他们的会话ID,但即使我正确登录(我已检查它正在使用我的数据库登录),它所做的一切都会显示“您未登录!” Login.inc.php <?php include '../dbh.php'; $email = $_POST['email'];
<?php
include '../dbh.php';
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM user WHERE email='$email' AND pwd='$pwd'";
$result = mysqli_query($conn, $sql);
if (!$row = mysqli_fetch_assoc($result)) {
echo "You are not signed in!";
} else {
$_SESSION['id'] = $row['id'];
}
header("Location: ../index.php");
?>
site_header.php
<head>
<?php
session_start();
?>
<link rel="stylesheet" type="text/css" href="vendors/css/normalize.css">
<link rel="stylesheet" type="text/css" href="vendors/css/grid.css">
<link rel="stylesheet" type="text/css" href="vendors/css/ionicons.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link href="https://fonts.googleapis.com/css?family=Lato:400,300,100,300,200italic" rel="stylesheet" type="text/css">
<title>iBPBuyer</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="index.php">HOME</a></li>
<li><a href="signup.php">SIGN UP</a></li>
<?php
echo "<form action='includes/login.inc.php' method='POST'>
<input type='email' name='email' placeholder='E-Mail'>
<input type='password' name='pwd' placeholder='Password'>
<button type='submit'>Login</button>
</form>";
?>
<?php
if(isset($_SESSION['id'])) {
echo $_SESSION['id'];
} else {
echo "You are not logged in!";
}
?>
</ul>
</nav>
</header>
iBPBuyer
您需要有
标题(“Location:../index.php”)代码>在发送或回显任何内容之前
由于头是用回音“您未登录”
发送的,因此它不能发送另一个头说要重定向
解决方法是跳过echo,或者使用Js重定向用户
if (!$row = mysqli_fetch_assoc($result)) {
header("Location: ../index.php");
} else {
$_SESSION['id'] = $row['id'];
}
这会管用的
Alos再往下,同样的方法也适用于session_start,因为它需要在头中发送会话cookie
所以这个头标签也造成了问题
<head>
<?php
session_start();
?>
检查您的错误日志,我打赌您已经发送了一个标题
因此,在启动会话或使用header()之前,最终无法发送或回显任何内容。另一个解决方法是使用输出缓冲区。看
你不只是想把东西重复出来。我建议使用经过身份验证的标志
<?php
include '../dbh.php';
session_start(); //THIS APPEARS TO BE MISSING
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM user WHERE email='$email' AND pwd='$pwd'";
$result = mysqli_query($conn, $sql);
if (!$row = mysqli_fetch_assoc($result)) {
//You have Session no matter what for this user
$_SESSION['authenticated'] = false;
} else {
$_SESSION['authenticated'] = true;
$_SESSION['id'] = $row['id'];
}
header("Location: ../index.php");
?>
那么在你看来,你可以做到这一点
<?php
if($_SESSION['authenticated']) {
echo $_SESSION['id'];
} else {
echo "You are not logged in!";
}
?>
将会话_start()
放在php的开头script@Ayaou,我不小心把它从我发布的代码脚本中删掉了,但它位于站点_header.php的顶部file@BenParry是否在登录中?另外,请使用准备好的语句