Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I have a member.php page which contains this piece of code:
<?php
if (isset($_SESSION['teacherusername']))
{
$username = $_SESSION['teacherusername'];
}
?>
Now in the teacherlogin.php I use "include" to include the code from the member.php page. The problem I have is that if I open up the teacherlogin.php page, it gives me an undefined variable error stating:
Notice: Undefined variable: username in ... on line 25
Why is it stating it is undefined because I have defined it in the member.php page?
Below is the code in the teacherlogin.php:
<?php
// PHP code
session_start();
ini_set('display_errors',1);
error_reporting(E_ALL);
// connect to the database
include('connect.php');
include('member.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
// required variables (make them explciit no need for foreach loop)
$teacherusername = (isset($_POST['teacherusername'])) ? $_POST['teacherusername'] : '';
$teacherpassword = (isset($_POST['teacherpassword'])) ? $_POST['teacherpassword'] : '';
$loggedIn = false;
$active = true;
if ($username){
echo "You are already Logged In: <b>{$_SESSION['teacherforename']} {$_SESSION['teachersurname']}</b> | <a href='./teacherlogout.php'>Logout</a>";
}
else{
echo "Please Login";
}
?>