Jump to content

Recommended Posts

hello all.....

 

I need help getting my php script to just recording clicks to recording they to certain variables during certain times of year. I need to record clicks from Jan-Mar, Apr-june, July-Sept, and Oct-Dec.

 

Here is the script I currently have that counts the times its accessed, increments the value in a text file then redirects to the clicked link.

 

<?php
  $file = 'someTextFile.txt'; 
    $data = @file($file);
    $data = $data[0];
  if($handle = @fopen($file, 'w')){
    $data = intval($data); $data++;
  fwrite($handle, $data);
  fclose($handle);
}  

// actual target location
  header('Location: http://www.somewhere.com');
?>

 

Please any positive input would be greatly appreciated as I am new to writing php scripts

You will be much better off learning SQL and using a MySQL database for handling this. A flat file will be overly slow to parse and if you are trying to read back statistics another person cannot actively write to that file while it is open.

 

After you stopped wanted to just track "clicks" is about when you need to goto a MySQL DB.

@premiso- not being able to write while its open is not a problem. the site does not get high volume of traffic. also please read on 

 

@ram4nd - it may be but its not what the site owner wants, she currently has a link to the text file which she sees when she wants, so I am stuck  just modifying the currently implemented method

Well why not just save the text file by month using date to get the current month. On a new month it writes to a new text file.

 

Then to get the stats you just have to open the 3 months and count that way. Simple and easy.

$months = array('jan-mar' => array('January', 'February', 'March'), 'apr-jun' => array('April', 'May', 'June'), 'jul-sep' => array('July', 'August', 'September'), 'oct-dec' => array('October', 'November', 'December'));

$currentMonth = date('F');

foreach ($months as $file => $month) {
    if (in_array($currentMonth, $month)) {
        $fileName = $file . ".txt";
        break;
    }
}

// file code below

 

Not the best way, but one way to do it.

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.