在$ _SESSION设置为有效的情况下登录后,isset()无法正常工作 [英] isset() didn't work after login with $_SESSION set to valid
问题描述
我有这部分代码的loginform.php(这是使用登录表单从index.php调用的):
I have this loginform.php with this part of code (this was called from index.php with a login form):
include("config.php");
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if($username == "" || $password == "")
{
echo "Either username or password field is empty.";
echo "<br/>";
echo "<a href='index.php'>Go back</a>";
}
else
{
$result = mysql_query("select * from users where user_username='$username' and user_password='$password'",$conn)
or die("Could not execute the select query.");
$row = mysql_fetch_assoc($result);
if(is_array($row) && !empty($row))
{
$validuser = $row['username'];
$_SESSION['valid'] = $validuser;
}
else
{
echo "Username and password do not match.";
echo "<br/>";
echo "<a href='index.php'>Go back</a>";
}
if(isset($_SESSION['valid']))
{
header("Location:index.php?");
}
}
}
但是之后,它应该再次显示没有登录表单的index.php
but after that it should've shown again the index.php without the login form
index.php具有以下代码,可在成功登录后显示用户:
index.php had this code to display the user after successful login:
<?php
if(isset($_SESSION['valid']))
{
include("config.php");
$result = mysql_query("select * from users",$conn);
echo "Welcome ".$_SESSION['valid']. "! <a href='logout.php'>Logout</a><br/>";
}
else
{
require('./loginform.php');
}
?>
使用此代码,登录表单不会显示,而是不显示任何内容.它应该显示已登录的用户.我不知道我错过了什么.我是php的新手.请帮忙.抱歉,请假定所有文件都有一个会话开始.
with this code the login form do not showed up but instead not showing anything. It should display the user that had logged in. I dont know what I missed. Im a newbie at php. Please help. sorry, please assumed that there is a session start at all file.
推荐答案
在检查会话变量是否存在之前,您必须添加session_start()
.
You have to add session_start()
before checking the existence of the session variable.
<?php
session_start();
if(isset($_SESSION['valid']))
....
session_start()
创建一个会话或恢复当前会话.
session_start()
creates a session or resumes the current one.
这篇关于在$ _SESSION设置为有效的情况下登录后,isset()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!