-2

特定のページをデータベースの特定のグループにのみ表示できるようにしたい。私のSQLテーブルは次のように設定されています:

テーブル:DD_users列:id | グループ| ユーザー名| 言い換え| ギルド| レベル| 塩

これは私が使おうとしているコードです:

// First we execute our common code to connection to the database and start the session
require("common.php");

// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
    // If they are not, we redirect them to the login page.
    header("Location: /DD/index.php");

    // Remember that this die statement is absolutely critical.  Without it,
    // people can view your members-only content without logging in.
    die("Redirecting to /DD/index.php");
}

if($_SESSION['user']['group'] == '0')
{
    // Destroy the session to make them log in again.
    unset($_SESSION['user']);

    // If they are not, we redirect them to the login page.
    header("Location: /DD/index.php");

    // Remember that this die statement is absolutely critical.  Without it,
    // people can view your members-only content without logging in.
    die("Redirecting to /DD/index.php");
}

// Everything below this point in the file is secured by the login system

これを試してみると、グループ1と2のみにページへのアクセスを許可したい場合に、すべてのユーザーグループ(0、1、および2)にページへのアクセスを許可します。

4

2 に答える 2

1

グループ1または2にあるかどうかを確認するコードはありません。コードを。にラップするだけifです。

if($_SESSION['group'] == '1' || $_SESSION['group'] == '2')

また、$ _SESSION['group']が。を使用して設定されていることを確認してissetください。設定されていない場合、最後ifは失敗します。

于 2012-12-20T04:09:36.213 に答える
0

別の方法で動作するようになりました:

require('common.php');
$charname = $_SESSION['user']['username'];
$query = "SELECT adminaccess, guild, username, class, level, active, canRegister, canNews, canActive 
      FROM DD_users 
      WHERE username = ?";
try
{
// These two statements run the query against your database table.
$stmt = $db->prepare($query);
$stmt->execute(array($charname));
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code. 
die("Failed to run query: " . $ex->getMessage());
}

// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();

//print_r($rows);
$group = $rows['0']['adminaccess'];
$guild = $rows['0']['guild'];
$username = $rows['0']['username'];
$class = $rows['0']['class'];
$level = $rows['0']['level'];
$accessAdmin = $rows['0']['adminaccess'];
$canRegister = $rows['0']['canRegister'];
$canNews = $rows['0']['canNews'];
$canActive = $rows['0']['canActive'];
于 2012-12-23T14:28:38.240 に答える