2012-01-16 15 views
1

私はAJAXをPHPと共に使用して、訪問者がページ上で費やした時間を、PHPスクリプトが他の詳細とともにテキストファイルに書き込むサーバーに伝えます。 AJAXスクリプトは、PHPスクリプトを呼び出すことができませんので、訪問者の詳細は。これは、AJAXスクリプトAjaxおよび/またはPHPエラー

PHPスクリプトがあまりにも下に与えられたある

<script type="text/javascript"> 
    var startTime = new Date();  //Start the clock! 
    window.onbeforeunload = function()  //When the user leaves the page(closes the window/tab, clicks a link)... 
    { 
     var endTime = new Date();  //Get the current time. 
     var timeSpent = (endTime - startTime);  //Find out how long it's been. 
     var xmlhttp;  //Make a variable for a new ajax request. 
     if (window.XMLHttpRequest)  //If it's a decent browser... 
     { 
      // code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp = new XMLHttpRequest();  //Open a new ajax request. 
     } 
     else  //If it's a bad browser... 
     { 
      // code for IE6, IE5 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  //Open a different type of ajax call. 
     } 

     xmlhttp.open("GET","ajaxphp.php?time="+timeSpent,false);  //The false at the end tells ajax to use a synchronous call which wont be severed by the user leaving. 
     xmlhttp.send();  //Send the request and don't wait for a response. 
    } 
</script> 

をログに記録されていません。

<?php 
$time=$_GET["time"]; 

$countfile = "counter.txt"; 

// location of site statistics. 
$statsfile = "stats.txt"; 

// checks whether the file exist, if not then server will create it. 
if (file_exists($countfile)) { 

// open the counter hit file. 
$fp = fopen($countfile, "r"); 

// reads the counter hit file and gets the size of the file. 
$output = fread($fp, filesize($countfile)); 

// close the counter hit file. 
fclose($fp); 

// get the integer value of the variable. 
$count = intval($output); 
} 

// if file is doesn't exist, the server will create the counter hit file and gives a value of zero. 
else { 
$count = 0; 
} 

// showcount function starts here. 
function ShowCount() { 

// declares the global variables. 
global $ShowCount, $countfile, $statsfile, $count,$time; 

// get the current month. 
$month = date('m'); 

// get the current day. 
$day = date('d'); 

// get the current year. 
$year = date('Y'); 

// get the current hour. 
$hour = date('G'); 

// get the current minute. 
$minute = date('i'); 

// get the current second. 
$second = date('s'); 

// this is the date used in the stats file 
$date = "$month/$day/$year $hour:$minute:$second"; 

// this is the remote IP address of the user. 
$remoteip = getenv("REMOTE_ADDR"); 

// some of the browser details of the user. 
$otherinfo = getenv("HTTP_USER_AGENT"); 

// retrieve the last URL where the user visited before visiting the current file. 
$ref = getenv("HTTP_REFERER"); 

// open the statistics file. 
$fp = fopen($statsfile, "a"); 

// put the given data into the statistics file. 
fputs($fp, "Remote Address: $remoteip | "); 
fputs($fp, "Information: $otherinfo | "); 
fputs($fp, "Date: $date | "); 
fputs($fp, "Referer: $ref\n"); 
fputs($fp, "Time Spent: $time | ") 

// close the statistics file. 
fclose($fp); 

// adds 1 count to the counter hit file. 
$count++; 

// open the counter hit file. 
$fp = fopen($countfile, "w"); 

// write at the counter hit file. 
// if the value is 34, it will be changed to 35. 
fwrite($fp, $count); 

// close the counter hit file. 
fclose($fp); 

// showcount variable is equal to count variable. 
$ShowCount = $count; 

// return the value of the count variable into showcount variable. 
return $ShowCount; 
} 

// display the value in the counter hits file. 
echo showcount(), " visits"; 

?> 
+0

ウィンドウイベントではなく、ajaxを手動で実行しようとしましたか? –

+0

はい、これもやっていますが、まだ動作していません。 – Shooter

+1

正確には何ですか* AJAXスクリプトはPHPスクリプト*を呼び出すことができませんか? URLは見つかりませんか?アクセスが拒否されました? JavaScriptは失敗しますか?そして、あなたがAJAXをたくさん使っているなら、確かにjQuery(http://api.jquery.com/jQuery.get/)を見てください。 – Quasdunk

答えて

1

以下を試してください:

  • ウェブブラウザでurl ajaxphp.php?time = 123456に直接アクセスしてくださいペン
  • 使用AJAX呼び出しを活性化し、AJAXのための代わりに
  • 使用jqueryのを何が起こるかに注意し、これらのいずれかがより多くのあなたを与えるだろう、私は確信している

を何が起こるかに注意ボタンのonclickハンドラあなたの現在のセットアップが失敗する理由の手がかり。

関連する問題