2011-03-06 6 views
1

사용자 테이블 아래에있는 게임 캐릭터의 속성을 표시하고 싶습니다. 따라서 로그인 한 사용자의 특정 속성을 표시하려면 해당 행에 있어야합니다. 내가하지 않았기 때문에 사용자를 세션에 등록해야합니까? 세션? 사용자 행을 표시하려면 어떻게해야합니까?

내가 로그인

<? 
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를 가져옵니다

  • 사용 세션 (세션에 사용자 ID를 저장하고 where id = {$id}
  • 같은 것을 사용하여 쿼리에이 추가 :. 당신은 당신이 시도 할 수있는 몇 가지 옵션이 있습니다 사용자가 로그인했는지 확인하는 동일한 코드가 사용자 ID를 반환 할 수 있습니다.

현재 코드는 로그인하는 방법을 보여 주며이 코드는 사용자가 사용했던 코드에서 세션을 사용할 수 있어야합니다. 예전과 마찬가지로

이를 확인하고 다른 코드를 이해해야합니다. 게시 한 코드를 이해하지 못하는 것처럼 느껴지므로 모든 것을 보여주기가 어렵지만 이렇게해야합니다.

<?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

그래도 여전히 분실되었지만 – Kelvin

+0

나는 질문을했습니다 ** 어떻게 시스템에 로그인합니까 **. 기본 정보를 제공하지 않으면 어떻게 문제를 해결할 수 있습니까? 내가 말한 것처럼, 어떤 사용자로 로그인했는지 찾아야합니다. 알고 있다면 쿼리에서 사용할 수 있습니다. 이 페이지에서 해당 사용자 ID를 복구 할 수 없다면 가능한지 확인해야합니다. 그렇다면 세션을 사용 중일 수 있습니다. – Nanne

+0

오케이> 다시 연락 드리겠습니다. – Kelvin

관련 문제