2017-02-20 1 views
0

PHP 세션 핸들러를 사용하여 내 사이트에 영구 세션을 구현하고 있습니다. 문제는 어떤 시점에서 user_key를 다른 MySQL 테이블에 삽입해야하며 코드에서 해당 정보를 검색하는 방법을 모르겠습니다.PHP로 영구 세션 처리기에서 특정 정보를 검색하려면 어떻게해야합니까?

active|i:1487613760;user_username|s:20:"[email protected]";user_key|s:8:"a5186adc";authenticated|b:1;user_name|s:12:"victor";user_email|s:20:"[email protected]";remember|b:1; 

와 나는 user_key 변수를 얻을 수있는 간단한 방법이 있는지 알고 싶습니다 :

예를 들어, 내 세션 테이블에 데이터 행입니다.

조금 혼란스러운 경우 죄송합니다.

+0

, 어떻게 세션 핸들러를 직렬화입니까? – DevDonkey

답변

0

첫 번째 옵션은이 문자열을 직렬화 해제하는 것입니다. http://php.net/manual/en/function.unserialize.php

두 번째 옵션, 당신은 다음 패턴으로는 preg_match 기능을 사용할 수 있습니다 :

preg_match('/user_key\|s:\d+:"([a-zA-Z0-9]+)"/', $string, $match); 
+0

도움을 주셔서 감사합니다 4EACH. 문제는이 특정 행을 얻는 방법을 모르는 것입니다. 한 번에 많은 세션이 동시에 발생합니다. –

+0

사용자에게 고유성 정보가 있습니까? – 4EACH

+0

다른 mysql 테이블에 삽입 할 수 있도록 user_key가 필요합니다. 처리기로 정보를 읽는 "read"방법이 있습니다 ... –

0

난 아무데도 직렬화 된 문자열, 내가 전에 본 적이 아니고 무엇인가의 형식을 처리 할 수있는 뭔가를 찾을 수 없습니다.

그러나 heres는 빠른 함수는 (이 지나치게 우아하지 않을 수도 있습니다,하지만 난 단지 1 커피 했어) 배열로 켜십시오 :

$string = 'active|i:1487613760;user_username|s:20:"[email protected]";user_key|s:8:"a5186adc";authenticated|b:1;user_name|s:12:"victor";user_email|s:20:"[email protected]";remember|b:1; 
'; 

$array = deserializeSessionString($string); 

echo $array['user_key']; 

// deserialize a session string into an array 
function deserializeSessionString($string) 
{ 
    $output = []; 
    // separate the key-value pairs and iterate 
    foreach(explode(';', $string) as $p) { 
     // separate the identifier with the contents 
     $bits = explode('|', $p); 

     // conditionally store in the correct format. 
     if(isset($bits[1])) { 
      $test = explode(':', $bits[1]); 
      switch($test[0]) { 
       // int 
       case 'i': 
        $output[$bits[0]] = $test[1]; 
        break; 
       case 's': 

        // string 
        // ignore test[1], we dont care about it 
        $output[$bits[0]] = $test[2]; 
        break; 

       case 'b': 
        // boolean 
        $output[$bits[0]] = ($test[1] == 1 ? true : false); 
        break; 
      } 
     } 

    } 

    return $output; 
} 

그때 당신은 무엇을 액세스 할 수 있어야합니다을 단지 키가 필요합니다

이상한 직렬화 형식을 보인다
echo $array['user_key']; 

heres an example

+0

답장을 보내 주셔서 감사합니다 DevMonkey ... 세션 ID 나 특정 행에 대한 "색인"을 얻을 수 있다면 도움이 될 수 있습니다. 세션 처리기가 코드 자체에서 도움이 될지 모르지만 그 사이에 _SESSION 변수의 모든 페이지에서 user_key를 설정하고 가져 오는 중입니다 ... 다시 한 번 감사드립니다. –

관련 문제