2011-03-06 50 views
1

我想顯示遊戲角色的屬性,這是用戶TABLE下的屬性。所以,我希望它顯示已登錄的用戶的特定屬性,因爲它應該在他的行中。我是否需要在會話中註冊用戶,因爲我沒有。會話?我如何顯示用戶行?

這是我用來獲取登錄時在

<? 
if(isset($_POST['Login'])) { 

    if (ereg('[^A-Za-z0-9]', $_POST['name'])) {// before we fetch anything from the database we want to see if the user name is in the correct format. 
     echo "Invalid Username."; 
     }else{ 

      $query = "SELECT password,id,login_ip FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'"; 
$result = mysql_query($query) or die(mysql_error()); 
$row = mysql_fetch_array($result); // Search the database and get the password, id, and login ip that belongs to the name in the username field. 

if(empty($row['id'])){ 
    // check if the id exist and it isn't blank. 
    echo "Account doesn't exist."; 
    }else{ 

     if(md5($_POST['password']) != $row['password']){ 
      // if the account does exist this is matching the password with the password typed in the password field. notice to read the md5 hash we need to use the md5 function. 
      echo "Your password is incorrect."; 
      }else{ 

       if(empty($row['login_ip'])){ // checks to see if the login ip has an ip already 
     $row['login_ip'] = $_SERVER['REMOTE_ADDR']; 
     }else{ 

     $ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it 

     if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) { 
     $row['login_ip'] = $row['login_ip']; 
     }else{ 
     $row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR']; 
     } 
     } 

    $_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user. 

$result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'") 
or die(mysql_error()); 

// to test that the session saves well we are using the sessions id update the database with the ip information we have received. 

header("Location: play.php"); // this header redirects me to the Sample.php i made earlier 


      } 
      } 
    } 
} 
?> 

回答

1

你需要找到您登錄作爲用戶在用戶會話的代碼。你如何登錄到你的系統?您有幾種選擇,你可以嘗試一下:

  • 使用會話(保存用戶ID的會話,並使用類似where id = {$id}
  • 添加到查詢從您的登錄密碼讓你的用戶ID所以檢查某用戶登錄相同的代碼,可以返回一個用戶ID。

您當前的代碼演示瞭如何登錄,而這個工作嗎?那你應該能夠在你有代碼使用會話up before。

就像一個例子,你需要檢查這一點,並理解其他代碼。這感覺有點像你沒有真正理解你發佈的代碼,所以很難顯示一切,但它應該是這樣的。

<?php 
session_start(); 
$id = $_SESSION['user_id']; 
//you need to do some checking of this ID! sanitize here! 
$result = mysql_query("SELECT * FROM users" where id = {$id}) or die(mysql_error()); 
// keeps getting the next row until there are no more to get 
    while($row = mysql_fetch_array($result)) { 
} 
+0

okay..but還是輸了,雖然 – Kelvin 2011-03-06 17:12:19

+0

我也問一個問題有**你如何登錄到你的系統**。如果您不提供基本信息,我們如何幫助您解答問題?就像我說的,你需要找到你登錄的用戶。如果你知道這一點,你可以在你的查詢中使用它。如果您無法在此頁面上恢復該用戶標識,那麼您需要確保您可以。是的,那麼你可能會使用會話。 – Nanne 2011-03-06 17:21:15

+0

好的>我會聯繫你的 – Kelvin 2011-03-06 20:31:25