2012-08-27 2 views
2

로그인 정보를 기억하기 위해 쿠키를 사용하려했지만 그 쿠키는 결코 "취하지"않는 것 같습니다. 여기에 초기 로그인 스크립트는 다음과 같습니다쿠키가 작동하지 않습니다

//try to login using cookies, but first make sure it doesn't have a $_SESSION variable for it 
if (!(isset($_SESSION['valid_user']))) { 
    if (isset($_COOKIE["login"])) { 
     //try to login using cookie 
     $query = "select from cookie_users where uniqueid = '".$_COOKIE["login"]."')"; 
     $conn = new mysqli('localhost', 'user', 'password', 'test'); 
     $result = $conn->query($query); 
     if ($result->num_rows>0) { 
      $result->fetch_assoc(); 
      $result['username'] = $username; 
      $result['password'] = $password; 
      //try to login using password and username from cookie database 
      $query = "select * from authorized_users where username = '".$username."' and password = '".$password."'"; 
      $result2 = $conn->query($query); 
      if ($result->num_rows>0) { 
       $_SESSION['valid_user'] = $username; 
      } 
     } 
    } 
} 

if (isset($_POST['userid']) && isset($_POST['password'])) { 
    // if the user has just tried to log in 
    $userid = $_POST['userid']; 
    $password = $_POST['password']; 

    $db_conn=new mysqli('localhost', 'user', 'password', 'test'); 

    if (mysqli_connect_errno()) { 
     echo 'Connection to database failed: '.mysqli_connect_errno(); 
     exit; 
    } 
    $query = 'select * from authorized_users '."where name='$userid' "."and password=sha1('$password')"; 

    $result = $db_conn->query($query); 
    if ($result->num_rows > 0) { 
     //if they are in the database, register the user id 
     $_SESSION['valid_user'] = $userid; 
     //set up cookie 
     setcookie("login", $uniqueid, time()*60*60*24*14); 
     $query = "insert into cookie_users values ('".$userid."', sha1('".$password."'), '".$uniqueid."')"; 
     $result = $db_conn->query($query); 
     if (!$result) { 
      echo 'Could not update cookie in database'; 
     } 
    } 
    $db_conn->close(); 
} 

회원 전용 콘텐츠 :

if (isset($_SESSION['valid_user'] { 
echo "members only content goes here"; 
} else { 
echo "you need to login"; } 

세션 및 로그인 스크립트가 잘 작동합니다. 로그인 페이지를 사용하여 직접 로그인하면 표시됩니다. 다른 페이지로 이동 한 다음 다시 돌아 오면 여전히 작동합니다. 그래서 세션이 잘 작동한다고 나는 생각합니다. 그러나 브라우저를 닫고 다시 돌아 오면 등록되지 않습니다. 스크립트가 엉망이되면 알려주세요. 어느 쪽이든, 나는 테스트 스크립트를 작성하고 그 작업조차도하지 않습니다. 여기에 있습니다 :

<?php 

setcookie("test", "the test is good", time()*60*60); 

?> 

<html> 
<body> 
<?php 
echo $_COOKIE["test"]; 
?> 
</body> 
</html> 

그래서 저는 근본적인 것이 빠져 있다고 생각합니다. 죄송합니다 대답이 고통 스럽다면 쿠키가 새로운 것입니다. 당신이 줄 수있는 도움을 주셔서 감사합니다.

+1

을하지만 당신은 더 설명 제목 혜택을 누릴 수 있습니다 .. :) – Coffee

+1

당신이 당신의 브라우저가 모든 쿠키를 가까이에서 삭제 된 일부 개인 정보 보호 모드로 설정 함 : 다행히, 당신은 매우 간단하게 문제를 해결할 수 있습니까? – Fluffeh

+1

또한'time() * 60 * 60'이 넘칠 수 있습니다. 대신'time() + (60 * 60)'을 사용하십시오. –

답변

3

귀하의 문제는이 라인에 : 여기

setcookie("login", $uniqueid, time()*60*60*24*14); 

, 당신은 시간을 곱하는 대신에 그것을 추가하는. 이 (가)MAX_INT크기 인 (Y2038 참조)을 초과 할 정도로 충분히 배가되었습니다. 어떤 이유로 든 아파치 (적어도 내 시스템에서)는 해당 헤더를 무시하고 브라우저는 세션 길이 동안 만 유지합니다.

setcookie("login", $uniqueid, time() + 60*60*24*14); // Adding two weeks, not multipying by >9000 
+0

감사합니다 arxanas. 내가 고칠거야. –

관련 문제