Visitor Statistics and IP Log

I’ll look at it again, did you get my PM?



thanks

I spent hours troubleshoot it but it doesn’t work :(, follow is the complete main.tpl



[HTML]{* $Id: main.tpl 936 2005-11-21 12:14:30Z vladimir $ *}




{if !$hide_sideboxes}

{/if}







{if !$hide_sideboxes}

{/if}


{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="L" show_order="B"}
{/if}
{include file="side_boxes/categories.tpl"}
{include file="addons/manufacturers/sidebox_manufacturers.tpl"}
{*{if $settings.Modules.bestsellers == 'Y'}
{include file="addons/bestsellers/sidebox.tpl"}
{/if}
{**[ADDONS_DATA]**}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="L" show_order="A"}
{/if}

{include file="side_boxes/information_center.tpl"}
{include file="side_boxes/fta_channels.tpl"}

{**[/ADDONS_DATA]**}


{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="C" show_order="B"}
{/if}
{include file="content.tpl"}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="C" show_order="A"}
{/if}

{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="R" show_order="B"}
{/if}

{*{if !$auth.user_id}
{include file="side_boxes/login.tpl"}
{else}
{include file="side_boxes/logout.tpl"}
{/if} *}
{if $settings.Modules.news_and_emails == 'Y'}
{include file="addons/news_and_emails/side_boxes/news.tpl"}
{/if}
{if $settings.Modules.bestsellers == 'Y'}
{include file="addons/bestsellers/sidebox.tpl"}
{/if}
{include file="side_boxes/stats.tpl"}
{include file="side_boxes/recent_products.tpl"}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="R" show_order="A"}
{/if}

[/HTML]

This is how your main should look if that is how you want your site to show:

```php

{* $Id: main.tpl 936 2005-11-21 12:14:30Z vladimir $ *}




{if !$hide_sideboxes}

{/if}







{if !$hide_sideboxes}

{/if}



{**[ADDONS_DATA]**}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="L" show_order="B"}
{/if}
{**[ADDONS_DATA]**}

{include file="side_boxes/categories.tpl"}
{include file="addons/manufacturers/sidebox_manufacturers.tpl"}
{[COLOR=Red]*[/COLOR]if $settings.Modules.bestsellers == 'Y'}
{include file="addons/bestsellers/sidebox.tpl"}
{/if[COLOR=Red]*[/COLOR]}
{include file="side_boxes/site_info.tpl"}

[COLOR=Red]{include file="side_boxes/information_center.tpl"}
{include file="side_boxes/fta_channels.tpl"}[/COLOR]

{**[ADDONS_DATA]**}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="L" show_order="A"}
{/if}
{**[/ADDONS_DATA]**}


{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="C" show_order="B"}
{/if}

{include file="content.tpl"}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="C" show_order="A"}
{/if}

{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="R" show_order="B"}
{/if}

{[COLOR=Red]*[/COLOR]if !$auth.user_id}
{include file="side_boxes/login.tpl"}
{else}
{include file="side_boxes/logout.tpl"}
{/if[COLOR=Red]*[/COLOR]}
{if $settings.Modules.news_and_emails == 'Y'}
{include file="addons/news_and_emails/side_boxes/news.tpl"}
{/if}
{if $settings.Modules.bestsellers == 'Y'}
{include file="addons/bestsellers/sidebox.tpl"}
{/if}
[COLOR=Red]{include file="side_boxes/stats.tpl"}[/COLOR]
{include file="side_boxes/recent_products.tpl"}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="R" show_order="A"}
{/if}


```
As you can see from my example on the url i gave you it works perfect, have you created a Data base and have you entered the correct information into each of the files in the right places.

okay if I used the code in post 13 it log visitor IP but, the page won’t display. I used the code you just post, the page will display but it won’t log

ok this is the new version (without the visitor template) this how I wanted to look.



```php {* $Id: main.tpl 936 2005-11-21 12:14:30Z vladimir $ *}




{if !$hide_sideboxes}

{/if}







{if !$hide_sideboxes}

{/if}



{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="L" show_order="B"}
{/if}

{include file="side_boxes/categories.tpl"}
{include file="addons/manufacturers/sidebox_manufacturers.tpl"}
{include file="side_boxes/information_center.tpl"}
{include file="side_boxes/fta_channels.tpl"}

{**[ADDONS_DATA]**}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="L" show_order="A"}
{/if}
{**[/ADDONS_DATA]**}


{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="C" show_order="B"}
{/if}

{include file="content.tpl"}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="C" show_order="A"}
{/if}

{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="R" show_order="B"}
{/if}

{if !$auth.user_id}
{include file="side_boxes/login.tpl"}
{else}
{include file="side_boxes/logout.tpl"}
{/if}
{if $settings.Modules.news_and_emails == 'Y'}
{include file="addons/news_and_emails/side_boxes/news.tpl"}
{/if}
{if $settings.Modules.bestsellers == 'Y'}
{include file="addons/bestsellers/sidebox.tpl"}
{/if}
{include file="side_boxes/recent_products.tpl"}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="R" show_order="A"}
{/if}


```

When I Added the stats template to

```php {/if}

{include file="side_boxes/categories.tpl"}
{include file="addons/manufacturers/sidebox_manufacturers.tpl"}
{include file="side_boxes/information_center.tpl"}
{include file="side_boxes/fta_channels.tpl"}
[COLOR="Red"]{include file="side_boxes/stats.tpl"}[/COLOR]

{**[ADDONS_DATA]**}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="L" show_order="A"}
{/if} ```

the page won't display right, and if I added the stats.tpl to

```php {/if}
[COLOR="Red"]{include file="side_boxes/stats.tpl"}[/COLOR]
{include file="side_boxes/recent_products.tpl"}
{if $settings.Modules.ads == 'Y'}
{include file="addons/ads/ads.tpl" location="R" show_order="A"}
{/if}


{/if} ```

the page display like it I wanted, but it won't log the visitor.

Yes I check the database, database username, pass all that

Hi taydu



One last look, post your stats.tpl as you have it now, not a copy of my stats.tpl and replace your DB info with XXXX so it cannot be seen.

okay to make this easy I install a free cart and still get some error, can you please take a look at [url]http://www.ftamegadiscount.com/Test/[/url].



thanks

It seems to be a mixture of thing but mostly Data-Base errors, post your stats.tpl for me, and follow the instructions a gave above.

[HTML]{* $Id: site_info.tpl 1096 2006-01-16 11:20:18Z zeke $ *}

{capture name=“sidebox”}



{php}

// set your infomation.

$dbhost=‘localhost’;

$dbusername=‘MY DATABASE USERNAME’;

$dbuserpass=‘MY DATABASE PASSWORD’;



// connect to the mysql database server.

mysql_connect ($dbhost, $dbusername, $dbuserpass);

//select the database

mysql_select_db(‘MY DATABASE NAME’) or die(‘Cannot select database’);



$query = “SELECT COUNT(ip) AS numrows FROM stats WHERE ip='”.$_SERVER[“REMOTE_ADDR”].“'”;

$result = mysql_query($query) or die(‘Error, query failed’);

$row = mysql_fetch_array($result);

$numrows = $row[‘numrows’];



if ($numrows == 0) {

mysql_query(“INSERT INTO stats(ip,hits,activity,user_agent)VALUES ('”.$_SERVER[“REMOTE_ADDR”].“‘,‘1’,now(),’{$_SERVER[‘HTTP_USER_AGENT’]}')”);

}else{

mysql_query(“UPDATE stats SET hits=hits+1 WHERE ip='”.$_SERVER[‘REMOTE_ADDR’].“'”);

}



$result = mysql_query(“SELECT COUNT(ip) AS numrows FROM stats”);

$row = mysql_fetch_array($result);

$unique = $row[‘numrows’];



$result = mysql_query(“SELECT hits FROM stats”);

while ($row = mysql_fetch_array($result)) {

$hits += $row[‘hits’];

}

{/php}



























{$lang.site_stats}
{$lang.unique} {php} echo $unique; {/php}
{$lang.hits} {php} echo $hits; {/php}


{/capture}
{include file="common_templates/sidebox.tpl" title=$lang.site_info content=$smarty.capture.sidebox icon="sidebox_icon_help.gif"}[/HTML]

I have updated the original post on first page, try that stats.tpl and the style.css code.



Other than that all i can say is that this mod works 100% as you can see.



Looking at your test site and the errors,


/customer/%%45^45E^45E480CD%%[COLOR="Red"]index.tpl.php[/COLOR]
/customer/%%72^72D^72DAF6E8%%[COLOR="Red"]main.tpl.php[/COLOR]
/customer/%%D4^D49^D4912800%%[COLOR="Red"]stats.tpl.php[/COLOR]




file extentions look odd

works great, thx.



one addition?

in visitor.php change:



$row[‘ip’]

to

gethostbyaddr($row[‘ip’])



this shows the DNS name in case it is available, otherwise the IP.

something like “billgates.hr.seattle.home.com” or so…: makes more sense:D















In case the host-lookup still doesnt work or takes too long try this:



change

$row[‘ip’]

to

gethostbyaddr_timeout($row[‘ip’],‘ns1.yourdomainoranygoodnameserver.com’)



and add the code underneath @ the bottom of visitor.php (and set the timeout=### to your needs, i have it set to 500ms):




function gethostbyaddr_timeout($ip, $dns, $timeout=500)
{
// random transaction number (for routers etc to get the reply back)
$data = rand(0, 99);
// trim it to 2 bytes
$data = substr($data, 0, 2);
// request header
$data .= "\1\0\0\1\0\0\0\0\0\0";
// split IP up
$bits = explode(".", $ip);
// error checking
if (count($bits) != 4) return "ERROR";
// there is probably a better way to do this bit...
// loop through each segment
for ($x=3; $x>=0; $x--)
{
// needs a byte to indicate the length of each segment of the request
switch (strlen($bits[$x]))
{
case 1: // 1 byte long segment
$data .= "\1"; break;
case 2: // 2 byte long segment
$data .= "\2"; break;
case 3: // 3 byte long segment
$data .= "\3"; break;
default: // segment is too big, invalid IP
return "INVALID";
}
// and the segment itself
$data .= $bits[$x];
}
// and the final bit of the request
$data .= "\7in-addr\4arpa\0\0\x0C\0\1";
// create UDP socket
$handle = @fsockopen("udp://$dns", 53);
// send our request (and store request size so we can cheat later)
$requestsize=@fwrite($handle, $data);

@socket_set_timeout($handle, $timeout - $timeout%1000, $timeout%1000);
// hope we get a reply
$response = @fread($handle, 1000);
@fclose($handle);
if ($response == "")
return $ip;
// find the response type
$type = @unpack("s", substr($response, $requestsize+2));
if ($type[1] == 0x0C00) // answer
{
// set up our variables
$host="";
$len = 0;
// set our pointer at the beginning of the hostname
// uses the request size from earlier rather than work it out
$position=$requestsize+12;
// reconstruct hostname
do
{
// get segment size
$len = unpack("c", substr($response, $position));
// null terminated string, so length 0 = finished
if ($len[1] == 0)
// return the hostname, without the trailing .
return substr($host, 0, strlen($host) -1);
// add segment to our host
$host .= substr($response, $position+1, $len[1]) . ".";
// move pointer on to the next segment
$position += $len[1] + 1;
}
while ($len != 0);
// error - return the hostname we constructed (without the . on the end)
return $ip;
}
return $ip;
}




Rudy

Hi nwsco



Thanks for the addition? great, and for letting me know it works on other system.



John

Hi John!

Sure it worked! Following your directions-> done in 5 mins: they are perfect!



I think most of the probs mentioned above are user-errors some way or another…



Doei!

[quote name=‘nwsco’]Hi John!

Sure it worked! Following your directions-> done in 5 mins: they are perfect!



I think most of the probs mentioned above are user-errors some way or another…



Doei![/QUOTE]



grrrr, i’ll do a fresh installation and see if it work.

Zardos



when you said ```php create the three new languages {$lang.site_stats} and {$lang.unique} and {$lang.hits}

```



did you mean add three variable called: $lang.site_stats, $lang.unique, and $lang.hits or just site_stats, unique, and hits???

Hi taydu



In your admin go to languages and add the three variable mentioned, (stite_stats, unique, hits) yes.

yes, it got that since the beginning, check check and check but still not working. I’ll try it again



thanks

i’m mad, check, and double check the setting but still get the same error grrr

b.t.w. taydu, i see in your code (dunno if its yours though) a few

posts back that you use this code:



{php}
// set your infomation.
$dbhost='localhost';
$dbusername='MY DATABASE USERNAME';
$dbuserpass='MY DATABASE PASSWORD';




that is the worst thing one could do, since all code in a .tpl file

so can be read like a .txt file by anyone…



i would never do this…maybe you have a reason? (that i cant imagine lol)

[color=Red][size=+2]MAJOR SECURITY PROBLEM - STOP AND REMOVE THIS SCRIPT IMMEDIATLY[/size][/color]



The correct way is make a new file visitorstats.php and paste the below php code into it this new file, save this new file in dir…/include/common/



Open…/include/common/customer_common.php and find:

include $common_dir.‘topics.php’;



add underneath:

include $common_dir.‘visitorstats.php’;



The script should work as previously and securly…


```php


if ( !defined('IN_CSCART') ) { die('Access denied'); }

$query = "SELECT COUNT(ip) AS numrows FROM stats WHERE ip='".$_SERVER["REMOTE_ADDR"]."'";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result);
$numrows = $row['numrows'];

if ($numrows == 0) {
mysql_query("INSERT INTO stats(ip,hits,activity,user_agent)VALUES ('".$_SERVER["REMOTE_ADDR"]."','1',now(),'{$_SERVER['HTTP_USER_AGENT']}')");
}else{
mysql_query("UPDATE stats SET hits=hits+1 WHERE ip='".$_SERVER['REMOTE_ADDR']."'");
}

$result = mysql_query("SELECT COUNT(ip) AS numrows FROM stats");
$row = mysql_fetch_array($result);
$unique = $row['numrows'];

$result = mysql_query("SELECT hits FROM stats");
while ($row = mysql_fetch_array($result)) {
$hits += $row['hits'];
}

?>

```