How to keep a dynamic webpage dynamic ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blubba54
Forum Newbie
Posts: 9
Joined: Fri Jul 05, 2002 6:53 am

How to keep a dynamic webpage dynamic ...

Post by blubba54 »

How to keep a dynamic webpage dynamic ...

Hi everybody.
Can someone help me with this big problem that I have. Me and my friend is trying to make a community in PHP / mySQL. On the site we have a “friends list” that is going to be updated so the person sees if a friend is online. We can easily solve this by updating the site every, say 30 second but this is not so smart. So we thought that we could make a PHP script that checked with the mySQL-database all the time and updated the page only when it is necessary. That will say when a friend in his or hers list go online.

But now we have 2 problems:

1) If just make a loop that searches for someone to go online in the mySQL-database the site will never stop loading. And if you as an user at the site presses “F5” or refresh you start a new session of PHP and we end up with 20 PHP sessions in the “Task Manager” in Windows 2000 that we are using.
2) The other problem is almost told in the first problem, and it’s the problem that occurs when the user refreshes the page. We end up with over 20 sessions of PHP.exe in the “Task Manager” in Windows 2000 and the server almost crashes. And we have tested “Exit;” but it doesn’t work when the script isn’t finished and the user refreshes the page. And one more thing is that explorer has an one timeout at 5 minute, so we hade to update and load the same page every 4 minute so Internet Explorer didn’t timeout our script.

This is what we written so far and it doesn’t work:

<?php
session_start();
$test = "strang";
print ("HTTP/1.1 200 OK\nContent-Type: multipart/x-mixed-replace;boundary=$test\n\n");

print("--$test");
set_time_limit(0);
ignore_user_abort(1);
$tid = time();
if(session_is_registered('check'))
{
exit;
}

else
{
session_register('check');
}

while(1)
{
if(time() - $tid > 240)
{
break;
}
print("--$test");
sleep(1);
}
?>
<?php header("Location: check.php");?>


Please help US!

//Bezze
:?: :!: :?: :!:
CodeEye
Forum Commoner
Posts: 25
Joined: Fri Jul 05, 2002 7:19 am

Post by CodeEye »

if the browsers are up todate you can use dom to load the a script sheet and then update the number eg
//example copied from dotvoid.com
<script type="text/javascript">
var g_remoteServer = 'server.js';
var g_intervalID;

function callServer() {
var head = document.getElementsByTagName('head').item(0);
var old = document.getElementById('lastLoadedCmds');
if (old) head.removeChild(old);

script = document.createElement('script');
script.src = g_remoteServer;
script.type = 'text/javascript';
script.defer = true;
script.id = 'lastLoadedCmds';
void(head.appendChild(script));
}

g_intervalID = setInterval(callServer, 30000);
</script>

just update the number from there
blubba54
Forum Newbie
Posts: 9
Joined: Fri Jul 05, 2002 6:53 am

Don't understand really ...

Post by blubba54 »

What is the file "server.js" and what dose it do? And can you explain some more how I can do this? :?:
CodeEye
Forum Commoner
Posts: 25
Joined: Fri Jul 05, 2002 7:19 am

Post by CodeEye »

it is the server file you would probably call it server.php it would contain

var el = document.getElementById("idusers");
<?php

$numbers = number of people connected;
el.appendChild( document.createTextNode(\$numbers\) );


?>

i am not sure how to write javscript in php but i think this is correct

idusers is the name of the numbers of users so the code would be

<div id="idusers">34</div>

the number would change as soon as the page loaded
CodeEye
Forum Commoner
Posts: 25
Joined: Fri Jul 05, 2002 7:19 am

Post by CodeEye »

i forgot to mention that you have to check if the browser is dom compatible or not put this if statement arround the first bit of java script
if(document.getElementById){
...
...
}
only works on thes browsers

Microsoft Internet Explorer 5+
Microsoft Internet Explorer 5 Mac
Mozilla M17+
Netscape 6+
Konqueror 2.2+
IceStorm 5.
iCab 2.x+
MS Pocket IE 3.x+
blubba54
Forum Newbie
Posts: 9
Joined: Fri Jul 05, 2002 6:53 am

thanks!

Post by blubba54 »

Thank you very much. I hope this will work. :)
Post Reply