Click counter without reset (with php)
Solvedletocest Posted messages 34 Status Member -
Hello,
I would like the people interested in a project on my site to be counted by clicking a button.
I found the script in HTML/Java, but the counter resets to zero when you leave the page.
I understand that I need to use PHP to keep the displayed number, but I don't have a good grasp of it (I'm a tinkerer, but not a champion).
Could someone explain to me (simply) how to do it? With example scripts, if possible.
I know I'm asking a lot, but I learn quickly...
Sincerely,
18 answers
-
It's me again. I just saw that I might need to use Ajax. But apart from Sophocles, Ajax...
-
Hello,
So yes, you will need JavaScript, Ajax, and a server-side language (such as PHP).
Then, the first question to ask is: where do you want to store the data from this counter? In a database or a simple file?
If it's a database, you will also need to learn SQL to be able to make INSERT or UPDATE queries.
Here are examples of Ajax JavaScript (jQuery) scripts: https://codes-sources.commentcamarche.net/source/102253-exemple-ajax-en-jquery
You will certainly find simpler ones all over the web...
Come back to us with your attempts if you run into any issues... we’ll see how we can help you fix it
.
Best regards,
Jordane -
Thank you very much. I already found an example on another site, with PHP and everything. The counter displays but doesn't increment. I will check your link and I am sure I will end up putting the pieces together. See you soon. Thanks again.
-
Hello,
here are some files I found for my counter. From what I understand, the data is recorded in files (PHP5 and clickcount.data) and not in a database. Is that correct?
1) Index
<?xml version="1.0" encoding="iso-8859-15"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>AJAX: Example client</title> <script type="text/javascript" src=""></script> </head> <body> <p> <a href="" onclick="gestionClic(); return false;">Click here!</a> (Number of clicks: <span id="nbr_clics"> <?php $str = @file_get_contents('compteur/clickcount.data'); if($str !== FALSE) echo unserialize($str); else echo 0; ?></span>) </p> </body> </html>2) /compteur/clickcount.data
03) /compteur/compteur.php5
<?php sleep(1); $nbr = 1; $str = @file_get_contents('clickcount.data'); if($str !== FALSE) $nbr = unserialize($str)+1; header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); echo $nbr; file_put_contents('clickcount.data', serialize($nbr)); ?>4) /js/compteur-click.js
var http; // Our XMLHttpRequest object function createRequestObject() { var http; if(window.XMLHttpRequest) { // Mozilla, Safari, ... http = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer http = new ActiveXObject("Microsoft.XMLHTTP"); } return http; } function gestionClic() { document.getElementById('nbr_clics').innerHTML = '<em>Loading...</em>'; http = createRequestObject(); http.open('get', '.compteur/compteur.php5', true); http.onreadystatechange = handleAJAXReturn; http.send(null); } function handleAJAXReturn() { if(http.readyState == 4) { if(http.status == 200) { document.getElementById('nbr_clics').innerHTML = http.responseText; } else { document.getElementById('nbr_clics').innerHTML = "<strong>N/A</strong>"; } } }I have several problems:
even though the PHP5 and clickcount.data files are incrementing, the result is N/A.
I did see the line of code in the compteur-click.js file.Excerpts:
(...)
function handleAJAXReturn() { if(http.readyState == 4) { if(http.status == 200) { document.getElementById('nbr_clics').innerHTML = http.responseText; } else { document.getElementById('nbr_clics').innerHTML = "<strong>N/A</strong>"; } }(...)
I don't know what's wrong and what I need to change.Additionally, I tried to modify the text, the case of the characters, etc., on the main page (index) with Kompozer, and everything disappeared. This is a problem because, of course, I want to insert this counter in a corner of the main page with a text that announces and accompanies it.
Also, when refreshing the index with fileZilla, it seems that the increment does not continue from the previous day, which is a big problem because I want to regularly modify the main page (index) and keep the increment.
That's where I am. It's progressing...
If it's useful, the address is
http://letaucestnous.lescigales.org -
Hello,
here are some files I found for my counter. From what I understand, the data is recorded in files (PHP5 and clickcount.data) and not in a database. Is that right?
1) Index
<?xml version="1.0" encoding="iso-8859-15"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>AJAX : Example of client</title> <script type="text/javascript" src="""></script> </head> <body> <p> <a href="" onclick="gestionClic(); return false;">Click here!</a> (Number of clicks: <span id="nbr_clics"> <?php $str = @file_get_contents('compteur/clickcount.data'); if($str !== FALSE) echo unserialize($str); else echo 0; ?></span>) </p> </body> </html>2) /compteur/clickcount.data
0 -
3) /counter/counter.php5
<?php sleep(1); $nbr = 1; $str = @file_get_contents('clickcount.data'); if($str !== FALSE) $nbr = unserialize($str)+1; header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); echo $nbr; file_put_contents('clickcount.data', serialize($nbr)); ?>4) /js/counter-click.js
var http; // Our XMLHttpRequest object function createRequestObject() { var http; if(window.XMLHttpRequest) { // Mozilla, Safari, ... http = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer http = new ActiveXObject("Microsoft.XMLHTTP"); } return http; } function handleClick() { document.getElementById('nbr_clics').innerHTML = '<em>Loading...</em>'; http = createRequestObject(); http.open('get', '.counter/counter.php5', true); http.onreadystatechange = handleAJAXReturn; http.send(null); } function handleAJAXReturn() { if(http.readyState == 4) { if(http.status == 200) { document.getElementById('nbr_clics').innerHTML = http.responseText; } else { document.getElementById('nbr_clics').innerHTML = "<strong>N/A</strong>"; } } } -
I have several problems:
although the PHP5 files and clickcount.data are incrementing, the result is N/A.
I have seen the code snippet in the compteur-click.js file.Excerpt:
(...)function handleAJAXReturn() { if(http.readyState == 4) { if(http.status == 200) { document.getElementById('nbr_clics').innerHTML = http.responseText; } else { document.getElementById('nbr_clics').innerHTML = "<strong>N/A</strong>"; } } (...)(...)
I don’t know what’s wrong and what I need to change.Additionally, I wanted to modify the text, the case of the characters, etc. on the main page (index) using Kompozer, and everything disappeared. This is an issue because, of course, I want to insert this counter in a corner of the main page with accompanying text.
Also, when updating index with fileZilla, it seems that the incrementing does not continue from the previous day, which is a big problem since I want to regularly update the main page (index) and keep the incrementing.
This is where I am. It’s progressing... (I sent it in several messages because it wouldn’t fit all at once)
-
Start by renaming your file
compteur.php5tocompteur.phpThen, replace your JS code with this one
var http; // Our XMLHttpRequest object var urlAjax = './compteur/compteur.php'; function createRequestObject() { if(window.XMLHttpRequest) { // Mozilla, Safari, ... http = new XMLHttpRequest(); } else if(window.ActiveXObject){ // Internet Explorer http = new ActiveXObject("Microsoft.XMLHTTP"); } return http; } function gestionClic() { document.getElementById('nbr_clics').innerHTML = '<em>Loading...</em>'; http = createRequestObject(); http.open('get', urlAjax, true); http.onreadystatechange = handleAJAXReturn; // Error Handling: http.onerror = function(error){ console.error( 'AJAX Error', error ); } http.send(null); } function handleAJAXReturn() { if(http.readyState == 4) { if(http.status == 200){ document.getElementById('nbr_clics').innerHTML = http.responseText; } else { document.getElementById('nbr_clics').innerHTML = "<strong>N/A</strong>"; console.error( 'AJAX Error', http.status, http.responseText ); } } }And remember to check your browser's console for what appears when you click your button...
-
Great! It works! And it will remain even if I modify the index? For the rest, I will run some tests, and if you have any ideas... Thank you very much!
-
The problem that remains for me is modifying the format (text size, possibly the button, etc.) to insert this counter in a corner of the main page with an accompanying announcement text. With Kompozer, I changed the text and everything disappeared online. Strange...
-
I just returned to the site and the number of clicks is not displayed. It only shows up if I click the button (with +1). It's annoying because I would like the number (of participants) to always be displayed. It's important.
-
The sentence is
$str = @file_get_contents('clickcount.data');(without the error suppression/) which I replaced with
$str = file_get_contents('./clickcount.data');(without the @) but it didn't change anything
-
Are you sure about the way?
Can you modify the code like this
<?php $cmptFile = __DIR__ . '/clickcount.data'; if(!file_exists($cmptFile)){ echo "Error .. File " . $cmptFile . " missing or not found..."; } else { $str = file_get_contents($cmptFile); var_dump($str); // test time to see what your variable contains if($str !== FALSE) { echo unserialize($str); } else { echo 0; } } ?>-
I'm not sure about anything at all (path?) I replaced the php with your code, the result is quite curious... (see the image)

I'm not sure about anything (path?) I replaced the php with your code, the result is quite curious, and it disappears when you reload the page.
It gives:
We are: string(5) "i:13;" 13
(the italics are mine)
I put back the previous PHP that worked very well (We are: 14), but it disappears every time the page loads.
However, I don’t really understand your comment. I'm not trying much; I'm just picking bits of code here and there... I haven't inserted the piece of code
<body onload="gestionClic(nbr_clics);"> <!-- ...... -->because I wouldn’t know where to put it.
Thank you for your response. In fact, the files are not very different from the first ones I showed (with your modifications).
There are 4 files (I might upload them in several batches);
1) index.html
2) /compteur/compteur.php
3) /compteur/clickcount.data
4) /js/compteur-click.js
and first...
1) index.html (the counter part)
(...)
</div> <div> <script type="text/javascript" src=""></script> <p style="text-align: center;"><a href="" onclick="gestionClic(); return false"><img style="border: medium none ;" src=""></a> <br> </p> <p style="text-align: center;"><big><big>(We are: <span id="nbr_clics"> <?php $str = @file_get_contents('compteur/clickcount.data'); if($str !== FALSE) echo unserialize($str); else echo 0; ?></span>)(...) it's strange that the source address src (an image) has disappeared from the code here.
2) /compteur/compteur.php
<?php sleep(1); $nbr = 1; //$str = @file_get_contents('clickcount.data'); $str = file_get_contents('./clickcount.data'); if($str !== FALSE) $nbr = unserialize($str)+1; header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); echo $nbr; //file_put_contents('clickcount.data', serialize($nbr)); file_put_contents('./clickcount.data', serialize($nbr)); ?>-
3) /counter/clickcount.data
04) /js/counter-click.js
var http; // Our XMLHttpRequest object var urlAjax = './counter/counter.php'; function createRequestObject() { if(window.XMLHttpRequest) { // Mozilla, Safari, ... http = new XMLHttpRequest(); } else if(window.ActiveXObject){ // Internet Explorer http = new ActiveXObject("Microsoft.XMLHTTP"); } return http; } function gestionClic() { document.getElementById('nbr_clics').innerHTML = '<em>Loading...</em>'; http = createRequestObject(); http.open('get', urlAjax, true); http.onreadystatechange = handleAJAXReturn; // Error Handling: http.onerror = function(error){ console.error( 'AJAX Error', error ); } http.send(null); } function handleAJAXReturn() { if(http.readyState == 4) { if(http.status == 200){ document.getElementById('nbr_clics').innerHTML = http.responseText; } else { document.getElementById('nbr_clics').innerHTML = "<strong>N/A</strong>"; console.error( 'AJAX Error', http.status, http.responseText ); } } }The counter works very well, increments and all. The only problem (annoying) is that it disappears at each page load.
Is your file named index.html ?
That's surprising... it's rare for server settings to allow PHP code execution in a .html file.
Rename it to index.php .. it should work better.
If it still doesn't work, please post the COMPLETE code of your index.php file along with the generated source code. (To get the generated source code, you need to display the page in your browser and then use the keyboard shortcut CTRL+u, copy it and paste it here)
.
Best regards,
JordaneIt doesn't work anymore... I'm lost in all my files. I've tried them all and after (I think) reinstalling yesterday's ones, when I click on the image of the counter, the page scrolls back to the top.
I can't put the site link, but by being clever:
letaucestnous dot lescigales dot org (for now with index.html since it was working) so you can see for yourself if it's possible
I just realized that the messages are a mess because I was writing messages that didn't appear but actually showed up later. And since I tried several times... Regarding html or php, the counter works for both, but the page encoding for php is a bit twisted. I'm willing to provide the entire index page and its source code; I didn't know they differed.
Moreover, I tried to put an image instead of "click here," I managed to do it before and I can't anymore (the counter isn't working anymore - it was working, it must be the beginner's luck).
So let's start over:
1) index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title>Index maquette</title> </head><body style="width: 94px;"> <div style="position: absolute; left: 36px; top: 356px; height: 68px; width: 587px;"><big><big style="font-weight: bold;">Layer 1 What is it?</big></big><br> </div> <div style="position: absolute; left: 228px; top: 303px; height: 46px; width: 137px; z-index: 1;"><big>Layer 4</big><br> </div> <div style="position: absolute; left: 744px; top: 362px;"><big><big style="font-weight: bold;">Layer 2 Articles</big></big><br> </div> <div style="position: absolute; left: 644px; top: 275px; height: 51px; width: 185px;"><big> <big style="font-weight: bold;">Layer 7</big></big><br> </div> <div style="position: absolute; left: 374px; top: 7px;"><big> <img style="width: 250px; height: 250px;" alt="" src=""></big><br> </div> <big> <br> </big> <div style="position: absolute; left: 115px; top: 113px;"><big style="font-weight: bold; font-family: Curlz MT;"><big><big>Site still under construction....</big></big></big><br> </div> <big><br> </big> <div style="position: absolute; left: 552px; top: 504px; height: 262px; width: 318px;"> <div style="text-align: center;"><big> <big style="font-weight: bold;">Let's count ourselves!</big></big><br> <big><big style="font-weight: bold;">If you wish to signify your participation to <span style="font-style: italic;"><br> we are the clamp</span>, </big></big><br> </div> <div> <script type="text/javascript" src=""></script> </head> <body> <p> <a href="" onclick="gestionClic(); return false;">Click here!</a> (We are: <span id="nbr_clics"> <?php $str = @file_get_contents('compteur/clickcount.data'); if($str !== FALSE) echo unserialize($str); else echo 0; ?></span>) </big></big></p> ;</div> <br> </div> <div style="position: absolute; left: 417px; top: 309px;"><big> Layer 6</big><br> </div> <div style="position: absolute; left: 56px; top: 315px; width: 135px; text-align: center;"><big><big> Layer 5</big></big><br> </div> <div style="position: absolute; left: 746px; top: 423px;"><big><big>Layer 3 Websites</big></big><br> </div> </body></html>2) source code index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title>Index maquette</title> </head><body style="width: 94px;"> <div style="position: absolute; left: 36px; top: 356px; height: 68px; width: 587px;"><big><big style="font-weight: bold;">Layer 1 What is it?</big></big><br> </div> <div style="position: absolute; left: 228px; top: 303px; height: 46px; width: 137px; z-index: 1;"><big>Layer 4</big><br> </div> <div style="position: absolute; left: 744px; top: 362px;"><big><big style="font-weight: bold;">Layer 2 Articles</big></big><br> </div> <div style="position: absolute; left: 644px; top: 275px; height: 51px; width: 185px;"><big> <big style="font-weight: bold;">Layer 7</big></big><br> </div> <div style="position: absolute; left: 374px; top: 7px;"><big> <img style="width: 250px; height: 250px;" alt="" src=""></big><br> </div> <big> <br> </big> <div style="position: absolute; left: 115px; top: 113px;"><big style="font-weight: bold; font-family: Curlz MT;"><big><big>Site still under construction....</big></big></big><br> </div> <big><br> </big> <div style="position: absolute; left: 552px; top: 504px; height: 262px; width: 318px;"> <div style="text-align: center;"><big> <big style="font-weight: bold;">Let's count ourselves!</big></big><br> <big><big style="font-weight: bold;">If you wish to signify your participation to <span style="font-style: italic;"><br> we are the clamp</span>, </big></big><br> </div> <div> <script type="text/javascript" src=""></script> </head> <body> <p> <a href="" onclick="gestionClic(); return false;">Click here!</a> (We are: <span id="nbr_clics"> <?php $str = @file_get_contents('compteur/clickcount.data'); if($str !== FALSE) echo unserialize($str); else echo 0; ?></span>) </big></big></p> ;</div> <br> </div> <div style="position: absolute; left: 417px; top: 309px;"><big> Layer 6</big><br> </div> <div style="position: absolute; left: 56px; top: 315px; width: 135px; text-align: center;"><big><big> Layer 5</big></big><br> </div> <div style="position: absolute; left: 746px; top: 423px;"><big><big>Layer 3 Websites</big></big><br> </div> </body></html>All this is quite complicated, sorry.
See you soon,
-
Whoa...
To begin with.. you have multiple <head> </head> tags in your code... that’s not possible!
And in the <head> tags.. you shouldn't have any HTML code... just the meta tags, the title of your page, and possibly the loading of your JS scripts.
JS script, by the way.. which is not loaded since its src tag is empty!
I confirm .. the fact that your file is index.html and not .php .. the php code is not interpreted ....
That's why the counter does not display.
Can you, in addition to your index.html .. create a file index.php in the same folder and let me know when it's done?
You will need to check the url: https://letaucestnous.lescigales.org/index.php to see if it works.
If you really cannot change your file to .php ... then you will have no choice but to make an ajax call to read the information and display it on your page. (You can strongly base it on the script you used to increment the number of clicks ..)
In your js, create a function getclic
var http; // Our XMLHttpRequest object var urlAjax = './compteur/compteur.php'; var urlAjax2 = './compteur/compteur2.php'; function createRequestObject() { if(window.XMLHttpRequest) { // Mozilla, Safari, ... http = new XMLHttpRequest(); } else if(window.ActiveXObject){ // Internet Explorer http = new ActiveXObject("Microsoft.XMLHTTP"); } return http; } function gestionClic() { document.getElementById('nbr_clics').innerHTML = '<em>Loading...</em>'; http = createRequestObject(); http.open('get', urlAjax, true); http.onreadystatechange = handleAJAXReturn; // Error Handling: http.onerror = function(error){ console.error( 'AJAX Error', error ); } http.send(null); } function getClic() { document.getElementById('nbr_clics').innerHTML = '<em>Loading...</em>'; http = createRequestObject(); http.open('get', urlAjax2, true); http.onreadystatechange = handleAJAXReturn; // Error Handling: http.onerror = function(error){ console.error( 'AJAX Error', error ); } http.send(null); } function handleAJAXReturn() { if(http.readyState == 4) { if(http.status == 200){ document.getElementById('nbr_clics').innerHTML = http.responseText; } else { document.getElementById('nbr_clics').innerHTML = "<strong>N/A</strong>"; console.error( 'AJAX Error', http.status, http.responseText ); } } }And create a second php file
<?php // './compteur/compteur2.php'; sleep(1); $nbr = 0; //$str = @file_get_contents('clickcount.data'); $str = file_get_contents('./clickcount.data'); if($str !== FALSE) { $nbr = unserialize($str); } header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); echo $nbr; ?>And in your index.html file, call the getClic function when the page has finished loading
document.addEventListener("DOMContentLoaded", function() { getClic(); // we call the getClic function once the page is loaded });-
-
You're going to say I'm exaggerating, but how do I put an image instead of "click here?" I managed to do it before, but it doesn't work anymore... I swear this is my last question.
This coding issue for the page is a bit annoying. I opened a new tab to display the site again, and the hieroglyphics instead of the accented letters have reappeared...
-
For the encoding, you must use utf8
see here:
https://forums.commentcamarche.net/forum/affich-37584944-php-html-caracteres-accentues-et-l-utf8
Then, for your image, you need to put it in your <a> tag
<a href="" onclick="gestionClic(); return false;"> <img src="" alt="Click here!"/> </a> (We are: <span id="nbr_clics"> <?php $str = @file_get_contents('compteur/clickcount.data'); if($str !== FALSE) echo unserialize($str); else echo 0; ?></span>) -
-
-