2009-02-07 4 views
3

어떻게하면이 스크립트에 자동 로그인을 "빌드 인"할 수 있습니까?쿠키 PHP

 
if (isset($_POST['login'])) { 
$query = mysql_query(" 
      SELECT * FROM users 
      WHERE user_name = '".mysql_real_escape_string($_POST['username'])."' 
     AND user_password = '".mysql_real_escape_string($_POST['password'])."' 
"); 

/* wrong login information? terminate the script */ 
if (!mysql_num_rows($query)){ 
header("Location: ./"); 
exit(); 
} 

/* set session with unique index */ 
$_SESSION['id'] = mysql_result($query, 0, 'user_id'); 
mysql_query("UPDATE users SET user_online = '1' WHERE user_id = '{$_SESSION['id']}'"); 
header("Location: ./"); 
exit; 
} 
+0

정확히 무엇이 문제입니까? –

답변

-2
//use request so you can link to the page and log the user in. it would be a good idead to use md5() on $_REQUEST['username'] and $_REQUEST['password'] so the password and usernames arent in plain text. see http://dev.mysql.com/doc/refman/5.1/en/encryption-functions.html#function_md5 

if (isset($_REQUEST['login'])) { 
$query = mysql_query(" 
      SELECT * FROM users 
      WHERE user_name = '".md5($_REQUEST['username'])."' 
     AND user_password = '".md5($_REQUEST['password'])."' 
"); 
/* wrong login information? terminate the script */ 
// there should only be one row returned with the query 
if (mysql_num_rows($query)!=1){ 
header("Location: ./"); 
exit(); 
} 

/* set session with unique index */ 
$_SESSION['id'] = mysql_result($query, 0, 'user_id'); 
mysql_query("UPDATE users SET user_online = '1' WHERE user_id = '{$_SESSION['id']}'"); 
header("Location: ./"); 
exit; 
} 


//now you can link to the page 
<a href="login.php?login=yes&username=**insert md5 hash of user name here**&password=md5 hash of password">auto login</a> 
+0

GET을 통해 누군가를 로그인하는 것을 원하지 않습니다. 그것은 보안 결함입니다. – Sasha

4

첫째, 몇 가지 제안 :

  1. 당신은하지 일반 텍스트로, salted hashes로 암호를 저장해야합니다.
  2. 일반적으로 인증 방법을 변경하고자 할 수 있습니다. 암호를 선택 ("선택 *"하지 말고)하여 사용자가 입력 한 암호의 해시 된 해시와 비교하는 것이 좋습니다.

사용자를 로그인 상태로 유지하는 방법을 올바르게 이해하고 있습니다. 기본적인 아이디어는 사용자를 고유하게 식별하는 쿠키를 저장해야한다는 것입니다 (그러나 쉽게 도용 될 수있는 것은 아닌지 확인하십시오. 하는 SHA1 해시 또는 뭔가처럼.)에 로그인 한 사용자를 유지하기 위해 쿠키에 멀리 만료 날짜를 설정합니다.

Here is the function you use to set cookies in PHP.

을 그런 다음 페이지를로드 할 때, 쿠키가 있는지 확인할 수 있습니다. 쿠키가 존재하고 사용자에게 SESSION 변수가 없으면 사용자에게 할당 할 수 있습니다.