我有一个包含这段代码的 member.php 页面:
<?php
if (isset($_SESSION['teacherusername']))
{
$username = $_SESSION['teacherusername'];
}
?>
现在在teacherlogin.php 中,我使用“include”来包含member.php 页面中的代码。我遇到的问题是,如果我打开teacherlogin.php 页面,它会给我一个未定义的变量错误,说明:
Notice: Undefined variable: username in ... on line 25
为什么它说它是未定义的,因为我已经在 member.php 页面中定义了它?
以下是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";
}
?>