我有这个 loginform.php 和这部分代码(这是从带有登录表单的 index.php 调用的):
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
index.php 有以下代码来在成功登录后显示用户:
<?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 的新手。请帮忙。抱歉,请假设所有文件都有一个会话启动。
你必须添加session_start()
在检查会话变量是否存在之前。
<?php
session_start();
if(isset($_SESSION['valid']))
....
session_start()
创建一个会话或恢复当前会话。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)