Jump to content

Statistics


Dawlish

Recommended Posts

New comer so do apologise if this is posted in the wrong place.

 

I'm working on a project that gathers the clients info (IP, Browser, Referral link and date+time of visit) which is then displayed in the browser in a html table,

I coded it so it wrote it to a txt file to check whether anything was happening (which it was) but i am now stuck on displaying it in a table.

 

Any help is appreciated!

Code below

 

 

<?php
 
$output = 'Log.txt'; /*This is the file where the data will be written to*/
 
@ $data = json_decode(file_get_contents("http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/json")); /*Gather the users IP and refer it over to the website which will return a json object which will be decoded and stored in $data*/
 
$filehandle = fopen($output, "a"); /*This opens the file declared in $output in append mode*/
if ($filehandle)
{
$string ='"'.$QUERY_STRING.'","'
.$_SERVER['REMOTE_ADDR'].'","' /*This writes the IP to the file*/
.$_SERVER['HTTP_USER_AGENT'].'","' /*This writes the users browser and OS to the file*/
.$_SERVER['HTTP_REFERER'].'","' /*This writes the the link that directed the user to this site to the file*/
.date("r").'"' /*This writes the date and time to the file*/
."\n"
;
$write = fputs($filehandle, $string);
fclose($filehandle);
}
?>
 
<html>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
border-spacing: 5px;
}
</style>
<h1>Statistics</h1>
<table width = "550px" height = "300px" border="1">
<tr bgcolour="#000000">
<th>IP address</th>
<th>Browser</th>
<th>Referrel URL</th>
<th>Date and Time</th>
</tr>
<tr>
<td>echo "$_SERVER['REMOTE_ADDR']"</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
 
</html>

post-206516-0-76619500-1513101388_thumb.png

Link to comment
Share on other sites

Hi,

 

I assume you mean something like:

?>

<table width="50%" align="center">

<tr><td>REMOTE_ADDR: <td/><td><?php echo $_SERVER['REMOTE_ADDR']; ?></td></tr>
<tr><td>HTTP_USER_AGENT: <td/><td><?php echo $_SERVER['HTTP_USER_AGENT']; ?></td></tr>
<tr><td>HTTP_REFERER: <td/><td><?php if(isset($_SERVER['HTTP_REFERER'])) {echo $_SERVER['HTTP_REFERER'];} ?></td></tr>
<tr><td>Date: <td/><td><?php echo date("r"); ?></td></tr>

</table>

<?php

Link to comment
Share on other sites

 

Hi,

 

I assume you mean something like:

?>

<table width="50%" align="center">

<tr><td>REMOTE_ADDR: <td/><td><?php echo $_SERVER['REMOTE_ADDR']; ?></td></tr>
<tr><td>HTTP_USER_AGENT: <td/><td><?php echo $_SERVER['HTTP_USER_AGENT']; ?></td></tr>
<tr><td>HTTP_REFERER: <td/><td><?php if(isset($_SERVER['HTTP_REFERER'])) {echo $_SERVER['HTTP_REFERER'];} ?></td></tr>
<tr><td>Date: <td/><td><?php echo date("r"); ?></td></tr>

</table>

<?php

Hi, 

You sir are an absolute genius i was stressing over this so much trying to get it to work!! 

Link to comment
Share on other sites

if you are doing a "whole php project" you might benefit from this style of coding:

$ra = $_SERVER['REMOTE_ADDR'];
$ua = $_SERVER['HTTP_USER_AGENT'];
if(isset($_SERVER['HTTP_REFERER']))
$hr = $_SERVER['HTTP_REFERER'];
else
$hr = '';
$dt = date('r');
$code=<<<heredocs
<table width="50%" align="center">
<tr><td>REMOTE_ADDR: <td/><td>$ra</td></tr>
<tr><td>HTTP_USER_AGENT: <td/><td>$ua</td></tr>
<tr><td>HTTP_REFERER: <td/><td>$hr</td></tr>
<tr><td>Date: <td/><td>$dt</td></tr>
</table>
heredocs;
echo $code;

 

Much easier to read and to maintain. Look up "heredocs" in the manual.

 

And if you really are doing a "project" you should look into getting rid of out-dated styles such as "align='center'" and use CSS.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.