if语句和会话

时间:2014-04-02 23:17:17

标签: php mysql session

如果用户已登录,我正在尝试相应地更改菜单。它不断出现这个错误:

注意:未定义的变量:第3行的C:\ xampp2 \ htdocs \ JobBoard \ menu.php中的_SESSION menu.php

<?php

$useremail = $_SESSION['login_user'];

if($useremail==NULL)
{
 $loggedinout='<div id="menulogin"><a href="register.php">Register</a> | <a href="login.php">Login</a> &nbsp </div>';
 }
 else
 $loggedinout='<div id="menulogin"><a href="logout.php">Logout</a> &nbsp </div>';

echo'
<link rel="stylesheet" type="text/css" href="stylesheets/menu.css" />
<div id="menucontainer">' . $loggedinout . '<div id="menulogo"><img src="images/logo.png" /></div>
<div id="searchmenu"></div>
'
?>

这是我开始会话的页面 的login.php

<?php
include 'menu.php';
include 'db_connect.php';
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// email and password sent from Form 
$myemail=addslashes($_POST['email']); 
$mypassword=addslashes($_POST['password']); 

$sql="SELECT userID FROM users WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);


// If result matched $myemail and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myemail");
$_SESSION['login_user']=$myemail;

header("location: index2.php");
}
else 
{
$error="location: test.php";
}
}
?>

我确信对某人来说这很容易。请帮忙

1 个答案:

答案 0 :(得分:4)

您之前有include 'menu.php'; session_start()。 PHP按顺序运行,因此当include发生时,会话尚未启动。

尝试将session_start()放在文件的最开头:

session_start();
include 'menu.php';
include 'db_connect.php';