【发布时间】:2014-09-20 13:38:03
【问题描述】:
这是面板登录:
<?php include('header.php');
$login=mysql_query("select * from user")or die(mysql_error());
$row=mysql_fetch_row($login);
$level = $row[3];
session_start();
if (isset($_SESSION['username'])){
if ($level == '1')
{
header('location:admin/index.php');
}
if ($level == '2')
{
header('location:member/index.php');
}
}
?>
<body>
<div class="row-fluid">
<div class="span12">
<center>
</br>
</br>
<div id="container">
<div id="header">
<div class="alert alert-success"><label>Login to the System</label></div>
</div>
<form method="post">
<table>
<tr>
<td><label>User Name</label></td>
<td><input type="text" id="username" name="username" placeholder="username" required></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input type="password" id="password" name="password" placeholder="Password" required></td>
</tr>
<tr>
<td></td>
<td><button type="submit" id="submit" name="submit" class="btn btn-success">Login</button></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit'])){
$username=$_POST['username'];
$password=($_POST['password']);
$login=mysql_query("select * from user where user_name='$username' and user_password='$password'")or die(mysql_error());
$count=mysql_num_rows($login);
$row=mysql_fetch_row($login);
$level = $row[3];
if ($count > 0){
$_SESSION['username']=$row[1];
if ($level == 1)
{
header('location:admin/index.php');
}
if ($level == 2)
{
header('location:member/index.php');
}
}
else{ ?>
<!-- <script type="text/javascript">
alert("Error Login! Wrong Combination of Username and Password!");
</script> -->
<div class="alert alert-error">Error login! Please check your username or password</div>
<?php
}}
?>
</div>
</center>
</div>
</div>
</body>
</html>
我使用正确的信息登录,但系统没有转发到会员页面。 $_SESSION['username'] 不起作用。我尝试了所有在互联网上可以找到的解决方案,但都没有奏效。我不知道为什么这不起作用。任何帮助将不胜感激,谢谢。
我使用了这个代码:你能帮我修复它吗?非常感谢
【问题讨论】:
-
您确定设置了吗?
var_dump($_SESSION);显示了吗?你能告诉我们你设置它的代码吗? -
...你有没有考虑过不先销毁会话?
-
你为什么要开始你的会话,破坏它,然后再开始呢?您将永远无法访问
username,因为您破坏了该会话并且再也没有设置username。 -
标题看起来像
header('Location: admin/index.php');,你应该记住它不会隐式停止脚本。它的执行一直持续到exit;或脚本结束。顺便说一句,您不能在 http 正文之后或期间发送标头。 -
您无法在任何输出完成后设置 http 标头,因此在您的情况下,
header()将不起作用。