这是我的头。php
我面临的唯一问题是屏幕底部的通知,但登录正常。当我提交用户名和密码时,它会通过另一个页面。该通知显示if($_SESSION['login_id']=="")
出现错误。
<?php
var_dump($_SESSION);
if($_SESSION['login_id']=="")
{
?>
| <a href="index.php?page=login">Login</a> |
<?php
}
else
{
echo "<font color='green'> | Welcome ".$_SESSION['login_usr']." ".$_SESSION['login_id']." | Logged in as ".$_SESSION['type']." | </font>";
if($_SESSION['type']=="Admin")
{
?> <a href="index.php?page=main_record_officer">
<?php
}
if($_SESSION['type']=="Student")
{
?> <a href="index.php?page=main_student">
<?php }
if($_SESSION['type']=="Teacher")
{
?><a href="index.php?page=main_teacher">
<?php }
if($_SESSION['type']=="Accountant")
{
?><a href="index.php?page=main_accountant">
<?php } ?>
My Page</a> | <a href="logoff.php">Logoff</a> | <?php
}
?>
<a href="index.php?page=help">Help</a> |
这是我的登录名.php
<?php
if(isset($_POST['submit_btn']))
{
$user=$_POST['usrname'];
$pass=$_POST['password'];
$sql="select * from record_officer where admin_name='$user' and admin_password='$pass'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$count=mysql_num_rows($result);
if($count==1)
{
$_SESSION['login_usr']=$row['admin_name'];
$_SESSION['login_id']=$row['admin_id'];
$_SESSION['type']="Admin";
print "<script>window.location='index.php?page=main_record_officer';</script>";
}
else if($pass==""||$user=="")
{
echo "<center><font color='red'>Required fields are empty !!! </font></center><br><br>";
}
else
{
echo "<center><font color='red'>Invalid user name and password !!! </font></center><br><br>";
}
}
?>
请确保通过在页面顶部包含session_start()
来启动会话,至少是那些需要会话功能的页面。也就是说,将您的测试:if($_SESSION['login_id']=="")
替换为:
if( isset($_SESSION['login_id']) && ($_SESSION['login_id']=="") )
我建议使用空(http://php.net/empty)因为它首先检查变量是否已经定义,然后检查它是否有内容。
所以这个:
if($_SESSION['login_id']==""){
}
最好是:
if(!empty($_SESSION['login_id'])){
}
如前所述,您需要确保您的会话处于活动状态,例如,在文档的开头:
// PHP >= 5.4
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// ..or else:
if(session_id() == '') {
session_start();
}
请注意,每当您尝试访问尚未设置的变量时,都会遇到"未定义.."错误。
因此,您需要检查您的代码,并在必要时添加检查。
例如:
$user=$_POST['usrname'];
$pass=$_POST['password'];
应为:
$user = empty($_POST['usrname']) ? "" : $_POST["usrname"];
$pass = empty($_POST['password']) ? "" : $_POST["password"];
这样,变量$user和$pass将为",以防未发送变量。