Click to See Complete Forum and Search --> : How many users on page
I need to know how I can setup a php script that would display "There are currently ## users on this site" on my main page of my site and it would be accurate. I was told you need to make it so the user registers but I need to know if thier is an easyer way and if so how and if not how do I do the user registration thing? Any help would be greatly appreciated! Thanks. :D
Nevermore
05-31-2003, 04:32 PM
You could do it with a Java applet and server side script, with JavaScript and Server-side script (although it wouldn't be very accurate), by time using PHP (ask me to explain later, I'm tired, it's bout 10:30 and I've had quite a lot to drink), or by logins. Tomorrow I will make you a script combining JavaScript and timing to do it roughly. I look forward to working with you again.
Nevermore
06-01-2003, 03:31 AM
If you want to do it via login + register, that would be better, and I can help you to do that...?
Nevermore
06-01-2003, 05:36 AM
Here's a script tht will calculate the number of users on a page at any one time, roughly. (I say roughly because people without JavaScript will never be detected as leaving). Without using login + register I think it's the best you can do. This is the bit where Pyro proves me wrong...
visitors.php:
<?php
$fh=fopen("online.txt","r+");
$read=fgets($fh,12);
setType($read,"integer");
if($_GET['minus']) $read--; else $read++;
rewind($fh);
fwrite($fh,$read);
echo("There are currently $read users online");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Visitor counter</title>
<?php if(!$minus) echo(' <script type="text/javascript">
<!--
function leave() {
notify=window.open("visitors.php?minus=yes");
window.close(notify);
}
// -->
</script>');?>
</head>
<body <?php if(!minus)echo('onunload="leave()"'); else echo('onload="window.close()');?> >
</body>
</html>
online.txt:
0
Just add your content to the framework provided by visitors.php. Need any more help, just post.
Thanks for your response. I was wundering if you could possibly post the files or the link to an example so I could see it in action and see if I like it? I would do it but I usally screw it up pretty bad and I want a working example. You could post both of you ideas the "Non Registration + Login" and the "Login + Registration" one so I could see which one I will use. Thank you so much for your help. I may not respond right away as I'll probably be gone all day so I'll try to view your response as soon as possible. Thanks again for your help. :D
PeOfEo
06-01-2003, 04:14 PM
lol I am inturding on the sacred ground of the php forum and I myself do not use php. Anyways to do a login/register you would need login forms and reistration forms and what makes you thing people are going to login if there is nothing for them you see where it says people browsing this forum thats login and registration but because we need to login to post it forces us to login which makes that method accurate but notice it only tells you who is loggedin not who is browsing and it does this by adding your name to a data base then removing it onunload or whatever the php equivalent to that is. It is the same in all server side llanguages the principle atleast not the code.
Well I was kinda planning to have a login/registation thing any way since I plan to have a chat room and forum and stuff on my site that I will only want registered users to have access to! I think that all I will worrie about is the amount of users logged in so people will know when someones on the forum or chat room or what not! I also want to be able for them to be greeted with their username when they log in on the main page and if they are not logged in it will say guest!
PeOfEo
06-01-2003, 05:06 PM
well the greet username is not as hard as the currently logged in junk all you have to is grab their session username or whatever the feild in your data base is for user name.
Originally posted by cijori
Without using login + register I think it's the best you can do. This is the bit where Pyro proves me wrong...
What I would do is just check if the users are active. This will count the number of active users in 15 minutes:
You need a file named data/online.txt, and don't forget to CHMOD it so it is writeable (666)
<?
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();
$minutes = 15;
$found = 0;
$users = 0;
$user = "";
$tmpdata = $SERVER['DOCUMENT_ROOT']."/online/data";
if (!is_file("$tmpdata/online.txt"))
{
$s = fopen("$tmpdata/online.txt","w");
fclose($s);
chmod("$tmpdata/online.txt",0666);
}
$f = fopen("$tmpdata/online.txt","r+");
flock($f,2);
while (!feof($f))
{
$user[] = chop(fgets($f,65536));
}
fseek($f,0,SEEK_SET);
ftruncate($f,0);
foreach ($user as $line)
{
list($savedip,$savedtime) = split("\\|",$line);
if ($savedip == $ip) {$savedtime = $time;$found = 1;}
if ($time < $savedtime + ($minutes * 60))
{
fputs($f,"$savedip|$savedtime\n");
$users = $users + 1;
}
}
if ($found == 0)
{
fputs($f,"$ip|$time\n");
$users = $users + 1;
}
fclose ($f);
print "<font style='color:white;font-size:10pt'>Visiters Online : $users</font>";
?>
PeOfEo
06-02-2003, 09:30 PM
god your code is confusuing when you dont know php. look like java script on shrooms.
Nevermore
06-03-2003, 11:25 AM
The hard part is remembering how to use loads of functions- there are literally hundreds. Guess what Pyro can do that I can't! Hopefully that will come with experience. Pyro, how long have you been using PHP?
I've been using PHP for around 6 months or so... As far as remembering functions, php.net is your best friend... :)
scriptkid
06-05-2003, 04:03 PM
There is a way to count active sessions I do believe...brb with that answer
scriptkid
06-05-2003, 04:20 PM
Well I ddint find anything in the 5 minutes I searched, but ther are hints that it is possible. You would have to access the global $_SESSION of course. If nothing else, set up a database table and log users from your main page by either there ip address or their GUID and check it on each page they go to also log their sessid and then on sessid_timeout have it remove it
sorry im in a hurry to get home and play counterstrike lol just got off work
Thank you guys. You all had great answers for my question. Pyro, thanks for your response! I think your idea will work for what I'm trying to accomplish but being such a newb to php that you know I am I need a little more explination on how to impliment this idea if you have time. From what I gather I need to make a php file with the php code you gave me and make a log.txt or something but I need more explination on what goes in the txt file and where I put the php file and the txt file and how I would set up my main html file to display "Currently Active Users". I would greatly appreciate any more help you can provide. Thanks so much for your help and hope to see a response from you soon! Also thanks again to all that responded. :D
This line:
$tmpdata = $SERVER['DOCUMENT_ROOT']."/online/data";points to your .txt file. So, if you file is at http://www.yourdomain.com/online/data/online.txt you would use the above line. I personally made a directory named online and threw the php code that I posted above in that. Then, inside that directory, I made a new directory named data, and inside that, I put the online.txt file. Please note that the online.txt file must be CHMODed to 666 to give the script permission to write to it.
Then, what you will do is put a PHP include on your pages that you want to log/display users. This would look something like this (assumbing that you named the PHP file online.php)
<?PHP
include ("path/to/online.php");
?>
Ohh thank you so much that helps so much. I actually understand it lol. I have just one more question now, what do I put on the page that I want to display the "currently active users"? Does the page have to be php and if so what php code would I put on that page to display something like "There are currently ## active users browsing this site" or something along those lines? I finally understand the rest of what you were telling me but I now just need to know what to put on the page to display the active user info? Thanks for all your help so far! :D
I said how in my last post:
Originally posted by pyro
Then, what you will do is put a PHP include on your pages that you want to log/display users. This would look something like this (assumbing that you named the PHP file online.php)
<?PHP
include ("path/to/online.php");
?>
Ohh... I'm so stupid some times. Thanks for clearing that up for me. Now from what I can make out the way you told me to it will just display the amount of users at the top of the page then so is there any way to configure the php include so that I can have "Currently Active Users" displayed on a certain part of the page and in my own font and what not! Also is there any info I need to put in the TXT file like numbers or something or will the php automaticly insert the info I need? Thanks for your help! :D
You just include the PHP where you want the text to show up. To change the way the text looks, change this:
print "<font style='color:white;font-size:10pt'>Visiters Online : $users</font>";
And, no, you don't insert anything into the .txt file. The PHP script will do it. Just make sure it is CHMODed to 666.
I did it! I actually figured it out thanks to your help. Now all I need to do is give it a shot and see if it'll work like I hope it will. I'm sure it will work though since your such a php guieneuss and all. Thanks for all your help and I'll post if I come up with any problems. Thanks again Pyro. :)
I set it up on my account on your server and I get this error when I go to the index file to try to view the currently active users:
Warning: split() [function.split]: REG_EMPTY in /*this is your server here but did not put for security reasons*/online/online.php on line 33
I chmod the txt file to 666 and did what you said but I get that error. Can you help me out here?
You still there? Look at my last post and see if you can help me out here. Thanks. :D
Mike, there were a few syntax errors in the code. I've fixed that now. (http://members.lycos.co.uk/deton/online.php)
Jona ;)
Pyro,
You made the code so it checks for new people online every 15 minutes, but I was wundering how I could change the code so it does it every like 40 seconds or so? Jona help me work some stuff out but I still get two errors! You can see what I'm talking about by going to: http://kd7pyo.infinitypages.com/online.php. So see what you can figure out and let me know the answers to my questions as soon as possible. Thanks. :D
Looks like you have an empty regexp on line 31.
If you want to change it from minutes to seconds, change this:
$minutes = 15; #line 6 in the original code
to
$seconds = 40;
and this:
if ($time < $savedtime + ($minutes * 60)) #line 35 in the original code.
to
if ($time < $savedtime + ($seconds))
Note: I edited my original code, to account for a formatting error from these forums.
I got it all working without any errors. I had two different computers connected to the internet with two different IP addresses on the same page but it dosn't ever show 2 active users? It always just shows 1. I think there might be something wrong with the updating thing. Can you take a look at http://kd7pyo.infinitypages.com/online.php and see if you can figure out whats wrong. Every thing else works fine now its just the amount of users thing? Its always 1 and never changes when it should? Thanks for your help. :D
I rewrote it... the other one was some older code. Hopefully this one will work better for you.
<?PHP
$file = "online.txt"; #chmod to 666
$minutes = "10"; # number of minutes until users are no longer online
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();
$data = "";
$users = "0";
$found = "0";
$lines = @file($file);
$contents = @split("\\|", $lines[0]); #split the first line of the file
if (count($lines) == 0) { #if file is empty
$x = 0;
}
else { #if there is already data in the file
$x = 1;
}
$fp = fopen($file, "w");
if ($x == 1) {
foreach ($contents as $content) { #loop through the users
list ($savedip,$savedtime) = split(":", $content); #list the users ip's and last time on page
if ($ip == $savedip) { #user already online, update time
$data .= "$ip:$time|";
$users++;
$found = "1";
}
else {
if ($time < $savedtime + ($minutes * 60)) { #other users online, don't update time
$data .= "$savedip:$savedtime|";
$users++;
}
}
}
}
if ($found == 0 or $x == 0) { #first time this user came online
$data .= "$ip:$time|";
$users++;
}
$data = substr($data, 0, -1); #cut off the trailing |
fwrite ($fp, $data); #write the file
fclose ($fp);
echo "<font style='color:black;font-family:verdana,arial,sans-serif;font-size:10pt'>Visitors Online: $users</font>";
?>
Ok, I got it all working now but now I need something from you or any body else on the forums. I need to add coding to the online.php file so it will log the users Date/Time they entered the page along with the IP. Right now it logs the users IP address when they enter any of the pages with the php include. I now want it to put a timestamp with it so it would look something like this: (127.0.0.1 | 1:00PM | 06/17/03). Please post the origional code plus the added elements if you answer my call for help!
Untested code:
<?PHP
$file = "online.txt"; #chmod to 666
$logfile = "log.txt"; #chmod to 666 -- used for log file
$minutes = "10"; # number of minutes until users are no longer online
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();
$data = "";
$users = "0";
$found = "0";
$lines = @file($file);
$contents = @split("\\|", $lines[0]); #split the first line of the file
if (count($lines) == 0) { #if file is empty
$x = 0;
}
else { #if there is already data in the file
$x = 1;
}
$fp = fopen($file, "w");
if ($x == 1) {
foreach ($contents as $content) { #loop through the users
list ($savedip,$savedtime) = split(":", $content); #list the users ip's and last time on page
if ($ip == $savedip) { #user already online, update time
$data .= "$ip:$time|";
$users++;
$found = "1";
}
else {
if ($time < $savedtime + ($minutes * 60)) { #other users online, don't update time
$data .= "$savedip:$savedtime|";
$users++;
}
}
}
}
if ($found == 0 or $x == 0) { #first time this user came online
$data .= "$ip:$time|";
$users++;
}
$data = substr($data, 0, -1); #cut off the trailing |
fwrite ($fp, $data); #write the file
fclose ($fp);
#set up log file data
$logtime = date("h:ia");
$logdate = date("m/d/y");
$logdata = "($ip | $logtime | $logdate)\n";
#write log file
$fp = fopen($logfile, "a");
fwrite ($fp, $logdata);
fclose ($fp);
echo "<font style='color:black;font-family:verdana,arial,sans-serif;font-size:10pt'>Visitors Online: $users</font>";
?>
brendandonhue
06-17-2003, 05:26 PM
Pyro-you just left out a semicolon
$logfile = "log.txt" #chmod to 666 -- used for log file
shoudl be
$logfile = "log.txt"; #chmod to 666 -- used for log file
Yep, just noticed that, and changed it... :p
Ok, I got it working so it now logs the IP and date but now when ever you refresh the page it adds another timestamp to the log file. I refreshed the page four times and it had four timestamps in the log file so you can just imagen the server bog down because every time someone refreshes the page it adds another timestamp which makes the file way big. :p
I need it so it will only log the user once so it won't overflow the log file. As you know I have a couple pages I put the php include on so it would also add more timestamps every time they went to the next page. So overall I need it to log the user only once nomatter if they refresh the page or go to another page with the php include. I also want it to log them if they come back another day so I could see if I have repeated visitors. Please help. Thanks. :D
I think it might be something to use "sessions" with!
brendandonhue
06-17-2003, 06:20 PM
Sounds like its getting pretty complicated-I suppose it would be possible to check a flatfile for an IP and just change the timestamp if they are there-but this is getting into a databases territory.
Surely Pyro or another expert can help you do what you want though :D
Originally posted by mjdimick
I think it might be something to use "sessions" with!
Sessions only work for a certain amount of time. To do what you want, you could do one of two things: use a database to store the information (best way), or use the flatfile as a database and search through it every time the file is included, loaded or reloaded. Of course, by "search through" I mean search to find the IP address, if it doesn't exist append the IP address, date and timestamp to the file; if it does exist, search to see the latest date that IP address occurs in the file--you can do this by searching in the file on a single line after you find the same IP address.
I think that by pointing out all that needs to be done, you could figure out some of it. ;)
Jona
Here's a third way, and is probably the easiest. Just set a cookie that last however long you want it not to count them, and then check if the cookie is set with something like this:
if (!isset($_COOKIE["cookiename"])) {
#do something (*hint* write the log file)
}
*hint on setting a cookie*
setcookie ("cookiename", "visited", time() +"1200"); #set for 20 minutes (time is in seconds)
Does anybody know where in the file and what file to put this peice of code pyro gave me? Do I put it in the online.php file? Is there any thing else that needs to be added to make this idea work? Please help. Thanks for all your guyeses help sofar. :D
I pretty much told you in my post above:
Originally posted by pyro
(*hint* write the log file)if (!isset($_COOKIE["cookiename"])) { #if cookie is not set
#write log file
$fp = fopen($logfile, "a");
fwrite ($fp, $logdata);
fclose ($fp);
setcookie ("cookiename", "visited", time() +"1200"); #set for 20 minutes (time is in seconds)
}
I got it all working now! But now I want to add something. I want it to say (There have been ## users on today) so it will be like this:
(Currently Active Users) = Users on right at that moment. (I already have this part done.)
and
(Users On Today) = Users that have been on all day. (I need help with this part.)
I think all you would have to do is make it look at the log.txt file and it will determin the amount of lines in the file and then it will post that number along with my other users online number to the main page. It would display 0 if there are no lines in the log file. It would reset the log file every night at 12:00PM so you can get a count for each day and not all the days. I got the idea I think but I just don't know how to impliment it. I would also like if I could add it to my origional php file so I only have one file to deal with. Please help me out someone. Thanks. :D
Here's how I would do it:
Use a new file, and basically make it a counter. Then set up a cron to run once a day to delete all the stuff in the file. This will make it so each day, it will contain the number of hits for that day...
Another option:
Set the date to the file, and then check if the date in the file is the same as today's date. If it is, add to the counter, if not, clear the file, then add the date, and reset the counter.
You mean a "most users online ever" script?
Jona
No I ment a script that would count the hits for that whole day insted of just for who is on right at that moment like the script I already have does!
Ok, I think I understand what your saying kinda but I don't really understand how to impliment it? BTW sorry for being such a pest to you about this topic. :(
You might want to try to learn PHP a bit. Here is the book I used to start learning:
http://www.amazon.com/exec/obidos/tg/detail/-/0201727870/qid=1056132820/sr=1-9/ref=sr_1_9/104-1584805-2347145?v=glance&s=books
And here are some online resources:
http://www.php.net (my personal favorite)
http://www.php.net/manual/en/tutorial.php
http://www.phpbuilder.com/
http://hotwired.lycos.com/webmonkey/programming/php/index.html
http://php.resourceindex.com
more links can be found at http://www.php.net/links.php
I got a idea for how to clear the log.txt file automaticly! What if you made a script that would clear the log file every time there is a certain amount of lines in the file! I need help with this one because I looked at those links you gave me but I have problems with learning coding. I have tried it many times in the past but it just never sinks in! Please help me. Thanks. :D
Untested code, but something real close to this (or, hopefully, this...) will do it. This should write the first 20 lines.
$file = "file.txt"; #file to open/write to
$lines = "20"; #number of lines to read
$data = "";
$content = @file($file);
foreach ($content as $line_num => $line) {
if ($line_num < $lines) {
$data .= $line;
}
}
$fp = fopen($file, "w");
fwrite($fp, $data); #write the file
fclose ($fp);Also, as far as learning goes, it takes more time than just reading a tutorial or two. Start with easy things... echo some text to the screen, etc.
Now would I put that in the online.php file I already have or would I create another file called like clear.php or something?
You would put it in whichever file is currently writing your log file. It will just write the first 20 lines to it...
Ok, here is the page that I'm trying to get this to work on: http://kd7pyo.infinitypages.com/ and here is the text version so you can see the code I have: http://kd7pyo.infinitypages.com/currentfile.txt because I'm getting a line 66 error and I can't figure out what is wrong? Maybe I missed something or put the peice of code you gave me in the wrong place or something. Please see if you can help me here. I had it working before I added the peice of code you gave me but now its not working. All I wanted to do is make it so it will clear the log file if there are more than 30 lines in it because my log file just keeps getting bigger and bigger and bigger and I don't want it to overload the server. I also don't want to have to go in all the time and manuelly remove all the entries in the log.txt file. If pyro or someone could help me out I would greatly appriciate it. Thanks. :D
Add a semi-colon to the end of line 65, just before the comment...
Your a guienius! It works now. But one thing I noticed is what its doing is just not allowing more than 20 entries to the log file. I wanted it so it would toatally clear the log.txt file if there are more than 20 lines in the file so then it would be a clear file for the script to write to! Sorry for the troble. I think I just didn't do a very good job explaining what I wanted. Sorry. :(
Well, maybe this...
foreach ($content as $line_num => $line) {
if ($line_num < $lines) {
$data .= $line;
}
else {
$data = "";
}
}
So you want me to replace the little scriptlet that you gave me with this? In the same spot? Any thing else need to go with that?
No, just replace the old foreach loop with this new one...
Ok, I got it and I don't see any errors. Can you goto: http://kd7pyo.infinitypages.com/ so it will add another line to the log file so I can see if it will clear it? It has 20 lines in it so it should clear when you go there if its working right! The script I have won't reconise me twice so I need you to go there real quick please! Thanks. :D
You could have set you log file so that you were number 21... Anyway, I clicked the link for ya...
It works! Thank you so much for your help. I can't thank you enough. I think its time to retire this post now. I have it all working how I want it to. Thanks! :D :D
Good, glad it's now working as you wish.
You're welcome...
I now know it is possible to get the time from the server with PHP. I think I now have a solution for my clear the log file at 12:00PM problem. This is how you would do it, you would get the time from the server and refresh that time every minute and post that time to a file. So with my theory the file would always have the current server time. You would then have my online.php file look in the file with the server time and when that file has the time of 12:00PM in it the online.php file would clear the log file. I think I have the idea but I just don't know where to get started. If you think/know I'm wrong about this please let me know why you think/know that it won't work. Also if my theory is a little off but on the right track let me know. Please help me out here. Thanks a million. :D
Originally posted by mjdimick
If you think/know I'm wrong about this please let me know why you think/know that it won't work.It will not work, and here's why:
A PHP script has two ways of running.
1. When a users browser sends a request for that page.
2. When a cron sends a request to run the script.
You see, your script will only update the time when a call has been made for the script via one of the methods listed above.
Could you do a clock that is not linked to the server but just has the same time as the server. I don't know how you would do it, but it's a suggestion!
Nope, I can't see anything like that working...
How about this idea. You would have a file that would have some sort of thing where it has all the timezones and you tell the script to put the server in the timezone it is in. When a user comes to the page the script looks at their time and then figures out what timezone the user is in and then looks at the file with the time zones and then ofsets the users time to the timezone the you specifie the server is in and then you would have the server time. Sorry for procuwing this so much I just wanted to know because I thought I saw this done some where once. ;)
It's possible that it's been done, maybe... But, I do not know of a way to get a script to run automatically at a certain time without the use of a cron.
If all you need is the server time, you can get that with the date() (http://us3.php.net/manual/en/function.date.php) function...
Check this out! I have a clock on this page that displays the live server time: http://kd7pyo.infinitypages.com/time/. Here is the code for it:
<?php
// This function is to be used in the <head> section of the page.
function InstallClockHead( )
{
echo "\n";
echo '<script language="JavaScript" type="text/javascript">';
echo "\n<!--\n";
echo 'var monat = new Array("January","February","March","April","May","June","July","August","September","October","November","December");';
echo "\n";
echo 'var digital = new Date( "'.date('M, d Y H:i:s').'");'; // <-- this is the trick! Here comes the server time
echo "\n//-->\n</script>\n";
}
// This function is to be used at the end of the <body> section of the page.
function InstallClockBody( )
{
?>
<script language="JavaScript" type="text/javascript">
<!--
function clock() {
if (!document.layers && !document.all) return;
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var amOrPm = "AM";
if (hours > 11) amOrPm = "PM";
if (hours > 12) hours = hours - 12;
if (hours == 0) hours = 12;
var m;
digital.setSeconds( seconds+1 );
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
m = digital.getMonth();
dispTime = digital.getDate()+". "+monat[ m ]+" "+digital.getFullYear()+" "+hours + ":" + minutes + ":" + seconds; " " + amOrPm;
if (document.layers) {
document.layers.pendule.document.write(dispTime);
document.layers.pendule.document.close();
}
else
if (document.all)
pendule.innerHTML = dispTime;
setTimeout("clock()", 1000);
}
// End of clock -->
</script>
<?php
}
// This is to be used where you want the clock to appear on your page.
function Clock( )
{
// To have it work with NS 4.7 the style "position:absolute" MUST be given (knito)
echo '<span id="pendule" style="position:absolute;">Serverzeit</span>';
}
?>
I have the variable in there to display AM/PM but for some odd reason its not displaying it. Can you check out my code and see if you can figure out why its not displaying AM/PM. Thanks. :D
I told you you could get the server time...Originally posted by pyro
If all you need is the server time, you can get that with the date() (http://us3.php.net/manual/en/function.date.php) function...
I figured out that this:
<?php
echo date("g:i:s A");
?>
Will display (1:45:50 AM) but its not a live updating time like the other one I showed you. Is there a peice of code I could add to make it so this is live?
This post explains how: http://forums.webdeveloper.com/showthread.php?s=&postid=60263#post60263
I modified the script so it would show the seconds in the time. I just copied the same idea as the origional script to display the seconds. I'm getting this error on line 60 which is the line in bold.
<script language="JavaScript">
function calculate() {
var serhours = <?PHP echo date("g") ?>;
var serminutes = <?PHP echo date("i") ?>;
var serseconds = <?PHP echo date("s") ?>;
var Digital=new Date();
var hours=Digital.getHours();
var minutes=Digital.getMinutes();
var seconds=Digital.getSeconds();
if (hours<=9) {
hours="0"+ hours;
}
if (minutes<=9) {
minutes="0"+minutes;
}
if (seconds<=9) {
seconds="0"+seconds;
}
hrsdif = serhours - hours;
mindif = serminutes - minutes;
secdif = serseconds - seconds;
show();
}
function show() {
var Digital=new Date();
var hours=Digital.getHours();
var minutes=Digital.getMinutes();
var seconds=Digital.getSeconds();
var serhrs = Number(hours) + Number(hrsdif);
var sermins = Number(minutes) + Number(mindif);
var sersecs = Number(seconds) + Number(secdif);
if (serhrs < 0) {
serhrs = 00;
}
if (sermins < 0) {
sermins = 00;
}
if (sersecs < 0) {
sersecs = 00;
}
document.tstest.clock.value=serhrs+":"+sermins+":"sersecs;
setInterval("show()",1000);
}
</script>
<head>
<body onload="calculate()">
<form name="tstest"><input type="text" size="10" name="clock"></form>
</body>
See if you can figure out what is wrong. Ohh and I do have it set as a ".php" file. :(
In line 53, you for got a + to concatenate the last variable. You have:
document.tstest.clock.value=serhrs+":"+sermins+":"sersecs;
it should be:
document.tstest.clock.value=serhrs+":"+sermins+":"+sersecs;
Ok it works now. I was wundering if there was a way to make it so it still displays the time live but not in a textbox. My other script did it like that! Thanks for your help. ;)
Change this:
document.tstest.clock.value=serhrs+":"+sermins+":"+sersecs;
to:
document.getElementById("clock").innerHTML = serhrs+":"+sermins+":"+sersecs;
and then add <span id="clock"></span> to your page.
Ok that part works now! What do I add to make it display AM/PM? Here is my code:
<script language="JavaScript">
function calculate() {
var serhours = <?PHP echo date("g") ?>;
var serminutes = <?PHP echo date("i") ?>;
var serseconds = <?PHP echo date("s") ?>;
var Digital=new Date();
var hours=Digital.getHours();
var minutes=Digital.getMinutes();
var seconds=Digital.getSeconds();
if (hours<=9) {
hours="0"+ hours;
}
if (minutes<=9) {
minutes="0"+minutes;
}
if (seconds<=9) {
seconds="0"+seconds;
}
hrsdif = serhours - hours;
mindif = serminutes - minutes;
secdif = serseconds - seconds;
show();
}
function show() {
var Digital=new Date();
var hours=Digital.getHours();
var minutes=Digital.getMinutes();
var seconds=Digital.getSeconds();
var serhrs = Number(hours) + Number(hrsdif);
var sermins = Number(minutes) + Number(mindif);
var sersecs = Number(seconds) + Number(secdif);
if (serhrs < 0) {
serhrs = 00;
}
if (sermins < 0) {
sermins = 00;
}
if (sersecs < 0) {
sersecs = 00;
}
document.getElementById("clock").innerHTML = serhrs+":"+sermins+":"+sersecs;
setInterval("show()",1000);
}
</script>
<head>
<body onload="calculate()">
<span id="clock"></span>
</body>
document.getElementById("clock").innerHTML = serhrs+":"+sermins+":"+sersecs+" <?PHP echo date("A"); ?>"; should do it...
That did the trick. It works now. Now the only thing I have to figure out is how to make my log.txt file clear when my clock.php file has the time of 12:00PM in it. I would make it recheck the file on one minute intervuls so it would check the clock.php file evry minute and if one time it checks the clock.php file and it has 12:00PM in it the log.txt file will clear. I know its possible because I have seen it done before. I just have to figure out what I will need to make it work!
You don't understand... unless you are going to leave this page running on your own computer ALL the time and send a request to the server when it hit's midnight (and, if you are going to do that, the above was the hard way...) it's not going to work anyway...
Sorry Pyro, it's just that someone once told me that you could make it so the script thinks that the clock file is the servers real clock, you could trick it. He said that where ever you would use a cron to go look at the server time you would just have it look at the clock file instead and it would work like that. He actually showed it to me working once but I havn't talked to him for a long time and I lost my copy that he gave me thats why I have been asking about all this on this forum! Sorry for inconvieniancing you so much. :(
No problem. I'm just trying to point out that unless this file is going to be left running somewhere all the time, it's not going to work.
Ohh well I think I'm just going to give up on this idea because I have more important things to work on my site right now like my chat which is wigging out. I can't figure out what is wrong. Anyways thanks. :D
Nevermore
06-26-2003, 11:03 AM
Why not have it clear when it gets to a certain file size? That would cater for sudden rushes too.