Jump to content

Simple CLick tracker...


se1zure

Recommended Posts

HOw woult I go about keeping track of the number of times a link/submit button has been clicked? I want it to be very simple,a nd have the data be stored in a text file.

I can't seemt o get the submit form to add 1 to the count without using a  second php page. (otherwise, simply refreshing the page adds one to the count)

In other words, how can I make it so when I click a button (in a form), it runs a php script, but not until the button is clicked. ALl without leaving that page (refreshign is okay, but leaving the page is not).
Link to comment
Share on other sites

well if there is a form on the page, im not sure how to do that, but just have the link go to".....Page.php?click=yes    after that in the header of your page go something like :

if ($_GET['click] == "yes") {

Write the ++1 to the text file. 
}


DISPLAY PAGE HERE.

if the link was halfway down the page, i dont know hoew i would save the data already6 saved. ( maybe using the field names and the vairbales and sending all the values, even if their empty.... i doint know)

good luck
Link to comment
Share on other sites

Alright. Well, right now, I just want it to add a click count to the thing, (and it sets a cookie, so that they can't just refresht he page).

[code]<?php
if (isset($_COOKIE["user"])){ // if it detects the cookie
    echo "A hit is only added once per hour"; } // Tell me that it has detected the cookie.
else if ($_GET[click] == "yes"){ // If no cookie is detected, and ?click is yes
              setcookie("user", "VISIT", time()+3600); // Set a cookie
              $filename= "counter.txt" ; // set filename var to counter.txt file.
              $fd = fopen ($filename , "r") or die ("Can't open $filename") ; // open counter.txt in read only format.
              $fstring = fread ($fd , filesize ($filename)) ; //Not %100 sure what this is for, just followed instructions.
              echo "$fstring" ; // Print counter.txt
              fclose($fd) ; // Close the connection with counter .txt

              $fd = fopen ($filename , "w") or die ("Can't open $filename") ; // reopen counter.txt in write format
              $fcounted = $fstring + 1 ; // Set $fcounted var to add one to $fstring.
              $fout= fwrite ($fd , $fcounted ) ; // write to $fd connection the new number $fcounted
              fclose($fd) } // close connection.
else { // if neither of the if statments are met.
    echo "<a href="?click=yes">Visit?</a>"; // display link to visit plage.
}
 

?>

[/code]

But this code outputs the error:
[code]Parse error: syntax error, unexpected '}' in /home2/data/hosted/scetchbook/php/diggtest/story.php on line 15[/code]

Which doesn't make sense... I am just closing the if else stament.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.