【115日目】ページごとのアクセス解析をするPHPを作成したい-5
Posted: Last Update:
ページごとのアクセスをカウントするPHPを書き始めて4日目です。昨日は集計ファイルを書き換えるPHPを途中まで作成しました。今日はできればPHPを完成させたいです!
昨日までの作業振り返り
昨日までで、一度ファイルの中身を変数に格納して、preg_replaceで書き換え、ファイル全体を上書きする、というPHPの途中まで書きました。
<?php
$value = "$myurl/$urlfolder/$filenum";
$str = file_get_contents("$mypath/top/access.txt");
$findvalue = $value.'[.*?]';
preg_match_all($findvalue, $str, $matches, PREG_SET_ORDER);
if ($matches > 0){
$replace = preg_replace('[.*?]', '[$i++]', $matches[0]);
$str = preg_replace($findvalue, $replace[0], $str, 1);
$fp = fopen("$mypath/top/access.txt", "w");
fwrite($fp, $str);
fclose($fp);
}
else{
$fp = fopen("$mypath/top/access.txt", "c+");
fwrite($fp, "\n".$value." [1]");
fclose($fp);
}
?>
$value = "$myurl/$urlfolder/$filenum";
$str = file_get_contents("$mypath/top/access.txt");
$findvalue = $value.'[.*?]';
preg_match_all($findvalue, $str, $matches, PREG_SET_ORDER);
if ($matches > 0){
$replace = preg_replace('[.*?]', '[$i++]', $matches[0]);
$str = preg_replace($findvalue, $replace[0], $str, 1);
$fp = fopen("$mypath/top/access.txt", "w");
fwrite($fp, $str);
fclose($fp);
}
else{
$fp = fopen("$mypath/top/access.txt", "c+");
fwrite($fp, "\n".$value." [1]");
fclose($fp);
}
?>
今現在、「Delimiter must not be alphanumeric or backslash」というエラーが出ています…。
エラー回避方法を考える
自分が書いたPHPとネット上に堕ちているサンプルのPHPを見比べてみました。どうも「$findvalue」に入れている文字列がいけないみたいです。書き換えてみました。
$value = "$myurl/$urlfolder/$filenum";
$str = file_get_contents("$mypath/top/access.txt");
$findvalue = "/.".$value."[.*?]/";
preg_match_all($findvalue, $str, $matches, PREG_SET_ORDER);
if ($matches > 0){
$replace = preg_replace('[.*?]', '['.$1++.']', $matches[0]);
$str = preg_replace($findvalue, $replace[0], $str, 1);
$fp = fopen("$mypath/top/access.txt", "w");
fwrite($fp, $str);
fclose($fp);
}
else{
$fp = fopen("$mypath/top/access.txt", "c+");
fwrite($fp, "\n".$value." [1]");
fclose($fp);
}
「Delimiter must not be alphanumeric or backslash」というエラーは出なくなりました。しかし、今度は「syntax error, unexpected '1'」というエラーが7行目に…。このやり方ではできないのか…。
今日はここまで
数字を足していくPHPについて検索している間に時間が来てしまいました。方向性があっているかどうかもわからないままですが、今日はここまで!
明日もまた、preg_replaceについて調べてみたいと思います!
おまけのロードマップ
サイト作成がどのくらい進んでいるかロードマップで進捗を確認すると、ここまで進んでいます。
- 準備 - サイトの全体像を決める
- 使用するプログラミング言語を決めて環境を整える
- サイトの基本構造を作って公開する
- 収益化に最適なサイトにアップグレードする
- GoogleアドセンスとAmazonアソシエイトの申し込みをする ←完了!
- ローカルで簡単に入力ができるようにフォームを作る
- フォローアップ - より収益化に向いたサイト構造に変更する
