我正在做一个元素,进入管理页面需要用户名和密码。一旦用户名和密码被更正或重定向到其他页面, session 就会创建,如果其中任何一个不正确。 我的代码是:
<?php
include_once './connection.php';
file_exists("connection.php");
$username = $_POST['username'];
$password = $_POST['password'];
$query=pg_query("select password from users where username like '".$username."' ");
$mypassword=pg_fetch_object($query)->password;
if(($password==$mypassword)and($mypassword!=null)){
$_SESSION["username"]="username";
$_SESSION["password"]="passwoed";
header("location:admin_page.php");
}
else{
header("location:attempt.php");
}
pg_close($connection);
?>
在管理页面,我的代码是:
<?php
session_start();
if(!isset($_SESSION["username"])){
header("location:homepage.php");
}
?>
<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
<font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font> </div>
</body>
问题是我被直接重定向到主页而无法进入管理页面。 session 注册有问题吗?但是,如果我将 php 代码更改为,我在管理页面上:
<?php
session_start();
if(isset($_SESSION["username"])){
header("location:homepage.php");
}
?>
我想显示我通过 $_SESSION[]
超变量保存的用户名。我收到的错误消息是 Notice: Undefined index: username in C:\xampp\htdocs\admin_page.php.
提前致谢
我按照你说的改了代码。
但是我是否写
<?php
session_start();
if(!isset($_SESSION["username"])){
header("location:homepage.php");
}
?>
<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
<font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font> </div>
或
<?php
session_start();
if(!isset($_SESSION["username"])){
header("location:homepage.php");
}
?>
<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
<font color='white'>Logged In As: <?php echo $username; ?></font> </div>
</body>
我得到了同样的错误。 “注意:未定义索引:C:\xampp\htdocs\admin_page.php 中的用户名。” 我不知道下一步该怎么做。请建议!!!
最佳答案
当我将代码更改为:
<?php
session_start();
include_once './connection.php';
file_exists("connection.php");
$username = $_POST['username'];
$password = $_POST['password'];
$query=pg_query("select password from users where username like '".$username."' ");
$mypassword=pg_fetch_object($query)->password;
if(($password==$mypassword)and($mypassword!=null)){
$_SESSION["username"]=$username;//instead of "username"
$_SESSION["password"]=$password;
header("location:admin_page.php");
}
else{
header("location:attempt.php");
}
pg_close($connection);
?>
谢谢 Geseft。
关于php - 用户 session 和显示用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24409666/