2012-01-25 17 views
2

私はコメントページを持っており、ユーザロール(グローバル管理者(3)、管理者(2)、ユーザすべてのページにコメントを投稿します。ユーザロールの代わりにユーザロール(例:Admin、User)を入力してください。

私の問題は、ユーザーの役割の代わりにユーザーの役割(1,2、または3)がテーブルに配置されていることです。これはどのように修正するのですか?ここに挿入コードがあります。

<? 
include 'dbconnect.php'; 
$userName = $_SESSION['user']; 
$comment = $_REQUEST["comments"]; 
$game_ID = $_SESSION['id']; 
$query = "INSERT INTO comments (userName, comment, page_ID) VALUES ('$userName',  '$comment', '$game_ID')"; 
mysql_query ($query); 
echo "Thanks, your comment has been added!"; 
include 'close.php'; 
?> 

ログインコード:あなたが$_SESSION['user'] = true;を設定したが、後で...あなたのログインコードは、代わりにあなたのコードがある

$_SESSION['user'] = $row['userName']; 

言うべき$userName = $_SESSION['user'];でアクセスしようとしている

$errorMessage = ''; 
if (!empty($_POST['txtuserName']) && !empty($_POST['txtpassword'])) { 
include 'dbconnect.php'; 


$user = $_POST['txtuserName']; 
$password = $_POST['txtpassword']; 

// check if the user id and password combination exist in database 
$query = "SELECT userName, password, role FROM member WHERE userName = '$user' AND password = '$password'"; 

$output = mysql_query($query) or die('Query failed. ' . mysql_error()); 
$row = mysql_fetch_array($output); 

if (mysql_num_rows($output) == 1 AND $row['role']==1) { 
    // the user id and password match, 
    // set the session 
    $_SESSION['user'] = true; 
    // after login we move to the main page 
    header('Location: games.php'); 
    exit; 
} 
elseif (mysql_num_rows($output) == 1 AND $row['role']==2) { 
    // the user id and password match, 
    // set the session 
    $_SESSION['admin'] = true; 
    $_SESSION['user'] = true; 
    // after login we move to the main page 
    header('Location: games.php'); 
    exit; 
} 
elseif (mysql_num_rows($output) == 1 AND $row['role']==3) { 
    // the user id and password match, 
    // set the session 
    $_SESSION['admin'] = true; 
    $_SESSION['user'] = true; 
    $_SESSION['global'] = true; 
    // after login we move to the main page 
    header('Location: listUsers.php'); 
    exit; 
} 
else { 
    $error = 'The supplied username and/or password was incorrect. Please try again.'; 
} 

include 'close.php'; 
} 
?> 

答えて

1

を単にセッションにuserNameを配置します。

$_SESSION['userName'] = $row['userName']; 

それは、あなたのアプリケーションの残りの部分で利用できるようになります。

また、配列をセッションに配置することもできます。

$_SESSION['user'] = array('userName'=>$row['userName'], 'role'=>$row['role']); 

そして、以降のコードでは:だから、あなたはあなたのようコードをリファクタリングすることができるだろう

if ($_SESSION['user']['role'] == 2) { 
    // allow admin stuff 
} 
1

$_SESSION['user']の値をuserNameのデータベースに挿入しますが、残念ながらそれは単にtrueに設定されていますログイン時に

+0

あなたが私の前に答えたとき、私が入力された何が! –

+0

治療を受けました!みんなありがとう!ブロックフレデリック笑より速くする必要があります:) –

関連する問題