Jump to content

[SOLVED] Quick Question (count)


LemonInflux

Recommended Posts

Right then, at the top of this page, I have the following:

 

<?php
error_reporting(0);
include('auth.inc.php');

$file = file_get_contents('../counter.php');
$visitors = explode('\n', $file);

$num_visitors = count($visitors);

$file2 = file_get_contents('../counter2.php');
?>

 

auth.inc.php is the user authentication code.

 

in counter.php, is the following

 

<?php die('You may not view this file.');
12.34.567.890|0 // the 12.34.567.890 represents an IP, the 0 is the number of times the user has visited the site.
12.34.567.890|0 // the 12.34.567.890 represents an IP, the 0 is the number of times the user has visited the site.
12.34.567.890|0 // the 12.34.567.890 represents an IP, the 0 is the number of times the user has visited the site.

 

Basically, the 0 increments when a user with a logged IP visits the site. Anyways, the problem is that I need to count these lines (separated by \n).

Link to comment
Share on other sites

You use count() to count the number of elements in an array.  The problem you are facing likely deals with the way different OSes deal with newlines.

 

*nix : \n

Windows : \r\n (or \n\r, I forget the exact order)

Mac : \r

 

mgall hinted at this earlier but didn't explain in any detail.

 

If you want to explode on the newline character in a file created by any OS:

<?php
  $contents = file_get_contents("the/file");
  $contents = str_replace("\r", "\n", $contents);
  $contents = str_replace("\n\n", "\n", $contents);
  $lines = explode("\n", $contents);
  echo count($lines);
?>

 

You could do it even more simply:

<?php
  $lines = file("the/file");
  echo count($lines);
?>

Link to comment
Share on other sites

Is there a way of keeping them, and it still working? Like, make the code ignore blank lines and <?php die(); ?> lines?

 

Since the very first line of your counter.php file is a die statement, I have to assume you don't mean for it to ever be viewed or included by another script.  You could try and do something with regexps or complicated text parsing to remove the PHP from the file, but why bother?

 

Just take the PHP out of the file and place it in a non-web accessible directory.  You aren't planning to include() it and it won't be accessible via the browser.

 

Alternatively you could give the file a non PHP extension, something like .nv (no-view) and set up an .htaccess rule to not allow any files ending with that extension.

 

I have to wonder why you're not using a DB to store this information as well.

 

You're making a mountain out of a mole-hill IMO.

Link to comment
Share on other sites

Unless your client is on a really, and I mean really, tight budget, they should look into upgrading their host.  You can get a hosting plan that has at least one database for as little as $3 / month AFAIK.

 

The amount of money they'll spend just paying you to write code to piss around with flat-file storage would probably cover the hosting cost for 6 to 12 months.

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.