• 0

Good free web counter?


Question

18 answers to this question

Recommended Posts

  • 0
  mikey said:
  BxBoy said:
<?PHP
$counternum = "counter.txt";
$fp = fopen($counternum,rw);
$num = fgets($fp,9999);
fclose($fp);
?>

can i just ask how that increments???

woops, forgot this part:

    $fp = fopen($counternum,w);
     $num += 1;
     fputs($fp, $num);
     fclose($fp);

  • 0

It doesn't look like it increments at all. It just gets the value. Try this:

$file = "counter.txt";
$fp = fopen($file, "r");
$hits = fgets($fp, 999);
fclose($fp);
$hits++;
$fp = fopen($file, "w");
fprintf($fp, "%d\n", $hits);
fclose($fp);
print $hits;

I think that's right, but it's been a long time since I used text files. Who needs text files when you have MySQL...

$query = "UPDATE pages SET hits = hits + 1 WHERE id = $pageId";
$r = mysql_query($query);
$query = "SELECT id,hits FROM hitsTable WHERE id = $siteId";
$r = mysql_query($query);
$hits = mysql_result($r, 0, "hits");
print $hits;

That'll do your job if you have a table of pages with at least an id field and an int field for the number of hits, although you'd want to add error checked too. This is how I implement my hit counters so I have a counter on every page. If I thought about it, I could probably come up with a way to do it in one SQL statement but as I never read the hit number on a page I increment it on (as showing hit counters to visitors is somewhat unprofessional and boastful), I've never had a reason to put any thought into it.

One thing to note about this is that I do the update first, then the select. The obivous way to to select, add one and update, however that can be faulty. Imagine it's a busy site - what might happen is this:

- Visitor 1 arrives and the web server process for him starts (which we'll call V1)

- V1 reads the number of hits for that page, let's say it's 100

- Visitor 2 arrives and the web server process starts for him (which we'll imaginatively call V2)

- V2 reads the number of hits for that page, which would still be 100

- V1 adds 1 to the value, so it's 101

- V2 adds 1 to the value, so it's 101

- V1 updates the database with the new value with the number 101

- V2 updates the database with the new value with the number 101

Voila - 2 hits, but the hit number has only gone up by 1 because V2 read the value before V1 had updated it. By reading,incrementing and updating in the same SQL statement means that the MySQL server will entirely do this for V1 before handling anything by V2, so when V2 is processed, the number is already 101.

This may be a little deep for a thread about free hit counters ... sorry, got started and couldn't stop :D

EDIT: Spent so frickin' long on my reply the orignal question was answered long before I posted. Oh well... :rolleyes:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • That's the publisher's fault and happens with or without GamePass. Microsoft at least offers the Play Anywhere option.
    • Windows Phone actually had some great features, the problem Microsoft had is that developers are not used to having to deal with stores etc. and they couldn't get them onboard. But there were a lot of good features, in fact we see more and more of them popping up on other OS' all the time.
    • Download Microsoft 365 Copilot At Work (worth $60) for free by Steven Parker Claim your complimentary eBook worth $60 for free, before the offer ends on June 25. In Microsoft 365 Copilot At Work: Using AI to Get the Most from Your Business Data and Favorite Apps, a team of software and AI experts delivers a comprehensive guide to unlocking the full potential of Microsoft's groundbreaking AI tool, Copilot. Written for people new to AI, as well as experienced users, this book provides a hands-on roadmap for integrating Copilot into your daily workflow. You'll find the knowledge and strategies you need to maximize your team's productivity and drive success. The authors offer you a unique opportunity to gain a deep understanding of AI fundamentals, including machine learning, large language models, and generative AI versus summative AI. You'll also discover: How Copilot utilizes AI technologies to provide real-time intelligent assistance and revolutionize the way you work with Microsoft 365 apps Practical Implementation Strategies for project and change management, as well as practical guidance on rolling out Copilot within your organization Specific use cases, including Outlook, Teams, Excel, PowerPoint, and OneNote, and how Copilot can streamline tasks and boost efficiency across various Microsoft applications Take your Copilot proficiency to the next level with advanced AI concepts, usage monitoring, and custom development techniques. Delve into Microsoft Framework Accelerator, Copilot plugins, semantic kernels, and custom plugin development, empowering you to tailor Copilot to your organization's unique needs and workflows. Get ready to revolutionize your productivity with Microsoft 365 Copilot! Expires June 25. How to get it Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Free offer expires today, June 11. Download Microsoft 365 Copilot At Work (worth $60) for free Offered by Wiley, view other free resources The below offers are also available for free in exchange for your (work) email: AI and Innovation ($21 Value) FREE – Expires 6/11 Unruly: Fighting Back when Politics, AI, and Law Upend [...] ($18 Value) FREE - Expires 6/17 SQL Essentials For Dummies ($10 Value) FREE – Expires 6/17 Continuous Testing, Quality, Security, and Feedback ($27.99 Value) FREE – Expires 6/18 VideoProc Converter AI v7.5 for FREE (worth $78.90) – Expires 6/18 Macxvideo AI ($39.95 Value) Free for a Limited Time – Expires 6/22 Microsoft 365 Copilot At Work ($60 Value) FREE – Expires 6/25 Natural Language Processing with Python ($39.99 Value) FREE – Expires 6/25 Excel Quick and Easy ($12 Value) FREE – Expires 6/24 The Inclusion Equation: Leveraging Data & AI ($21 Value) FREE – Expires 6/24 The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Check out our partner software in the Neowin Store Buy a T-shirt at Neowin's Threadsquad Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
    • This is my story as well, and I also converted 4 people myself.
  • Recent Achievements

    • First Post
      NeoToad777 earned a badge
      First Post
    • Week One Done
      JoeV earned a badge
      Week One Done
    • One Month Later
      VAT Services in UAE earned a badge
      One Month Later
    • First Post
      LsDmT earned a badge
      First Post
    • Week One Done
      evershinefacilityservice earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      571
    2. 2
      ATLien_0
      247
    3. 3
      +Edouard
      162
    4. 4
      +FloatingFatMan
      151
    5. 5
      Michael Scrip
      113
  • Tell a friend

    Love Neowin? Tell a friend!