Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6945169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:24:05+00:00 2026-05-27T13:24:05+00:00

I am a total PHP novice and am trying to write what I think

  • 0

I am a total PHP novice and am trying to write what I think is a pretty simple script. This is the code I have so far:

HTML / PHP

<?php

$hits = file_get_contents('hits.txt');
 ++$hits;
file_put_contents('hits.txt', $hits);
echo $hits;

$url = $_GET['w'];
?>

<iframe src="<?php echo $url; ?>"></iframe>

<p>
<?php echo $hits; ?>
</p>

The result is a page with an iframe and a hit counter.

The problem with this script is that if the variable $url changes, the hit counter does not. My goal would be that if I visited http://www.website.com/index.php?w=blue.html I would get a different counter than if I visited http://www.website.com/index.php?w=yellow.html.

EDIT: I should add that this script is designed to accept any URL. I realize this complicates things significantly. My ultimate goal would be that if the counter didn’t already exist for that particular URL, it would be generated on the fly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:24:06+00:00Added an answer on May 27, 2026 at 1:24 pm

    Your current code saves the hit points for every page to the same file as a simple string.

    You have a number of options. Here’s one that would work if you prefer to stick with text files instead of databases.

    You could take the URL in, hash it, and save the counter for that page in a hash-named text file.

    Something like

    if( isset($_GET) && !empty($_GET['W']) ){
    $url = md5($_GET['w']);
    $hits = file_get_contents('/hit_counters/'.$url.'.txt');
    $hits++;
    file_put_contents('/hit_counters/'.$url.'.txt', $hits);
    }
    

    and then later you could echo out the hits under that or pull the hits in on another script and echo like that.

    If you need it to create new ones on the fly, you could add something like

    if(!is_file('/hit_counters/'.$url.'.txt')){
    $fh= fopen('/hit_counters/'.$url.'.txt', 'w');
    fwrite($fh, '1');
    fclose($fh);
    }
    

    NOTE
    This could end up creating a ton of tiny text files, though. So be aware. If you are worried about that, you would really need to look into a database or read in a text file line by line to find the same hash.

    TO IMPLEMENT
    Replace the top part of your code within the <?php ?> with the following:

    if( isset($_GET) && !empty($_GET['W']) ){
        $url = md5($_GET['w']);
        if(!is_file('/hit_counters/'.$url.'.txt')){
            $fh= fopen('/hit_counters/'.$url.'.txt', 'w');
            fwrite($fh, '1');
            fclose($fh);
        }else{
            $hits = file_get_contents('/hit_counters/'.$url.'.txt');
            $hits++;
            file_put_contents('/hit_counters/'.$url.'.txt', $hits);
        }
    }
    

    This will take the page name in, hash it, check to see if /hit_counters/THEHASH.txt exists, and create it if not or add +1 to it otherwise. A hash is sort of like encryption, but not really. It will change your $_get[‘w’] into a longer random-looking string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this simple PHP loop that is generating html table data with
I'm trying to write a PHP script that gets my total unread count from
i need help with disk_total_space function.. i have this on my code <?php $sql=select
I have a simple PHP script with a form that has two select fields,
I'm a total newbie at PHP, but this seems so simple, there doesn't seem
Given this PHP code: // total is 71.24 (float) $value = $total * 100;
I have a PHP code: if($billing_total>$limit_to_send){ echo '<script type=text/javascript> window.onload = function() { alert(Sorry,
I have been working on a small php app (400K total). But in the
Total newbie question but this is driving me mad! I'm trying this: myInt =
Im trying to make dynamic column list that will be 4 columns total (PHP).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.