각종 소스창고/php소스

방문자 카운드

아침한때비 2012. 3. 21. 18:29

// #######################################################################
// 프로그램명 : 카운터
// 현재파일정보 : 카운트프로그램의 전부
// 개발일자 : 1999.6
// 프로그래머 : 곽두섭
// 이-메일 : kts_boss@naver.com
// 프로그램소개 : 전체, 어제, 오늘 사이트방문자를 체크
// <iframe height="1" src="counter.php?mode=start" frameborder="0" width="1"></iframe>
// #######################################################################


// #############################################################################
//
// dbConnCounter() -> 데이타베이스 컨넥트 ( MySQL 연결함수 )
//
// #############################################################################

function dbConnCounter()
{
global $connect;

$host_name = "localhost"; // 호스트명(보통 localhost)
$user_name = dB명"; // 사용자
$user_password = "비밀번호"; // 비밀번호
$db_name = "dB명"; // DB명

$connect = mysql_connect( $host_name, $user_name, $user_password );
$db_choice = mysql_select_db( $db_name, $connect );

return $connect;
}

// #############################################################################
//
// dB_Conn_TBL() -> 테이블생성 ( table을 자동으로 생성시키는 함수 )
//
// #############################################################################

function dB_Conn_TBL( $db_tbl )
{
global $connect;

//-- 테일블설계

$click = "CREATE TABLE click (
uid int(11) DEFAULT '0' NOT NULL auto_increment,
hit varchar(20),
PRIMARY KEY (uid) )";

$tot = "CREATE TABLE tot (
uid int(11) DEFAULT '0' NOT NULL auto_increment,
tot int(10) DEFAULT '0',
PRIMARY KEY (uid) )";

$time = "CREATE TABLE time (
uid int(11) DEFAULT '0' NOT NULL auto_increment,
date varchar(20),
host varchar(20),
yy int(4) DEFAULT '0',
mm int(4) DEFAULT '0',
dd int(4) DEFAULT '0',
hh int(4) DEFAULT '0',
PRIMARY KEY (uid) )";

$now = "CREATE TABLE now (
uid int(11) DEFAULT '0' NOT NULL auto_increment,
date varchar(20),
host varchar(20),
PRIMARY KEY (uid) )";

$connect = dbConnCounter();

switch( $db_tbl )
{
case ("click") : mysql_query($click, $connect); break;
case ("tot") : mysql_query($tot, $connect); break;
case ("time") : mysql_query($time, $connect); break;
case ("now") : mysql_query($now, $connect); break;
}

return $connect;
}

// #############################################################################
//
// Counter_Start() -> 카운터를 체크함
//
// #############################################################################

function Counter_Start()
{
global $connect;

$signdate = date("Ymd"); // 날짜형식별로 정의
$signdate_now = date("YmdH");
$signdate_tot = date("YmdHim");
$signdate_format = date("Y-m-d-H:i:m");

$y1 = date("Y");
$m1 = date("m");
$d1 = date("d");
$h1 = date("H");

$host = getenv("REMOTE_ADDR");

$que_click = "SELECT * FROM click"; // 메인을 클릭한 총횟수
$result_click = mysql_query( $que_click, $connect );

if ( $result_click ) { $total_click = mysql_num_rows( $result_click ); }
if ( !$total_click )
{
$result_in_click = "INSERT INTO click VALUES( '', '1' )";
$result = mysql_query( $result_in_click, $connect );
}
else
{
mysql_data_seek( $result_click, 0 );
$row_click = mysql_fetch_array( $result_click );

$click_hit = $row_click[hit] + 1;

$ok = mysql_query( "UPDATE click SET hit = '$click_hit' WHERE uid = $row_click[uid]", $connect );
}

$que_host = "delete from now where ($signdate_tot - 30*10) > date"; // 접속한지 10분이 경과한 데이타는 삭제
$result_host = mysql_query( $que_host, $connect );

$que = "SELECT * FROM now where host='$host'"; // 현재접속자 DB에서 자신의 아이피를 읽음(있다면 추가하지 않음)
$result = mysql_query( $que, $connect );

if( $result ) { $total = mysql_num_rows( $result ); }
if (!$total)
{
$insert_result_insert = "INSERT INTO now VALUES( '', '$signdate_tot', '$host' )"; // 현재접속자에 자신의 아이피가 없으면 추가
$result = mysql_query( $insert_result_insert, $connect );

$que_tot = "SELECT * FROM tot order by uid asc"; // 전체수를 읽음
$result_tot = mysql_query($que_tot, $connect);

if( $result_tot ) { $total = mysql_num_rows($result_tot); }

if ( !$total )
{
$insert_result_insert = "INSERT INTO tot VALUES('', '1')"; // 최초라면 1로 설정
$result = mysql_query($insert_result_insert, $connect);
}
else
{
mysql_data_seek( $result_tot, 0 );
$row = mysql_fetch_array( $result_tot );

$tot = $row[tot] + 1; // 최초가 아니라면 1증가하여 재저장
$ok = mysql_query( "UPDATE tot SET tot = '$tot' WHERE uid = $row[uid]", $connect );
}

$que_time = "SELECT date FROM time where host='$host' order by uid desc"; // 접속자의 상세정보 별도로 저장
$result_time = mysql_query( $que_time, $connect );

if ( $result_time )
{
mysql_data_seek( $result_time, 0 );
$row = mysql_fetch_array( $result_time );
}

if ( $signdate != $row[date] ) // 오늘 날짜에 접속자의 아이피가 있다면 추가저장은 하지 않음
{
$insert_result_insert = "INSERT INTO time VALUES( '', '$signdate', '$host', '$y1', '$m1', '$d1', '$h1' )";
$result = mysql_query( $insert_result_insert, $connect );
}
}
}

// #############################################################################
//
// Click_Hit() -> 총 클릭된 수
//
// #############################################################################

function Click_Hit()
{
global $connect;

$signdate = date( "Ymd" );
$signdate_tot = date( "YmdHim" );
$hh = date( "H" );
$time_chk = $hh . " 시경";

$que = "SELECT * FROM click";
$result = mysql_query( $que, $connect );

if( $result )
{
mysql_data_seek( $result, 0 );
$hit = mysql_fetch_array( $result );
}

return $hit[hit];
}

// #############################################################################
//
// Total_Hit() -> 전체 접속자 수
//
// #############################################################################

function Total_Hit()
{
global $connect;

$signdate = date( "Ymd" );
$signdate_tot = date( "YmdHim" );
$hh = date( "H" );
$time_chk = $hh . " 시경";

$que = "SELECT * FROM tot";
$result = mysql_query( $que, $connect );

if( $result )
{
$total_cnt = mysql_num_rows( $result );

if( $total_cnt )
{
mysql_data_seek( $result, 0 );
$row = mysql_fetch_array( $result );
$total = $row[tot];
}
}

return $total;
}

// #############################################################################
//
// Today_Hit() -> 오늘 접속자 수
//
// #############################################################################

function Today_Hit()
{
global $connect;

$signdate = date( "Ymd" );
$signdate_tot = date( "YmdHim" );
$hh = date( "H" );
$time_chk = $hh . " 시경";

$que = "SELECT * FROM time where date='$signdate'";
$result = mysql_query( $que, $connect );

if( $result ) { $today = mysql_num_rows( $result ); }

return $today;
}

// #############################################################################
//
// Now_Hit() -> 현재 접속자 수
//
// #############################################################################

function Now_Hit()
{
global $connect;

$signdate = date( "Ymd" );
$signdate_tot = date( "YmdHim" );
$hh = date( "H" );
$time_chk = $hh . " 시경";

$que_host = "delete from now where ($signdate_tot - 10*10) > date"; // 접속한지 10분이 경과한 데이타는 삭제
$result_host = mysql_query( $que_host, $connect );

$que = "SELECT * FROM now";
$result = mysql_query( $que, $connect );

if( $result ) { $now = mysql_num_rows( $result ); }

return $now;
}

// #############################################################################
//
// Now_Time_Hit() -> 현재시간 접속자 수
//
// #############################################################################

function Now_Time_Hit()
{
global $connect;

$signdate = date( "Ymd" );
$signdate_tot = date( "YmdHim" );
$hh = date( "H" );
$time_chk = $hh . " 시경";

$que = "SELECT * FROM time where date='$signdate' and hh='$hh'";
$result = mysql_query( $que, $connect );

if( $result ) { $now_time = mysql_num_rows( $result ); }

return $now_time;
}

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
// 프로그램 시작 ( 모드값에 의한 분기 결정 )
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

$connect1 = dB_Conn_TBL( "click" );
$connect2 = dB_Conn_TBL( "tot" );
$connect3 = dB_Conn_TBL( "time" );
$connect4 = dB_Conn_TBL( "now" );

switch( $mode )
{
case ("start") : Counter_Start(); break;
}

?>


사용방법은

index.html 밑부분에 전에

<iframe height="1" src="counter.php?mode=start" frameborder="0" width="1"></iframe>

이부분을 삽입하시고...

관리자모드에서

<?

$click = Click_Hit();
$tot = Total_Hit();
$today = Today_Hit();
$now = Now_Hit();
$now_time = Now_Time_Hit();

echo("

총 클릭수 : $click
전체 접속자수 : $tot
오늘 접속자수 : $today
현재 접속자수 : $now

현재시간 접속자수 : $now_time
");

?>