请帮助我的退出脚本。我很抱歉愚蠢的错误我是非常新的PHP。请向我提供有关如何解决此问题的详细信息和示例。非常感谢你。
登录页面:有4种类型的用户。每个用户都将获得一个单独的主页。
<?php
session_start();
require_once('common/config.php');
if(isset($_POST['username']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM `users` WHERE username='".$username."' and password='".$password."'";
$result = mysql_query($sql) or die(mysql_error());
$fetched = mysql_fetch_array($result);
if ($fetched['user_type'] == "admin")
{
header('location: adminhomepage.php');
}
else if ($fetched['user_type'] == "po")
{
header('location: pohomepage.php');
}
else if ($fetched['user_type'] == "pw")
{
header('location: pwhomepage.php');
}
else if ($fetched['user_type'] == "ps")
{
header('location: pshomepage.php');
}
else
{
header('location: invalid.php');
exit();
}
}
?>
主页:例如,这是管理员主页。
<?php
session_start();
if (isset($_SESSION['username']) && ($_SESSION['username'] !== 1))
{
header('Location: login.php');
}
?>
退出页面
<?php
session_start();
$_SESSION['username'] =0;
?>
退出按钮
<form action = "logout.php">
<input id="logoutbutton" type="submit" value="Logout">
</form>
答案 0 :(得分:1)
<?php
session_start();
unset($_SESSION['susername']);
$_SESSION['susername'] = "";
session_destroy();
header("location:index.php");
?>
session_start();
if (isset($_SESSION['uname']) == "")
{
require_once('index.php');
}
$user_name = $_POST['user_name'];
$user_pass = $_POST['user_pass'];
$_SESSION['susername'] = $user_name; // or other value
答案 1 :(得分:0)
退出页面 -
<?php
session_start();
$_SESSION['username'] =0;
?>
在访问之前,您必须先启动session
。
答案 2 :(得分:0)
在logout.php
中,请更新此代码......
<?php
session_start();
if(isset($_SESSION['username']))
{
unset($_SESSION['username']);
header('Location: login.php');
}
?>
答案 3 :(得分:0)
使用unset($ _ SESSION ['susername'])或session_destroy
答案 4 :(得分:0)
编辑:
根据您的新信息,很明显您从未真正设置$ _SESSION [&#39;用户名&#39;]值。
if ($fetched['user_type'] == "admin")
{
$_SESSION['username'] = $username;
header('location: adminhomepage.php');
}
else if ($fetched['user_type'] == "po")
{
$_SESSION['username'] = $username;
header('location: pohomepage.php');
}
else if ($fetched['user_type'] == "pw")
{
$_SESSION['username'] = $username;
header('location: pwhomepage.php');
}
else if ($fetched['user_type'] == "ps")
{
$_SESSION['username'] = $username;
header('location: pshomepage.php');
}
else
{
header('location: invalid.php');
}
exit();
你的问题是你的比较。
if ($_SESSION['username'] != 1)
如果$ _SESSION [&#39;用户名&#39;]未设置,null,字符串,false等等,则为true ...
这可能更符合您的要求。
if (isset($_SESSION['username']) && is_string($_SESSION['username']) && strlen($_SESSION['username']))
你需要在这里解决你的SQL注入问题
$sql = "SELECT * FROM `users` WHERE username='".$username."' and password='".$password."'";
使用mysql_real_escape_string转义变量或使用正确的预处理语句使用PDO。
您还应将密码存储为hashes with password_hash()。获取用户,将存储的哈希值与password_verify进行比较。
if (!password_verify($password, $fetched["password"])) {/* wrong password, show error or something */}