简体   繁体   中英

if statements and sessions

I am attempting change the menu accordingly if the user is logged in or not. It keeps on coming up with this error:

Notice: Undefined variable: _SESSION in C:\\xampp2\\htdocs\\JobBoard\\menu.php on line 3 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>
'
?>

This is my page whwere I start the session 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";
}
}
?>

I'm sure it's quite an easy thing for someone. Please help

You have include 'menu.php'; before session_start() . PHP runs through in order, so when the include happens, the session hasn't been started yet.

Try putting the session_start() at the very beginning of the file instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM