2

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']として設定しようとしている方法が問題の原因であることを示しています。

4

3 に答える 3

2

問題が見つかりました。

読み取ったコード:

$result = $mysqli->query($sql); 

// If one row is returned, username and password are valid 
if (is_object($result) && $result->num_rows == 1) { 

// ADD THIS SET OF LINES
$data = mysql_fetch_assoc( $result );

// Replace auth_lvl with the name of your database column name
$Auth_lvl = $data['Auth_lvl'];


// Set session variable for login status to true 
$_SESSION['auth_lvl'] = $Auth_lvl;
$_SESSION['logged_in'] = true;

このコードでmysqliを使用したため、// ADD THIS SETOFLINESのコードで「i」が欠落していることに気づきませんでした。コードを次のように変更したとき:

$data = mysqli_fetch_assoc( $result );

すべてが命を吹き込みました。

助けてくれてありがとう。

于 2012-04-21T09:00:13.823 に答える
1

$ Auth_lvlに値を割り当てた場所がどこにも見当たらないので、次のようにします。

$_SESSION['auth_lvl'] = $Auth_lvl;

おそらくnullが割り当てられています。

于 2012-04-19T14:54:15.630 に答える
0

$Auth_lvlをどこに設定しているのかわかりません。そこにある結果と行を確認した後、空の変数に対してセッション変数を設定することになります。

      if (is_object($result) && $result->num_rows == 1) { 
                    // ADD THIS SET OF LINES
                    $data = mysql_fetch_assoc( $result );

                    // Replace auth_lvl with the name of your database column name
                    $Auth_lvl = $data['auth_lvl']'

                    // Set session variable for login status to true 

                    $_SESSION['auth_lvl'] = $Auth_lvl;
                    $_SESSION['logged_in'] = true;


                    redirect('../index.php'); 

次に、logged_inセッション変数を使用して、ブール値trueとして設定し、通常のテキストとしてエコーアウトしようとします。

if ( $_SESSION['logged_in'] ) { echo "Login status is: True"; }

それがお役に立てば幸いです。

-ダン

于 2012-04-19T14:54:02.160 に答える