gpt4 book ai didi

php - PDO: Assets 和 session 未正确显示

转载 作者:行者123 更新时间:2023-11-29 13:21:50 24 4
gpt4 key购买 nike

用户登录后,我很难显示用户的信息。

这是我的index.php 文件:

<?php
require 'core/db/connect.php';

if(isset($_SESSION['username'])){
include 'includes/menu.php';
}
else{
echo '<a href="register.php">Register</a><br />';
echo '<a href="login.php">Login</a><br />';
}
?>

对于 profile.php,每当我回显某些内容时,它不会回显:

<?php 
if(isset($_SESSION['username'])){
include 'core/db/connect.php';

$username = $_SESSION['username'];

$query = dbConnect()->prepare("SELECT username FROM users WHERE username = :username");
$query->execute(array(':username' => $username));
$row = $query->fetch();

echo 'Logged in as: ', $row['username'];
}

?>

这是我的第一个脚本,非常抱歉!

如果这有帮助,这是我的login.php:

<form method="POST">
<input type="text" name="username" placeholder="username"><br/>
<input type="password" name="password" placeholder="password"><br />
<input type="submit">
</form>

<?php
if(isset($_POST['username'], $_POST['password'])){
require 'core/db/connect.php';

$query = dbConnect()->prepare("SELECT username, password FROM users WHERE username = :username AND password = :password");
$query->execute(array(
':username' => $_POST['username'],
':password' => $_POST['password']
));

if($query){
header('Location: index.php');
}
else{
echo 'Username or Password is wrong.';
}
}
?>

最佳答案

在 PHP 中使用 session 之前,您必须调用 session_start(); .

关于php - PDO: Assets 和 session 未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20716800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com