Hi folks.
A very dumb question from a HTML / PHP beginner...
I have written a simple hit counter prog which I can access by running: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?we...
I now want to integrate this into a HTML website page, so that this remote script "counter.php" is run and the result displayed on the HTML webpage each time the page is viewed.
I have created a simple HTML page: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/count_page.htm...
which just consists of:
<HTML> <BODY> <script src="http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?webpage=main"></script> </BODY> </HTML>
When I go to this count_page.html webpage the hit counter is incremented but the result is not displayed on the webpage (even though it's output is visible when I run the script counter.php directly).
How do I get the result from the remote script counter.php to display on my count_page.html page?
sagr
On Tue, 2007-12-04 at 10:25 +0000, sagr wrote:
Hi folks.
A very dumb question from a HTML / PHP beginner...
I have written a simple hit counter prog which I can access by running: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?we...
I now want to integrate this into a HTML website page, so that this remote script "counter.php" is run and the result displayed on the HTML webpage each time the page is viewed.
I have created a simple HTML page: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/count_page.htm...
which just consists of:
<HTML> <BODY> <script src="http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?webpage=main"></script> </BODY> </HTML>
When I go to this count_page.html webpage the hit counter is incremented but the result is not displayed on the webpage (even though it's output is visible when I run the script counter.php directly).
How do I get the result from the remote script counter.php to display on my count_page.html page?
Hi sagr,
Your problem here is that the counter page is not generating valid ECMAscript (aka Javascript) to insert elements into the outer page. There are a couple of solutions that spring to mind:
[1] Counter page generates valid ECMAscript, eg: document.write(counter);
[2] Counter page generates vallid HTML, which you include in an IFRAME: <html><body>$counter</body></html> main page contains: <iframe src="...counter.php?page=main"/>
[3] Since it's PHP, just include the counter page in all other pages? This does mean you cannot have static HTML pages, everything must be a PHP.
HTH, Phil.
Hi Phil,
Your problem here is that the counter page is not generating valid ECMAscript (aka Javascript) to insert elements into the outer page.
Oh dear... sounds like this little project is a bit more complex than I thought.
There are a couple of solutions that spring to mind:
[1] Counter page generates valid ECMAscript, eg: document.write(counter);
[2] Counter page generates valid HTML, which you include in an IFRAME:
<html><body>$counter</body></html> main page contains: <iframe src="...counter.php?page=main"/>
Thanks for those helpful suggestions, and the suggestions of other ALUGers. I will try a few experiments along the lines of what you suggest.
[3] Since it's PHP, just include the counter page in all other pages? This does mean you cannot have static HTML pages, everything must be a PHP.
Unfortunately some of the pages which I wish to use this remote PHP script are hosted on "free" webhosts and the webspace they provide is very basic and not PHP/SQL enabled. Hence why I want to host the script on my main PHP enabled webspace but to sometimes access it from basic HTML (not PHP enabled) webspace pages.
Thanks to yourself, Brett and Dave I now have some ideas for the way forward.
Sagr.
On Tue, Dec 04, 2007 at 10:25:09AM -0000, sagr wrote:
Hi folks.
A very dumb question from a HTML / PHP beginner...
I have written a simple hit counter prog which I can access by running: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?we...
I now want to integrate this into a HTML website page, so that this remote script "counter.php" is run and the result displayed on the HTML webpage each time the page is viewed.
I have created a simple HTML page: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/count_page.htm...
which just consists of:
<HTML> <BODY> <script src="http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?webpage=main"></script> </BODY> </HTML>
When I go to this count_page.html webpage the hit counter is incremented but the result is not displayed on the webpage (even though it's output is visible when I run the script counter.php directly).
<script> doesn't do what you appear to think it does. It points to a location for a javascript file (or other supported scripting language), you are not getting back a javascript file, you're getting some numbers.
You should be able to do something like: <script language="javascript"> url = "http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?we..." reqObj = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0"); reqObj.open("GET", url, false); reqObj.send(""); count = reqObj.responseText(); document.write(count); </script>
Or... you could use something like SSI to call it.
Cheers,
On Tue, 2007-12-04 at 10:25 +0000, sagr wrote:
Hi folks.
A very dumb question from a HTML / PHP beginner...
I have written a simple hit counter prog which I can access by running: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?we...
I now want to integrate this into a HTML website page, so that this remote script "counter.php" is run and the result displayed on the HTML webpage each time the page is viewed.
I have created a simple HTML page: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/count_page.htm...
which just consists of:
<HTML> <BODY> <script src="http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?webpage=main"></script> </BODY> </HTML>
When I go to this count_page.html webpage the hit counter is incremented but the result is not displayed on the webpage (even though it's output is visible when I run the script counter.php directly).
How do I get the result from the remote script counter.php to display on my count_page.html page?
The HTML <script> tag is for embedding JavaScript; you cant include a PHP file that way IIRC.
Try renaming the html file to have a .php extension, and then replace the <script> ... </script> with "<?php include('http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?webpage=main'); ?>
Hope this helps.
Dave Crisp wrote:
The HTML <script> tag is for embedding JavaScript; you cant include a PHP file that way IIRC.
Try renaming the html file to have a .php extension, and then replace the <script> ... </script> with "<?php include('http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?webpage=main'); ?>
Based on the OP's choice of Subject ("... *remote* PHP script...") I assume that the calling code should not be assumed to be on the same server, and therefore should not be assumed to be running PHP (although clearly in the OP's example the host is the same). It looks like an attempt to build a fairly standard counter script (a good way to learn some PHP, by the way).
Brett's Javascript is the best way to do this without changing the counter script, IMHO.
On the other hand, assuming that changing the counter script is OK, I am guessing that the script ends something like: echo $counter; .. which could be replaced by: echo "document.write($counter);"; .. to turn it into a Javascript response that the OP's HTML could use unchanged.
Or: if (!empty($_GET['js'])) echo "document.write($counter);"; else echo $counter;
.. then change the HTML from: <script src="http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?webpage=main"></script> .. to .. <script src="http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?js=1&webpage=main"></script>
That way, whether or not it outputs the script instead of the raw value is configurable depending on the parameters passed to the counter script.
Any or all of the suggestions in this thread will work of-course, and to the OP: "welcome to PHP!"
Hi Mark,
Based on the OP's choice of Subject ("... *remote* PHP script...") I assume that the calling code should not be assumed to be on the same server, and therefore should not be assumed to be running PHP
Correct. The calling code will sometimes be located on non PHP webspace so must work from within a basic HTML page.
(although clearly in the OP's example the host is the same).
I just put the calling code page on our server to provide a test example in case ALUGers wanted to run it.
It looks like an attempt to build a fairly standard counter script (a good way to learn some PHP, by the way).
Yes, I am trying to learn PHP. We use 3rd Party Hit Counters at the moment but they are very fragmented with each website having it's own separate hit counter (and in some cases individual pages having their own counters). I am aiming to organise this chaos and have all our individual websites report their counts to a single central admin website directory so I can, when I gain the skill, do some more detailed overall analysis of visitor patterns across our various websites using just one database.
Brett's Javascript is the best way to do this without changing the counter script, IMHO.
Yes, his suggestion was very helpful (but a little beyond my understanding at the moment).
On the other hand, assuming that changing the counter script is OK
Yes.
I am guessing that the script ends something like: echo $counter; .. which could be replaced by...
if (!empty($_GET['js'])) echo "document.write($counter);"; else echo $counter;
Thanks for that idea which I have now implemented.
.. then change the HTML from:
<script src="http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?webpage=main"></script>
.. to ..
<script src="http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter.php?js=1&webpage=main"></script>
That way, whether or not it outputs the script instead of the raw value is configurable depending on the parameters passed to the counter script.
Excellent! Works a treat!
Thanks to everyone for their helpful tips and suggestions.
Sagr.
sagr wrote:
Yes, I am trying to learn PHP. We use 3rd Party Hit Counters at the moment but they are very fragmented with each website having it's own separate hit counter (and in some cases individual pages having their own counters). I am aiming to organise this chaos and have all our individual websites report their counts to a single central admin website directory so I can, when I gain the skill, do some more detailed overall analysis of visitor patterns across our various websites using just one database.
There's doubtless some standard packages available for this but your desire to create one yourself is laudable.
Something to consider (if you haven't already) would be to use a session cookie within the script so that refreshes from a single visit do not increase the count (unless you want a high count :-)
Brett's Javascript is the best way to do this without changing the counter script, IMHO.
Yes, his suggestion was very helpful (but a little beyond my understanding at the moment).
Basically, what it did was use a JavaScript function to download the output of your counter script then display it. (Once you know that, his code is probably fairly easy to follow.) The functions he used are the basis of AJAX and all sorts of clever web features these days, so worth having seen (even if they are probably overkill for your needs!).
[...] Thanks for that idea which I have now implemented.
No problem, glad to be of assistance.
On Tuesday, December 04, 2007 6:18 PM, Mark Rogers said:
There's doubtless some standard packages available for this but your desire to create one yourself is laudable.
My simple prog is rather basic (you can sneak a peak at: http://www.suffolk-ancestor-genealogy-research.co.uk/counters/counter_admin.... ) but is good enough for the moment while I learn more skills to refine it.
Something to consider (if you haven't already) would be to use a session cookie within the script so that refreshes from a single visit do not increase the count (unless you want a high count :-)
I like the idea of counting raw page views as it gives me an idea of which pages are most popular.
I am not sure about your session idea. I do not understand the concept fully but it sounds like this will just record the first page a visitor loads. I suppose this could be a handy stat to have in addition to raw views as I suppose this session count would give an indication about the first page a visitor loads on our website. Our website is very simple and basic at the moment but this coming year we hope to greatly increase the number of pages and amount of content so I suppose there is a chance our website may then become listed on Google et al. When this occurs I suppose your session idea would let us discover which pages people arrive at via search engines as well as how man unique visitors we get.
Sounds like I have a lot of homework to do!
Thanks once again for your help.
Sagr.
sagr wrote:
I like the idea of counting raw page views as it gives me an idea of which pages are most popular.
You need to distinguish between: - me viewing three different pages - me viewing the same page three times in one session (ie a few refreshes) - three people viewing the same page
You could make a note within a session cookie which pages have been viewed by the visitor and only increment the counter if the page isn't already in the session cookie. You'd still get counts of each page, just not the refreshes.
Otherwise, can you tell how many people have come to your homepage? Given that a typical visitor probably goes to and from the home page as they navigate around the site?
I am not sure about your session idea. I do not understand the concept fully but it sounds like this will just record the first page a visitor loads. I suppose this could be a handy stat to have in addition to raw views as I suppose this session count would give an indication about the first page a visitor loads on our website. Our website is very simple and basic at the moment but this coming year we hope to greatly increase the number of pages and amount of content so I suppose there is a chance our website may then become listed on Google et al. When this occurs I suppose your session idea would let us discover which pages people arrive at via search engines as well as how man unique visitors we get.
To be honest if you're serious about this there are lots of 3rd party apps that do all the hard work for you. For example I've just started playing with phpMyVisites (it originated in France hence the spelling). With tools like this you can work out all sorts of things about landing pages, routes through the site etc. Also look up Google Analytics as a free 3rd party service.
Also, you might find that a CMS package (Joomla, CMS Made Simple, etc) might make it easier to build an maintain your site, and most of them build tracking into the sites automatically. You would of-course need PHP (and MySQL) on the host site for that to be relevant.
That notwithstanding, it's worth learning the stuff yourself for your own benefit.