Jump to content

Count times link is accessed?


Graxeon

Recommended Posts

I have a few PHP scripts that do basically the same thing but for different content.

 

So I have these php files:

 

a.php
b.php
c.php
etc

 

And they have different inputs:

 

a.php?a=abstract
a.php?a=abs
a.php?a=attitude
b.php?a=bob
b.php?a=bat
etc

 

Now I want to create a log file that counts how many times each of those "?a=___" inputs are accessed.

 

So it would be something like:

 

$log = $_GET['a']
logfileto('http://site.com/log.php?a=' .$log) //Not sure what this code would be, just giving an example.

//rest of my script here.

 

And then log.php would be something like:

 

$logged = $_GET['a']
writetolog($logged +1) //The +1 would refer to the "a.php?a=___" being accessed and adding on to the original count for that input. Again, not sure how this code would be.

//Then tell the script to add this input if it hasn't been created already and add 1 to it. If it is created, then just add 1 to it. Idk how this code would be written.

 

 

Now I've done something similar to this but it's not for keeping a log. Here's what I've done where I can write to a file:

 

$time = $_SERVER['REQUEST_TIME'];
$handle = fopen('lists/' . $time . 'file.xml', 'w');
fwrite($handle, $xml);
fclose($handle);

header('Location: lists/'. $time . 'file.xml');

 

So I'm guessing the code should be something similar to this.

 

 

Can someone help me with this? I'm not too great at PHP so I don't know all of the different functions that can pull this off xD

 

Edit:

 

Oh, and to view the number of times the ".php?a=___" has been accessed, then something like this would be done:

 

$count = $_GET['ca'] //Where "ca" would be like "log.php?ca=bob"
//And if "bob" was accessed 37 times then it would output:

echo $timesaccessed($count)

Link to comment
https://forums.phpfreaks.com/topic/184581-count-times-link-is-accessed/
Share on other sites

do you want to write the info to a text file or to a MySQL database? A database could potentially be a little easier when managing, outputting and counting the logs. You have to consider that the larger the log text file gets, the longer it will take to process the data. Same goes for a MySQL db, but a db is much more efficient than a txt file.

 

If you use a MySQL database, you can log the date/time, the value ("abstract", "attitude", etc), and maybe even the users IP address, then calling back the information would be significantly easier. For getting the count of each value, you can do a mysql select statement like:

 

SELECT COUNT(*) as num FROM TABLE_NAME WHERE value="abstract"

 

and that will return a variable called num that contains the number of times "abstract" was clicked. And if you want to output the entire log, you can do another select statement like:

 

SELECT * FROM TABLE_NAME

 

which will return all the data from the table and you can create a for loop to output the data to screen. A far as writing the data, you would just use an INSERT mysql statement.

well first thing's first, the reason your log writing code isn't work is because you should use "a" as the last parameter, not "w".

 

'w' - empties the file if it exists are starts writing.

'a' - appends data to the end of the file if it exists; if it doesn't exists, it creates the file and writes the data to the beginning.

 

See the documentation here: http://php.net/manual/en/function.fopen.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.