LemonInflux Posted September 24, 2007 Share Posted September 24, 2007 $visitor = explode('\n', $visitors); How do I count the number of ($visitor)'s there are? Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/ Share on other sites More sharing options...
pocobueno1388 Posted September 24, 2007 Share Posted September 24, 2007 $num_visitors = count($visitor); echo $num_visitors; Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354351 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 Didn't work. Is returning 1, not 4. I tried that. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354353 Share on other sites More sharing options...
pocobueno1388 Posted September 24, 2007 Share Posted September 24, 2007 You need to post more of your code then, and maybe explain what the $visitor variable holds. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354356 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 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). Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354360 Share on other sites More sharing options...
marcus Posted September 24, 2007 Share Posted September 24, 2007 Try \n\r or \r\n Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354364 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 Tried it, as expected, didn't work. They're separated by \n, so I guess \n is all that'll work. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354369 Share on other sites More sharing options...
pocobueno1388 Posted September 24, 2007 Share Posted September 24, 2007 To see what your visitors variable actually holds, do this: print_r($visitors); The reason your only getting the value '1' is probably because there is only one value in the array. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354370 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 weird. Array ( [0] => **.***.**.***|25 ***.**.*.***|5 ). They should have been exploded into: **.***.**.***|25 and ***.**.*.***|5 Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354375 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 Can't see an error in the data storage file: <?php die('You are not allowed to view this page'); ?> *ip*|25 *ip*|5 Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354377 Share on other sites More sharing options...
marcus Posted September 24, 2007 Share Posted September 24, 2007 i like lemonade <?php $file = file_get_contents('lemonade_file.txt'); $omg_boom = explode("\n",$file); foreach($omg_boom AS $boom){ echo $boom . "<br />\n"; } ?> [tested:works] Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354381 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 I know how to echo each 'boom', I just don't know how to count them. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354383 Share on other sites More sharing options...
marcus Posted September 24, 2007 Share Posted September 24, 2007 <?php $file = file_get_contents('lemonade_file.txt'); $omg_boom = explode("\n",$file); echo count($omg_boom); ?> [tested:works] Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354386 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 The only problem with this code, is because my page is in .php, this code includes the die() line, and the return line. Is there a way of keeping them, and it still working? Like, make the code ignore blank lines and <?php die(); ?> lines? Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354391 Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354395 Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354401 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 to roopurt: This isn't for personal experience, this is for a client. His website host doesn't support SQL or .htaccess. So, they're both ruled out. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354409 Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 So place the file in a directory that can't be accessed by a browser. Or when you upload files via FTP does it automatically stick them in public_html? Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354416 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 auto into public. EDIT: Wait, I have a solution. All I did was re-arrange the write to write: newline ip | hits Then the script counts the lines and takes 1. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354418 Share on other sites More sharing options...
roopurt18 Posted September 24, 2007 Share Posted September 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70535-solved-quick-question-count/#findComment-354423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.