У меня, кажется, возникают проблемы при входе в систему с использованием имени переменной сеанса из класса сеанса. Я не знаю, какой код мне не хватает. Страница приветствия не подберет пользователя, вошедшего в систему. Ниже приведен код.
class Session
{
private $message;
function __construct()
{
session_start();
}
function getVariable($varname)
{
if(isset($_SESSION['$varname'])) #17
return $_SESSION['$varname'];
else #19
{
$this->message = "No such variable in
this session";
return FALSE;
}
}
function storeVariable($varname,$value)
{
if(!is_string($varname)) #29
{
throw new Exception("Parameter 1 is not a
valid variable name.");
return FALSE;
}
else
$_SESSION['$varname'] = $value;
}
function getMessage()
{
return $this->message;
}
function login(Account $acct,$password) #44
{
if(!$acct->comparePassword($password)) #46
{
return FALSE;
}
$this->storeVariable("user_name", "user_name"); #47
return TRUE;
}
}
Ниже приведен код страницы входа в систему, где я вызываю объект сеанса
//First time form is displayed. Form is blank. //
if (!isset($_POST['Button'])) #26
{
$form->displayForm();
exit();
}
// Process form that has been submitted with user info //
else
{
$sess = new Session(); #34
try
{
$db = new Database("verybig.data"); #37
$db->useDatabase("database"); #38
$acct = new Account($db->getConnection(),"user");
}
catch(Exception $e)
{
echo $e->getMessage()."\n<br>";
exit();
}
if(!$sess->login($acct,$_POST['password'])) #76
{
$GLOBALS['message_1'] = $acct->getMessage().
" Please try again.";
$form->displayForm();
exit();
}
header("Location: companyhome.php"); #83
exit();
try
{
if($acct->selectAccount($newdata['user_name'])) #140
{
$GLOBALS['message_2'] =
"Member ID already used.
Select a new Member ID.";
$form->displayForm();
exit();
}
if(!$acct->createNewAccount($newdata)) #148
{
echo "Couldn’t create new account.
Try again later.";
exit();
}
$sess->storeVariable("user_name", $newdata['user_name']); #154
$sess->storeVariable("user_dept", $newdata['dept_id']);
А вот часть, где я хочу, чтобы сохраненная переменная сеанса появлялась на странице приветствия. Здесь страница приветствия просто не подберет сохраненные переменные сеанса. Вопрос в том, как передать переменные сеанса на эту страницу
if (!isset($_SESSION))
session_start();
echo $_SESSION['user_name']. "<br>";
$admin = FALSE;
$base_url = "companyhome.php";
$trail = "<a href='$base_url'>Home</a>";
if (!isset($_SESSION['user_name']))
header("Location: login-oo.php");#31
else
{
if (isset($_SESSION['user_name'])
&& isset($_GET['dept_id']))
{
$admin = $_SESSION['user_dept'] == $_GET['dept_id'];
}
$left_nav_links = array();
$page["browse_level"] =
isset($_GET['browse_level']) ?
$_GET['browse_level'] : "home";
Вот немного кода из класса учетной записи
class Account
{
private $user_name = NULL;
private $cxn; // database connection object
private $table_name;
private $message;
function __construct( mysqli $cxn,$table)
{
$this->cxn = $cxn;
if(is_string($table)) #17
{
$sql = "SHOW TABLES LIKE '$table'"; #19
$result = $this->cxn->query($sql);
if($result->num_rows > 0) #21
{
$this->table_name = $table;
}
else #25
{
throw new Exception("$table is not a table
in the database");
return FALSE;
}
}
else #32
{
throw new Exception("Second parameter is not a
valid table name");
return FALSE;
}
}
function selectAccount($user_name)
{
$user_name = trim($user_name); #42
$sql = "SELECT user_name FROM $this->table_name
WHERE user_name ='$user_name'"; #44
if(!$result = $this->cxn->query($sql))
{
throw new Exception("Couldn't execute query: "
.$this->cxn->error());
return FALSE;
}
if($result->num_rows < 1 ) #51
{
$this->message = "Account $user_name
does not exist!";
return FALSE;
}
else #57
{
$this->user_name = $user_name;
return TRUE;
}
}
function comparePassword($password)
{
if(!isset($this->user_name)) #66
{
throw new Exception("No account currently selected");
exit();
} #70
$sql = "SELECT user_name FROM $this->table_name
WHERE user_name ='$this->user_name' AND
password = md5('$password')";
if(!$result = $this->cxn->query($sql)) #74
{
throw new Exception("Couldn't execute query: "
.mysql_error());
exit();
}
if($result->num_rows < 1 ) #80
{
$this->message = "Incorrect password for
account $this->user_name!";
return FALSE;
}
else #86
return TRUE;
}