2012-11-22 172 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