2012-11-22 174 views
1

我使用本教程Tracking Tool dispaly沒有顯示,我得到了它的工作。我試圖修改它以適應我的需求,跟蹤Verizon Wirless(我的手機連接),以監視他們何時開始扼殺我的IP更改。我的哥哥有AT & T,所以我在我的數據庫中添加了一個主機名字段,這樣我們就可以區分我們的手機......但可以讓它顯示在IP旁邊的報告頁面中。當我點擊視圖來顯示我訪問過的頁面時,我可以在那裏顯示它,但不在主頁面上,繼承我的代碼,如果任何人能夠指出爲什麼它不顯示或我改變了錯誤PHP和MySQL查詢

只提注意到2貼被刪除......我「M不跟蹤任何人,但我自己,,,我已經在Verizon Wirless的一個植根霹靂,,,一次我打4演出的數據在一天(仍然有無限的計劃)verizon喜歡從一個IP啓動我,並將我切換到另一個更慢,我試圖指出哪些IP我注意到更好的帶寬,所以我可以循環收音機,直到它重新開始再好的一個

MySQL的

DROP TABLE IF EXISTS `testing_db`; 
CREATE TABLE IF NOT EXISTS `testing_db` (
    `entry_id` INT(11) NOT NULL AUTO_INCREMENT, 
    `visitor_id` INT(11) DEFAULT NULL, 
    `ip_address` VARCHAR(15) NOT NULL, 
    `hostname` VARCHAR(295) NOT NULL, 
    `server_name` text, 
    `useragent` text, 
    `page_name` text, 
    `query_string` text, 
    `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    PRIMARY KEY (`entry_id`), 
    KEY `visitor_id` (`visitor_id`,`timestamp`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

ip_tracker.php

<?php 
//define our "maximum idle period" to be 30 minutes 

$mins = 1; 

//set the time limit before a session expires 
ini_set ("session.gc_maxlifetime", $mins * 60); 

session_start(); 

$ip_address = $_SERVER["REMOTE_ADDR"]; 
$page_name = $_SERVER["SCRIPT_NAME"]; 
$query_string = $_SERVER["QUERY_STRING"]; 
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); 
$host_name = $hostname; //$_SERVER['HTTP_HOST']; 
$server_name = $_SERVER['SERVER_NAME']; 
$useragent=$_SERVER['HTTP_USER_AGENT']; 

$current_page = $page_name."?".$query_string; 

//connect to the database using your database settings 
include("db_connect.php"); 

if(isset($_SESSION["tracking"])){ 

    //update the visitor log in the database, based on the current visitor //id held in $_SESSION["visitor_id"] 
    $visitor_id = isset($_SESSION["visitor_id"])?$_SESSION["visitor_id"]:0; 

    if($_SESSION["current_page"] != $current_page){ 
     $sql = "INSERT INTO testing_db 
      (ip_address, page_name, query_string, visitor_id, hostname, host_name, server_name, useragent) 
      VALUES ('$ip_address', '$page_name', '$query_string', '$visitor_id','$hostname','$host_name','$server_name','$useragent')"; 
     if(!mysql_query($sql)){ 
      echo "Failed to update visitor log"; 
     } 
     $_SESSION["current_page"] = $current_page;   
    } 
    $_SESSION["tracking"] = false; 
}else{ 
    //set a session variable so we know that this visitor is being tracked 

    //insert a new row into the database for this person 
    $sql = "INSERT INTO testing_db 
      (ip_address, page_name, query_string, visitor_id, hostname, host_name, server_name, useragent) 
      VALUES ('$ip_address', '$page_name', '$query_string', '$visitor_id','$hostname','$host_name','$server_name','$useragent')"; 
    if(!mysql_query($sql)){ 
     echo "Failed to add new visitor into tracking log"; 
     $_SESSION["tracking"] = false; 
    } else { 
     //find the next available visitor_id for the database 
     //to assign to this person 
     $_SESSION["tracking"] = true; 
     $entry_id = mysql_insert_id(); 
     $lowest_sql = mysql_query("SELECT MAX(visitor_id) as next FROM testing_db"); 
     $lowest_row = mysql_fetch_array($lowest_sql); 
     $lowest = $lowest_row["next"]; 
     if(!isset($lowest)) 
      $lowest = 1; 
     else 
      $lowest++; 
     //update the visitor entry with the new visitor id 
     //Note, that we do it in this way to prevent a "race condition" 
     mysql_query("UPDATE testing_db SET visitor_id = '$lowest' WHERE entry_id = '$entry_id'"); 
     //place the current visitor_id into the session so we can use it on 
     //subsequent visits to track this person 
     $_SESSION["visitor_id"] = $lowest; 
     //save the current page to session so we don't track if someone just refreshes the page 
     $_SESSION["current_page"] = $current_page; 

ip_report.php

<?php 
include("db_connect.php"); 
//retrieve the appropriate visitor data 
$view = $_GET["view"]; 
//set a default value for $view 
if($view!="all" && $view!="record") 
    $view = "all"; 
if($view == "all") 
{ 
    //show all recent visitors 
    $sql = "SELECT visitor_id, GROUP_CONCAT(DISTINCT ip_address) as ip_address_list, 
COUNT(DISTINCT ip_address) as ip_total, COUNT(visitor_id) as page_count, 
MIN(timestamp) as start_time, MAX(timestamp) as end_time FROM testing_db GROUP BY visitor_id"; 
    $result = mysql_query($sql); 
    if($result==false){ 
     $view = "error"; 
     $error = "Could not retrieve values"; 
    } 
} else { 
    //show pages for a specific visitor 
    $visitor_id = $_GET['id']; 
    //rung $visitor_id through filter_var to check it's not an invalid 
    //value, or a hack attempt 
    if(!filter_var($visitor_id, FILTER_VALIDATE_INT, 0)){ 
     $error = "Invalid ID specified"; 
     $view = "error"; 
    } else { 
     $sql = "SELECT timestamp, page_name, query_string, ip_address, hostname, host_name, server_name, useragent FROM 
testing_db WHERE visitor_id = '$visitor_id'"; 
     $result = mysql_query($sql); 
    } 
} 
function display_date($time){ 
    return date("F j, Y, g:i a", $time); 
} 

?> 
<html> 
<head> 
<title>IP Tracker Report Page</title> 
<style> 
html {font-family:tahoma,verdana,arial,sans serif;} 
body {font-size:62.5%;} 
table tr th{ 
font-size:0.8em; 
background-color:#ddb; 
padding:0.2em 0.6em 0.2em 0.6em; 
} 
table tr td{ 
font-size:0.8em; 
background-color:#eec; 
margin:0.3em; 
padding:0.3em; 
} 
</style> 
</head> 
<body> 
<h1>IP Tracker Report</h1> 
<?php if($view=="all") { 
    //display all of the results grouped by visitor 
    if($row = mysql_fetch_array($result)){ 
    ?> 
<table> 
<tbody> 
<tr> 
<th>Id</th> 
<th>IP Address(es)</th> 
<th>Host Name</th> 
<th>Entry Time</th> 
<th>Duration</th> 
<th>Pages visited</th> 
<th>Actions</th> 
</tr> 
<?php 
     do{ 
     if($row["ip_total"] > 1) 
      $ip_list = "Multiple addresses"; 
     else 
      $ip_list = $row["ip_address_list"]; 
     $start_time = strtotime($row["start_time"]); 
     $end_time = strtotime($row["end_time"]); 
     $start = display_date($start_time); 
     $end = display_date($end_time); 
     $duration = $end_time - $start_time; 
     if($duration >= 60) { 
      $duration = number_format($duration/60, 1)." minutes"; 
     } 
     else { 
      $duration = $duration." seconds"; 
     } 
     $host - $row["hostname"]; 
     echo "<tr>"; 
     echo "<td>{$row["visitor_id"]}</td>"; 
     echo "<td>$ip_list</td>"; 
     echo "<td>$host</td>"; 
     echo "<td>$start</td>"; 
     echo "<td>$duration</td>"; 
     echo "<td>{$row["page_count"]}</td>"; 
     echo "<td><a href='ip_report.php?view=record&id={$row["visitor_id"]}'>view</a></td>"; 
     echo "</tr>"; 
     } while ($row = mysql_fetch_array($result)); 
    ?> 

</tbody> 
</table> 
<?php } else { ?> 
<h3>No records in the table yet</h3> 
<?php } ?> 
<?php } elseif($view=="record"){ ?> 
<h3>Showing records for Visitor <?php echo $visitor_id; ?></h3> 
<p><a href="ip_report.php">back</a></p> 
<?php 
    //show all pages for a single visitor 
    if($row = mysql_fetch_array($result)){ 
    ?> 
<table> 
<tbody> 
<tr> 
<th>Page viewed</th> 
<th>User Agent</th> 
<th>Time of view</th> 
</tr> 
<?php 
     do{ 
     if($row["ip_total"] > 1) 
      $ip_list = "More than 1"; 
     else 
      $ip_list = $row["ip_address_list"]; 
     $time = display_date(strtotime($row["timestamp"])); 
     echo "<tr>"; 
     echo "<td>{$row["page_name"]}</td>"; 
     echo "<td>{$row["hostname"]}</td>"; 
     echo "<td>$time</td>"; 
     echo "</tr>"; 
     } while ($row = mysql_fetch_array($result)); 
    ?> 
</tbody> 
</table> 
<?php } else { ?> 
<h3>No records for this visitor</h3> 
<?php 
    } 
} elseif($view=="error") { ?> 
<h3>There was an error</h3> 
<?php echo $error; 
} 
?> 

</body> 
</html> 
+2

編程是當你明白你在做什麼,而不是隻取代碼的文章,使一些隨機變化,並期望它的工作。 – zerkms

+0

好吧,除了當我嘗試在不同的地方顯示數據,,,所以文章確實工作,我只是不理解我可能做錯了什麼。而且由於這是不是編程是什麼所謂的,所以我可以張貼我的問題在正確的位置 – acrichm

+0

「我只是不理解什麼是我可以做的錯誤」 ---那是因爲學習應該從基礎開始。尤其是什麼調試以及如何調試代碼。 – zerkms

回答

2

最後一行添加到您的報表腳本:

do{ 
    if($row["ip_total"] > 1) 
     $ip_list = "Multiple addresses"; 
    else 
     $ip_list = $row["ip_address_list"]; 

     // Add the following line here 
     $host = $row["hostname"]; 
+1

另外,他需要從他的查詢中刪除不存在的'host_name'列並僅使用'hostname'。 –

+0

k我從數據庫和腳本中刪除了host_name並移動了變量..仍然沒有顯示,,,可能是因爲我在數據庫中使用VARCHAR()作爲主機導致它不顯示? – acrichm

+0

感謝TheSmose和Botond,兩個簡單的修復移動$主機和去除host_name和不得不主機名添加到第一個查詢的報告頁面上 – acrichm