세션만을 이용한 현재 접속자. 로케이션 표시

· 18년 전 · 2616

간단한 사이트 작업일 경우
데이타 베이스를 사용하지 않고 현재 접속자를 출력하는 방법입니다.

아래의 예제는
현재 접속 경로와 리퍼러를 출력하는 예제입니다.
<?php

//현재 접속자 상세정보 가졍오기
//Yget_filelist_in_dir 함수에 의존성을 갖는다.
//Yread_file 함수에 의존성을 갖는다.
function Yin_user($dir_session){

  $time1 = 60 * 10;//활동중인 현재 접속자 기준 1분
  $time2 = 60 * 20;//현재접속자의 기준 5분

  if (empty($dir_session)) return Array();
  if (!is_dir($dir_session)) return Array();

  $dir_array = Yget_filelist_in_dir($dir_session);

  $return = Array();
  $i = 1;
  foreach($dir_array as $val){

    if (filemtime($val) < (time() - $time2)) continue;

    if (filemtime($val) > (time() - $time1)) {

      $text = Yread_file($val);

      $return[$i]['active'] = 'Active!!';

      preg_match("`s_location\|s:.+:\"(.+)\";`U", $text, $m);

      $m[1] = trim($m[1]);
      $return[$i]['location'] = (!empty($m[1])) ? $m[1] : '위치정보 없음';

      preg_match("`s_referer\|s:.+:\"(.+)\";`U", $text, $m);

      $m[1] = trim($m[1]);
      $return[$i]['referer'] = (!empty($m[1])) ? $m[1] : 'Direct!!';
    }
    else {

      $text = Yread_file($val);

      $return[$i]['active'] = 'Inactive!!';

      preg_match("`s_location\|s:.+:\"(.+)\";`U", $text, $m);

      $m[1] = trim($m[1]);
      $return[$i]['location'] = (!empty($m[1])) ? $m[1] : '위치정보 없음';

      preg_match("`s_referer\|s:.+:\"(.+)\";`U", $text, $m);

      $m[1] = trim($m[1]);
      $return[$i]['referer'] = (!empty($m[1])) ? $m[1] : 'Direct!!';
    }

    $i++;
  }

  return $return;
}

//넘어온 디렉토리의 모든 일차 파일리스트를 배열로 반환
function Yget_filelist_in_dir($dir_path){

  $dir_path = preg_replace("`/+$`", '', $dir_path);
  if(!is_dir($dir_path)) return Array();

  $return_file = Array();

  $d = dir($dir_path) or die('해당 디렉토리(' . $dir_path . ')를 열수 없습니다.');
  while (false !== ($entry = $d->read())) {

    $temp_file = $dir_path . '/' . $entry;
    if (is_file($temp_file) && !preg_match("`\.$`", $temp_file)) {

      $return_file[] = $temp_file;
    }
  }

  $d->close();

  return $return_file;
}

//로컬파일 읽어오기
function Yread_file ($read_file_path){

  if (empty($read_file_path) || !file_exists($read_file_path)) return false;

  if (is_readable($read_file_path) !== true) die('해당 파일(' . $read_file_path . ')은 읽을 권한이 없는 파일입니다.');
  $fr = fopen($read_file_path, "r") or die('해당 파일(' . $read_file_path . ')을 열수 없습니다.');

  if (empty($fr)) return false;

  $filesize = filesize($read_file_path);
  if (empty($filesize)) $filesize = 1024;
  $read_text = fread($fr, $filesize);
  fclose($fr);

  if (empty($read_text))  return false;

  return $read_text;
}

//각 페이지 접속시 세션 시작 이후 페이지 로케이션을 세션으로 저장한 경우 사용할수 있습니다.
//저같은 경우

if (empty($title)) $location = $_SERVER['PHP_SELF'];
else $location = $title;

$_SESSION['s_location'] = $location;

if (!empty($_SERVER['HTTP_REFERER']) && !preg_match("`^https?://(www\.)?chancein\.com`i", $_SERVER['HTTP_REFERER'])) $_SESSION['s_referer'] = $_SERVER['HTTP_REFERER'];

unset($location);

//현재 접속자 가져오기, 세션을 저장하는 디렉토리 경로를 절대경로 or 상대경로로서 인자로 넘김, $dir_data_session


//출력
$current = Yin_user($dir_data_session);

foreach($current as $k =>$v){

  echo '<b>' . $k . '.</b> [<b>' . $v['active'] . '</b>] ' . $v['location'] . '<br>&nbsp;&nbsp;&nbsp;&nbsp;[<b>referer</b>] ' . $v['referer'] . '<br><br>';
}

?>

예제 : http://chancein.com/svc/current.php

[이 게시물은 관리자님에 의해 2011-10-31 17:12:10 PHP & HTML에서 이동 됨]
|
댓글을 작성하시려면 로그인이 필요합니다. 로그인

프로그램

+
제목 글쓴이 날짜 조회
17년 전 조회 3,351
17년 전 조회 1,531
17년 전 조회 1,655
17년 전 조회 1,370
17년 전 조회 2,185
17년 전 조회 3,130
17년 전 조회 2,272
17년 전 조회 1,731
17년 전 조회 2,982
17년 전 조회 4,427
17년 전 조회 1,743
17년 전 조회 3,240
17년 전 조회 1,568
17년 전 조회 3,482
17년 전 조회 5,838
17년 전 조회 1,420
17년 전 조회 2,157
17년 전 조회 1,788
17년 전 조회 1,528
17년 전 조회 2,416
17년 전 조회 2,625
17년 전 조회 1,591
17년 전 조회 2,680
17년 전 조회 3,171
17년 전 조회 1,524
17년 전 조회 1,938
17년 전 조회 3,843
17년 전 조회 4,474
17년 전 조회 1,869
17년 전 조회 1,727
17년 전 조회 2,668
17년 전 조회 1,849
17년 전 조회 3,936
17년 전 조회 1,641
16년 전 조회 1,924
17년 전 조회 2,258
17년 전 조회 2,106
17년 전 조회 3,564
18년 전 조회 1,583
18년 전 조회 2,654
18년 전 조회 1,635
18년 전 조회 1,906
18년 전 조회 7,425
18년 전 조회 1,996
18년 전 조회 4,088
18년 전 조회 2,421
18년 전 조회 2,429
18년 전 조회 2,149
18년 전 조회 2,193
18년 전 조회 2,617
18년 전 조회 3,213
18년 전 조회 5,322
18년 전 조회 2,777
18년 전 조회 2,693
18년 전 조회 2,544
18년 전 조회 1,793
18년 전 조회 2,200
18년 전 조회 5,308
18년 전 조회 3,021
18년 전 조회 2,583
18년 전 조회 3,258
18년 전 조회 2,476
18년 전 조회 2,832
18년 전 조회 5,863
18년 전 조회 2,614
18년 전 조회 2,010
18년 전 조회 2,644
18년 전 조회 1,473
18년 전 조회 9,796
18년 전 조회 4,454
18년 전 조회 3,282
18년 전 조회 1,808
18년 전 조회 3,601
18년 전 조회 2,279
18년 전 조회 3,726
18년 전 조회 5,176
18년 전 조회 3,829
18년 전 조회 2,672
18년 전 조회 2,937
18년 전 조회 2,442
18년 전 조회 2,783
18년 전 조회 1,896
18년 전 조회 3,436
18년 전 조회 2,137
18년 전 조회 2,262
18년 전 조회 2,080
18년 전 조회 3,048
18년 전 조회 1,997
18년 전 조회 2,663
18년 전 조회 3,867
18년 전 조회 4,561
18년 전 조회 2,372
18년 전 조회 2,841
18년 전 조회 4,614
18년 전 조회 2,681
18년 전 조회 1,870
18년 전 조회 3,316
18년 전 조회 3,135
18년 전 조회 3,856
18년 전 조회 2,169
🐛 버그신고