mysqlクエリの結果を使用してセッション変数を設定したいのですが、現在、設定するのに苦労しています。
私が持っているコードは次のとおりです。
<?php
ob_start("ob_gzhandler");
?>
<?php
// Include required MySQL configuration file and functions
require_once('config.inc.php');
require_once('functions.inc.php');
// Start session
session_start();
// Check if user is already logged in
if ($_SESSION['logged_in'] == true) {
// If user is already logged in, redirect to main page
redirect('../index.php');
} else {
// Make sure that user submitted a username/password and username only consists of alphanumeric chars
if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR
(!ctype_alnum($_POST['username'])) ) {
redirect('../login.php');
}
// Connect to database
$mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
// Check connection
if (mysqli_connect_errno()) {
printf("Unable to connect to database: %s", mysqli_connect_error());
exit();
}
// Escape any unsafe characters before querying database
$username = $mysqli->real_escape_string($_POST['username']);
$password = $mysqli->real_escape_string($_POST['password']);
// Construct SQL statement for query & execute
$sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . md5($password) . "'";
$result = $mysqli->query($sql);
// If one row is returned, username and password are valid
if (is_object($result) && $result->num_rows == 1) {
// Set session variable for login status to true
$_SESSION['auth_lvl'] = $Auth_lvl;
$_SESSION['logged_in'] = true;
redirect('../index.php');
} else {
// If number of rows returned is not one, redirect back to login screen
redirect('../login.php');
}
}
?>
次に、セッション変数を次のようにテストしています。
<?php
session_start();
echo "Login Status is:".$_SESSION['logged_in'];
echo "<br/>";
echo "Auth status is level:".$_SESSION['auth_lvl'];
?>
私のテストページでは、ログインしたセッションは正常に機能し、正しい情報が表示されますが、authlvl変数には何も表示されません。
そもそも変数を設定しているという情報を正しく呼んでいないとしか思えません。
どんな助けでもいただければ幸いです。
アラン。
私が気づき、提案を試みてきたのは、データベースから値を取得しようとするのではなく、定義を設定した場合、その値がテストページに返されるということです。すなわち
$_SESSION['auth_lvl'] = 'test';
これは、データベースからデータを取得し、それを$_SESSION['auth_lvl']として設定しようとしている方法が問題の原因であることを示しています。