2013-02-27 29 views
-1

首先,我從http://www.webestools.com/scripts_tutorials-code-source-15-personal-message-system-in-php-mysql-pm-system-private-message-discussion.html如何製作多用戶?

得到這個編碼之前,我使用相同的編碼創建用戶頁面和管理頁面。我編輯相同的代碼來查看不同的用戶和管理頁面。我在同一時間在同一瀏覽器上運行..它運行正常。但是對於這種編碼,我使用戶和管理員使用相同的編碼,相同的瀏覽器並運行相同的時間。我先登錄管理員,然後登錄用戶。在我登錄用戶之後,我刷新了管理頁面。我在管理中使用的會話更改爲用戶頁面。

connexion.php

<?php 
include('config.php'); 
?> 
    <div class="header"> 
<a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a> 
</div> 
<?php 
//If the user is logged, we log him out 
if(isset($_SESSION['username'])) 
{ 
//We log him out by deleting the username and userid sessions 
unset($_SESSION['username'], $_SESSION['userid']); 
?> 
<div class="message">You have successfuly been loged out.<br /> 
<a href="<?php echo $url_home; ?>">Home</a></div> 
<?php 
} 
else 
{ 
$ousername = ''; 
//We check if the form has been sent 
if(isset($_POST['username'], $_POST['password'])) 
{ 
//We remove slashes depending on the configuration 
if(get_magic_quotes_gpc()) 
{ 
$ousername = stripslashes($_POST['username']); 
$username = mysql_real_escape_string(stripslashes($_POST['username'])); 
$password = stripslashes($_POST['password']); 
} 
else 
{ 
$username = mysql_real_escape_string($_POST['username']); 
$password = $_POST['password']; 
} 
//We get the password of the user 
$req = mysql_query('select password,id from users where username="'.$username.'"'); 
$dn = mysql_fetch_array($req); 
//We compare the submited password and the real one, and we check if the user exists 
if($dn['password']==$password and mysql_num_rows($req)>0) 
{ 
//If the password is good, we dont show the form 
$form = false; 
//We save the user name in the session username and the user Id in the session userid 
$_SESSION['username'] = $_POST['username']; 
$_SESSION['userid'] = $dn['id']; 
?> 
<div class="message">You have successfuly been logged. You can access to your member area.<br /> 
<a href="<?php echo $url_home; ?>">Home</a></div> 
<?php 
} 
else 
{ 
//Otherwise, we say the password is incorrect. 
$form = true; 
$message = 'The username or password is incorrect.'; 
} 
} 
else 
{ 
$form = true; 
} 
if($form) 
{ 
//We display a message if necessary 
if(isset($message)) 
{ 
echo '<div class="message">'.$message.'</div>'; 
} 
//We display the form 
?> 
<div class="content"> 
<form action="connexion.php" method="post"> Please type your IDs to log in:<br /> 
<div class="center"> 
<label for="username">Username</label><input type="text" name="username" id="username"value="<? 
php echo htmlentities($ousername, ENT_QUOTES, 'UTF-8'); ?>" /><br /> 
<label for="password">Password</label><input type="password" name="password" id="password" />br /> 
<input type="submit" value="Log in" /> 
</div> 
</form> 
</div> 
<?php 
} 
} 
?> 

的index.php

<?php 
include('config.php') 
?> 

<?php 
//We display a welcome message, if the user is logged, we display it username 
?> 
Hello<?php if(isset($_SESSION['username'])){echo ' '.htmlentities($_SESSION['username'],ENT_QUOTES, 'UTF-8');} ?>,<br /> 
Welcome on our website.<br /> 
You can <a href="users.php">see the list of users</a>.<br /><br /> 
<?php 
//If the user is logged, we display links to edit his infos, to see his pms and to log out 
if(isset($_SESSION['username'])) 
{ 
//We count the number of new messages the user has 
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['userid'].'" and user1read="no") or (user2="'.$_SESSION['userid'].'" and user2read="no")) and id2="1"')); 
//The number of new messages is in the variable $nb_new_pm 
$nb_new_pm = $nb_new_pm['nb_new_pm']; 
//We display the links 
?> 
<a href="edit_infos.php">Edit my personnal informations</a><br /> 
<a href="list_pm.php">My personnal messages(<?php echo $nb_new_pm; ?> unread)</a><br /> 
<a href="connexion.php">Logout</a> 
<?php 
} 
else 
{ 
//Otherwise, we display a link to log in and to Sign up 
?> 
<a href="sign_up.php">Sign up</a><br /> 
<a href="connexion.php">Log in</a> 
<?php 
} 
?> 
+2

用戶名被分配給會話,所以你可以通過添加一個新的用戶/密碼到你的表中來使其成爲多用戶設置。以純文本保存您的密碼是個不錯的主意。 :-) – Oliver 2013-02-27 12:09:47

+0

使用MySQL而不是MySQLi或PDO也是一個壞主意 – 2013-02-27 12:11:51

+0

對不起,但你的問題是非特定的,你張貼太多的代碼,請閱讀如何在SO – michi 2013-02-27 12:12:09

回答

1

你要添加到您的會話管理一些新的指標,它會像下面 如果一個正常的用戶登錄在檢查他是否是管理員或您不存儲您正在使用的正常用戶會話索引。 $_SESSION['username'] etc .. 並且如果它是管理員登錄,則存儲類似於例如

$_SESSION['isAdmin']; 
$_SESSION['adminName']; 

等。 ,然後你在管理面板檢查管理會話.. 再視你的決定顯示和不顯示什麼,要求登錄,如果沒有會話變量'isAdmin'設置..

+0

感謝您的答案的常見問題。它非常有幫助。 :) – 2013-02-27 12:50:45

+0

不客氣:-) – ImadBakir 2013-02-27 13:07:34