以下兩種方式可以實現(xiàn)php直接記錄網(wǎng)頁受訪次數(shù),無需經(jīng)過數(shù)據(jù)庫儲存數(shù)據(jù)。

方法一:

<?php
session_start();//定義session,同一IP登錄不累加
$filepath = 'tongji.txt';
if ($_SESSION['temp'] == '')//判斷$_SESSION[temp]的值是否為空,其中的temp為自定義的變量
{
if (!file_exists($filepath))//檢查文件是否存在,不存在剛新建該文件并賦值為0
{
$fp = fopen($filepath,'w');
fwrite($fp,0);
  fclose($fp);
  counter($filepath);
}else
{
  counter($filepath);
}
$_SESSION['temp'] = 1;//登錄以后,給$_SESSION[temp]賦一個值1
}
echo '歡迎光臨,您是本站第<font color="#FF0000">'.file_get_contents($filepath).'</font>位訪客';
//counter()方法用來得到文件內(nèi)的數(shù)字
function counter($f_value)
{
//用w模式打開文件時會清空里面的內(nèi)容,所以先用r模式打開,取出文件內(nèi)容,保存到變量
$fp = fopen($f_value,'r') or die('打開文件時出錯。');
$countNum = fgets($fp,1024);
fclose($fp);
$countNum++;
$fpw = fopen($f_value,'w');
fwrite($fpw,$countNum);
fclose($fpw);
}
//注釋下面一行可以實現(xiàn)同一IP登錄不累加效果,測試時可以打開
session_destroy();
?>

 

方法二:將以下代碼儲存為“tongji.php”

<?php
$n=file_get_contents('tongji.txt');
$n++;
file_put_contents('tongji.txt',$n);
echo "document.write($n);";
?>

 

數(shù)據(jù)使用方式:

你是第<script type=text/javascript src="tongji.php"></script>位訪問者