3

I'm using this method to display different content based on the variable that was passed down from my previous page.

    <?php
    if (isset($_GET['click'])) {
        if($_GET['click'] == 'person'){
          file_put_contents('person.txt', ((int) file_get_contents('person.txt')) + 1);
          include 'resultperson.php';
        } elseif ($_GET['click'] == 'text'){
          file_put_contents('text.txt', ((int) file_get_contents('text.txt')) + 1);
          include 'resulttext.php';
        } elseif ($_GET['click'] == 'online'){
          file_put_contents('online.txt', ((int) file_get_contents('online.txt')) + 1);
          include 'resultonline.php';
        }
    } else {
        include 'resulttext.php';
    } 
    ?>  

Problem is, when I hit refresh, the file_put_contents() function will be executed again. It's just a way that I use to track how many times users have clicked that button.

How do I prevent the increment of the integer being injected upon refreshing the page?

Or if there's a simpler method doing all this?

6 Answers 6

1

Using a session is probably your best bet, unless you don't mind that when the user closes the browser and visits the page again it will be triggered.

// This will be set to true if the user has the session variable set
$clicked = isset($_SESSION['clicked']);

// Check if get variable 'click' is set, and that $clicked is false. 
// If 'click is set and $clicked if false, the variable $click is set to the 
// value of $_GET['click'] (for example 'person'). Other wise it will be set to false.
$click = (isset($_GET['click']) && !$clicked) ? $_GET['click'] : false;   

// Set the session variable 'clicked' if it isn't set so that the next time 
// the user visits, we'll know
if (!$clicked) {
    $_SESSION['clicked'] = 1;
}

if($click == 'person'){
    file_put_contents('person.txt', ((int) file_get_contents('person.txt')) + 1);
    include 'resultperson.php';
} elseif ($click == 'text'){
    file_put_contents('text.txt', ((int) file_get_contents('text.txt')) + 1);
    include 'resulttext.php';
} elseif ($click == 'online'){
    file_put_contents('online.txt', ((int) file_get_contents('online.txt')) + 1);
    include 'resultonline.php';
} else {
    include 'resulttext.php';
}
5
  • This didn't worked out for me. Can you explain what's going on at $click = (isset($_GET['click']) && !$clicked) ? $_GET['click'] : false; especially at ? $_GET['click'] : false
    – Randize
    Sep 15, 2012 at 15:52
  • Updated the code with comments, hope it helps. Maybe I'm missing how your logic is supposed to work. Sep 15, 2012 at 16:07
  • Pardon me it worked, I just forgot to start the session. I'm still new at this, you mind explaining the code?
    – Randize
    Sep 15, 2012 at 16:30
  • Ok thanks! Seems I'm just a few seconds behind your last reply haha Great answer @nerdklers
    – Randize
    Sep 15, 2012 at 16:32
  • Oh sorry, I forgot about that :) Isn't the comments sufficient? Writing $variable = (condition) ? true : false is the same as if (condition) { $variable = true; } else { $variable = false; } Sep 15, 2012 at 16:33
0

If you want to track where the user has come from, you can use the referer field of the http header.

If that's not an option, you could have your click link to a php page that registers the click, and then do a header("Location: x.php"); to the page that displays the content.

1
  • Certainly a viable solution. Thanks.
    – Randize
    Sep 15, 2012 at 15:54
0

With your actual code, you can't prevent the increment of the integer on a page refresh.

The question is: does it matter? How important and accurate you want your tracker to be?

There is a way to attach the increment to the button click, which is to use AJAX. You can attach a handler to the button click event, which makes the appropriate AJAX call to the PHP script.

This is not simpler, but it is simple.

Both this solution and your actual one can't prevent a malicious user to make HTTP requests to call your PHP script, incrementing your counter at will...

0

This should work, and is simpler, and more easily extendable:

<?php
if (isset($_GET['click']) && isset($_SERVER["HTTP_REFERER"])) {
    $click = $_GET['click'];
    if (in_array($click, array('person', 'text', 'online')) {
      file_put_contents($click . '.txt', ((int) file_get_contents($click . '.txt')) + 1);
      include 'result' . $click . '.php';
    }
} else {
    include 'resulttext.php';
} 
?>
1
  • Much cleaner and simpler, hey still learning here so this is great thanks. However the $_SERVER["HTTP_REFERER" variable is not going to stop the counter increment upon page refresh. Let me know in case you can think of other variables I could use.
    – Randize
    Sep 15, 2012 at 16:04
0
  1. You could use an ajax-call to trigger that operation. This way the url gets called "behind the scenes" without reloading the content. I recommend this option; using JQuery ajax it can be as simple as writing a onClick function like function clicked(){$.ajax("yourClickCounter.php");} and then adding <button ... onClick="clicked()">...</button> to the html. Of course, you would then have to have the clickCounting done in a separate PHP-file, but it has the advantage of not reloading all the content every time the button is clicked.

  2. You could use a session variable like $_SESSION['has_clicked']. This requires that you start a session first, with session_start()

0

To diferentiate when a user visits the page from your previous page, rather than hitting refresh, look at the $_SERVER["HTTP_REFERER"] variable. That variable will contain the url of the previous page from when the user came, but in the case of a refresh, that variable will not exist as the user hasn't come from any page (he didn't click a link to get to your page) instead was the browser that sent the request.

If your previous page is called 'mypage.php' for this matter, then replace your following line:

if (isset($_GET['click'])) {

for this one:

if (isset($_GET['click']) && isset($_SERVER["HTTP_REFERER"]) && strpos($_SERVER["HTTP_REFERER"],'mypage.php')) {

that should do what you want.

Update:

Above solution will not work as when hitting Refresh on browser the page will maintain the same Referer value.

1
  • That's a brilliant use of $_SERVER["HTTP_REFERER"] I wanted this to work, but the variable doesn't change upon refresh, it echoed the previous page still.
    – Randize
    Sep 15, 2012 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.