/    Sign up×
Community /Pin to ProfileBookmark

count($array);

on this page i mentioned i had header errors trying to send cookie
[url]https://webdeveloper.com/forum/showthread.php?t=98565[/url]

to solve this i did as suggested and used ip.
i am using a txt file to do store the ip and the time of access.
this is then seperated into an array using split and |
this means i have to sets of data in the array
200603111453 (eg. YYYYMMDDHHMM) and ip
so what i do is use a for loop to search through this
odd numbers being ip and even being time
i do this by adding an extra $i++ under for.

the script is checked to see if ip is the same as current ip and whether the time in the file is more than 5 mins ago.(if more than 5 mins ago unset array[$i]; unset[$array[$i+1]; +1 being the corresponding ip address)

at the end of this the array is imploded…
and the number of users online is printed…this being the valiue of (count($array)/2)

with the contents of file.txt being |YYYYMMDDHHmm|127.0.01 only this is showing as 1.5 not 1 !!! if i add an extra set to this it is at 2.5 (im guessing this will go to 3.5 and 4.5 but can’t garuntee as i only have 2 computers) where is extra .5 coming from???

should i just go -.5 to the value or a round() function or something?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsMar 11.2006 — There's probably an empty line or something. Try using [b]var_dump()[/b] on your array and see what that turns out.
Copy linkTweet thisAlerts:
@bokehMar 11.2006 — on this page i mentioned i had header errors trying to send cookie

https://webdeveloper.com/forum/showthread.php?t=98565

to solve this i did as suggested and used ip.

i am using a txt file to do store the ip and the time of access.

this is then seperated into an array using split and |

this means i have to sets of data in the array

200603111453 (eg. YYYYMMDDHHMM) and ip

so what i do is use a for loop to search through this

odd numbers being ip and even being time

i do this by adding an extra $i++ under for.

the script is checked to see if ip is the same as current ip and whether the time in the file is more than 5 mins ago.(if more than 5 mins ago unset array[$i]; unset[$array[$i+1]; +1 being the corresponding ip address)

at the end of this the array is imploded...

and the number of users online is printed...this being the valiue of (count($array)/2)

with the contents of file.txt being |YYYYMMDDHHmm|127.0.01 only this is showing as 1.5 not 1 !!! if i add an extra set to this it is at 2.5 (im guessing this will go to 3.5 and 4.5 but can't garuntee as i only have 2 computers) where is extra .5 coming from???

should i just go -.5 to the value or a round() function or something?[/QUOTE]

You seem to be really making a meal of this. Also try to avoid writing "[I]story[/I]" questions. If you want a script to count the online users and won't use MySQL, the following should be fine. No cookies or sessions which you where trying to use to start with. Just put it in a file and include it on each of your pages (or in your template) where you want the user count to appear.[code=php]<?php

function number_online($duration = 5 /* minutes*/)
{
$records_file = $_SERVER['DOCUMENT_ROOT'].'/currently_viewing.php';
$now = time();
if(!@include($records_file)) $records = array();
foreach(array_keys($records, $_SERVER['REMOTE_ADDR']) as $key)
{
unset($records[$key]);
}
foreach($records as $time => $IP)
{
if($time < ($now - ($duration * 60)))
{
unset($records[$time]);
}
}
$records[$now] = $_SERVER['REMOTE_ADDR'];
if($fp = fopen($records_file, 'w+'))
{
fwrite($fp,'<?php $records = unserialize(''.serialize($records).''); ?>');
fclose($fp);
}
return count($records);
}

$viewers = number_online($records);

echo 'There '.(($viewers !== 1)?'are':'is').' currently '.$viewers.' user'.(($viewers !== 1)?'s':NULL).' online.';

?>[/code]
Copy linkTweet thisAlerts:
@72newbieMar 12.2006 — Ok, not to hijack but i have a question.

If i wanted to see who was on index.php

would I change currently_viewing.php to index.php on the code you left

and then add that to the index.php page?


or upload it as say "currently_viewing.php"

and place this on the index:
[CODE]<? include ("/currently_viewing.php"); ?> [/CODE]
Copy linkTweet thisAlerts:
@bokehMar 12.2006 — Ok, not to hijack but i have a question.

If i wanted to see who was on index.php

would I change currently_viewing.php to index.php on the code you left

and then add that to the index.php page?


or upload it as say "currently_viewing.php"

and place this on the index:
[CODE]<? include ("/currently_viewing.php"); ?> [/CODE][/QUOTE]
Neither! [I]curently_viewing.php[/I] was the name I gave to the records file. Save the file with it's own unique name. Not [I]currently_viewing.php[/I] or [I]index.php[/I]. Include that file in [I]index.php[/I] where you want the number of users to appear.
Copy linkTweet thisAlerts:
@72newbieMar 12.2006 — Thanks Bokeh! ?
×

Success!

Help @frup spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...