rnintulsa Posted September 4, 2008 Share Posted September 4, 2008 Hi, I am not getting any text on the page that is supposed to tell me the number of hits to my site. New to php and working on this one for three days now. Need help. Searched high and low for a hidden counter tutorial that would work on my index.html page. Both of these pages are in a folder named log, and the file permissions of that folder are set to chmod 777 command. This is the Javascript I put in my head tags on the index page. <script language="javascript" type="text/javascript" src="/log/webcounter.php"></script> This is the php page that does the processing called webcounter.php <?php $maxlines = 100; // the maximum number of lines to show $fn = "log.txt"; // the name of the log file to create $s = 'document.write(\'© 2008 All Rights Reserved\')'; $host = 'my host'; // the shortest url // get referring page $page = @$HTTP_REFERER; $uarray = parse_url($page); $refhost = $uarray["host"]; // check referring page is on this site if ( strpos($refhost, $host) === false ) { exit ("Access denied from: $refhost"); } // create new file if necessary if (!file_exists($fn)) { $fp = fopen ($fn, "w"); $os = "0\n"; $fw = fwrite($fp, $os); fclose($fp); } // read existing data $fa = file($fn); $nl = count($fa); $c = $fa[0] + 1; // inc hit // write data to file $fp = fopen ($fn, "w"); if (flock($fp, LOCK_EX)) { //lock the file $os = $c . "\n\n"; $fr = fwrite($fp,$os); $ip = getenv("REMOTE_ADDR"); $os = $uarray["path"]."\t".date("D M jS g:ia")."\t$ip\t".$_GET['ref']; $fr = fwrite($fp,$os); if ($nl > $maxlines) { $nl = $maxlines; } if ($nl > 1) { for ($i = 1; $i < $nl; $i++) $fr = fwrite($fp,$fa[$i]); } flock($fp, LOCK_UN); //release lock fclose($fp); } //output the javascript text echo ($s); ?> If I understand right, I should be able to go to the log.txt via my browser to view the current number of hits, right? I got this code online while searching for a tutorial. I appreciate all suggestions and explanations. Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/ Share on other sites More sharing options...
MatthewJ Posted September 4, 2008 Share Posted September 4, 2008 After closer inspection, I see that it is outputting javascript at the end of the file . So editing this for stupidity and sleepy eyes Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633667 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 Thank you for looking. But my index page is an html page. Doesn't it have to be a php page in order to include a php file? Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633669 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 True, but even that copyright line does not show up when I try to view the log.txt page inside the log folder. Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633673 Share on other sites More sharing options...
Hinty Posted September 4, 2008 Share Posted September 4, 2008 <!--#include virtual="/log/webcounter.php""--> Don't no if it will work Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633675 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 Ok glad you caught it. I will ignore Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633680 Share on other sites More sharing options...
Hinty Posted September 4, 2008 Share Posted September 4, 2008 u put it in the body of the html but not sure if it will work tbh, never actually tried it Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633682 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 I'm a bit confused. What do I put in the body? Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633683 Share on other sites More sharing options...
Hinty Posted September 4, 2008 Share Posted September 4, 2008 <!--#include virtual="/log/webcounter.php""--> put that anywhere in the <body></body> in your html Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633685 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 I put that in, and to view the output I am supposed to go to the log/log.txt page, and what should be on there is the hit count and the copyright line, right? I see a blank page. Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633689 Share on other sites More sharing options...
discomatt Posted September 4, 2008 Share Posted September 4, 2008 Your best bet here is to just call the PHP file via AJAX on body load. Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633690 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 Thank you discomatt, but I know not one thing about AJAX. Is it something I could do with limited php knowledge? Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633692 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 I have been searching to learn more about AJAX and how I could possibly use it to call the php file. Can anyone help me understand what discomatt meant? I know I would use this XMLHttpRequest but how and where? I will keep looking to understand, but this is new to me and so is php. Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633704 Share on other sites More sharing options...
discomatt Posted September 4, 2008 Share Posted September 4, 2008 Well, your BEST bet is to just have PHP pages and include the counter script directly, but if it's not an option http://www.w3schools.com/Ajax/Default.Asp Will give you the basics on calling an external page with javascript. It's up to you to learn a bit of javascript to apply it to your site. I'll gladly help but I won't code for you Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633706 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 Unfortunately these pages have to be html pages. Doesn't the way I have it : <script language="javascript" type="text/javascript" src="/log/webcounter.php"></script> call the external page? Can you explain why this javascript code will not work. I am trying to learn, and explanations really help me. I will now go to the link you posted, thanks. I am new to php and javascript. Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633713 Share on other sites More sharing options...
discomatt Posted September 4, 2008 Share Posted September 4, 2008 Well, it most case it will work, but it is wrong in a standards-complaint sense... I suppose as long as the PHP identifies itself as a JS file ( proper headers ) and no content/valid javascript is returned, there shouldn't be an issue. Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633718 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 I didn't see your answer ...thanks Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633732 Share on other sites More sharing options...
rnintulsa Posted September 4, 2008 Author Share Posted September 4, 2008 Well, I wonder why the tutorial did it this way. Where can I get a decent tutorial on how to create a hidden counter for an html page. I know html and css. I am learning php and javascript. But I need the counter this week. Link to comment https://forums.phpfreaks.com/topic/122710-hit-counter/#findComment-633737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.