<?php
header("Content-type: image/png");
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "pragma: no-cache" );
header( "Cache-control: no-cache" );
# ***************************
# DB 情報
# ***************************
$server = 'localhost';
$dbname = 'lightbox';
$user = 'root';
$password = 'password';
# **************************************
# 埋め込みを許可したページ以外はエラー
# **************************************
$trust = false;
$file = file( "trust.lst" ); // 許可対象 URL の先頭文字列リスト
for( $i = 0; $i < count( $file ); $i++ ) {
# 改行削除
$target = rtrim( $file[$i] );
# 空行は無視
if ( $target == "" ) {
continue;
}
# コメント文字
if ( substr( $target, 0, 1 ) == ";" ) {
continue;
}
$len = strlen( $target );
$ref = substr( $_SERVER['HTTP_REFERER'], 0, $len );
if ( $target == $ref ) {
$trust = true;
break;
}
}
# ***************************
# 使用不可
# ***************************
if ( !$trust ) {
// 使用権限なし
$img_text = "8888888888";
}
else {
# ***************************
# id が指定されていない場合
# ***************************
if ( $_GET['id'] == '' ) {
$_GET['id'] = "default";
}
// 接続
$connect = @mysql_connect( $server, $user, $password );
# ***************************
# 処理
# ***************************
if ( $connect ) {
mysql_select_db( $dbname, $connect );
$_GET['id'] = mysql_real_escape_string( $_GET['id'], $connect );
# ***************************
# SQL文ヒアドキュメント
# ***************************
$query_update = <<<QUERY_UPDATE
update page_counter
set access_count = access_count + 1
where page_id = '{$_GET['id']}'
QUERY_UPDATE;
$query_insert = <<<QUERY_INSERT
insert into page_counter
values('{$_GET['id']}',0)
QUERY_INSERT;
$query_create = <<<QUERY_CREATE
create table page_counter (
page_id VARCHAR(20)
,access_count INT
,primary key(page_id)
)
QUERY_CREATE;
$query_select = <<<QUERY_SELECT
select * from page_counter
where page_id = '{$_GET['id']}'
QUERY_SELECT;
// 更新処理( +1 )
$result = mysql_query($query_update,$connect);
if ( !$result ) {
$result = mysql_query($query_create,$connect);
// 新規行追加
$result = mysql_query($query_insert,$connect);
$result = mysql_query($query_update,$connect);
}
else {
if ( mysql_affected_rows($connect) == 0 ) {
$result = mysql_query($query_insert,$connect);
$result = mysql_query($query_update,$connect);
}
}
// 更新結果読み出し
$result = mysql_query($query_select,$connect);
$column = mysql_fetch_row($result);
$img_text = sprintf( "%010d", $column[1]+0 );
}
else {
// 接続エラー
$img_text = "0000000000";
}
}
$font_path = "./7barPBd.TTF";
# ***************************
# キャンバス作成
# ***************************
$im = imagecreate( 150, 30 );
# ***************************
# 画像の背景色
# imagecolorallocate() の最初のコールで背景色がセットされます
# ***************************
$white = imagecolorallocate( $im, 255, 255, 255 );
# ***************************
# 画像の文字色
# ***************************
$black = imagecolorallocate( $im, 0, 0, 0 );
imagettftext(
$im,
20, # サイズ
0, # 角度
5, # x 座標
25, # y 座標
$black,
$font_path,
$img_text);
# ***************************
# PNG 出力
# ***************************
imagepng($im);
# ***************************
# 終了処理
# ***************************
imagecolordeallocate( $im, $black );
imagecolordeallocate( $im, $white );
imagedestroy($im);
?>