【发布时间】:2014-08-16 01:48:24
【问题描述】:
我正在做一个项目,在其中进入需要用户名和密码的管理页面。一旦用户名和密码被更正或重定向到其他页面,会话就会创建,其中任何一个页面都不正确。 我的代码是:
<?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>
问题是我直接被重定向到主页而无法进入管理页面。会话注册有问题吗?但是,如果我将 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 中的用户名。” 我不知道下一步该做什么。请推荐!!!
【问题讨论】:
-
您在设置会话的页面上的任何位置都没有
session_start()。 -
将 if 后面的代码包装到 else{} 语句中。问题是你的header已经设置好了,但是你的html中的php也编译好了,没有$_SESSION["username"]
标签: php html css session usersession