FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Demo PHP/Ajax slideshow server crashing
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Demo PHP/Ajax slideshow server crashing [message #170902] Mon, 06 December 2010 15:45 Go to previous message
pittendrigh is currently offline  pittendrigh
Messages: 4
Registered: December 2010
Karma:
Junior Member
I wanted to learn something about Ajax, so I wrote the following quick-
and-dirty slideshow demo starting from Rasmus Lerdorf's PHP/Ajax post
(about AJAX) to this group from (5,6,7,8 years ago?).

This "slideshow" is a little jerky because it does not do image to
image fading just yet. I'll worry about that later.

I put this on the main page of a page rank 4 website, looping over
approximately 100 images.
It ran for close to a week without incident. Then, out of curiosity, I
made the image collection closer to 1000 images.

That ran (seemingly) without incident for close to 8 hours (on a
virtual dedicated centos server at godaddy) and
then the apache server crashed. I reduced the number of images to 50
and restarted apache. And then the following has
been running (over 50 rather than 1000) images for another week
without incident. What crashed apache when the image
collection was large?

Would this be a memory leak/garbage collection issue?

<?php
session_start();

function is_image($file)
{
$ret=FALSE;
$dotpos = strrpos($file,'.');
if($dotpos)
{
$suffix = substr($file,$dotpos+1);
if(stristr($suffix,'jpg')
|| stristr($suffix,'jpeg')
|| stristr($suffix,'gif')
|| stristr($suffix,'png')
){ $ret=TRUE;}
}

return $ret;
}

function getImageFilenames($dir) {
$imageFilenames = array();
$cnt = 0;
if ($dir_handle = opendir($dir)) {
while (($file = readdir($dir_handle)) != false) {
if ($file != '.' && $file != '..') {
if (is_image($file)) {
$imageFilenames[$cnt] = $file;
$cnt++;
}
}
}
closedir($dir_handle);
} else {
echo "failed to opendir $dir for some reason<br>";
exit();
}
return $imageFilenames;
}

if(!isset($_SESSION['imageFilenames']))
{
$_SESSION['imageFilenames'] = getImageFilenames("looper");
$_SESSION['cnt'] == count($_SESSION['imageFilenames']);
}

echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
var http = createRequestObject();

function createRequestObject()
{
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

function doit()
{
setInterval("sndReq()", 4000);
}

function sndReq()
{
http.open(\'get\', \''.$_SERVER['PHP_SELF'].'\');
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse()
{
if(http.readyState == 4)
{
var response = http.responseText;
var update = new Array();
if(response.indexOf(\'|||\' != -1))
{
update = response.split(\'|||\');
var obj = document.getElementById(\'foo\');
obj.style.background = \'url(looper/\' + update[4]
+ \') no-repeat center center\';
obj.innerHTML = update[4];
}
}
}
</script>';

$def = $_SESSION['imageFilenames'][0];

echo '
<style type="text/css">
html,body {width: 100%; height: 100%; background: #cccccc; }
#foo
{
width: 300px;
height: 200px;
margin: 0 auto;
padding: 1em;
color: white;
background: url(looper/'.$def.') no-repeat;
text-shadow: #222222 1px 1px 0px;
}

</style>
</head>

<body onload="doit()">';

$_SESSION['cnt'] += 1;
if($_SESSION['cnt'] > count($_SESSION['imageFilenames'])-1)
{
$_SESSION['cnt'] = 0;
}
$cnt = $_SESSION['cnt'];
$imgName = $_SESSION['imageFilenames'][$cnt];

// javascript in handleResponse () changes the background url for
div id="foo"
echo '<div id="foo"></div>';

echo '<div style="display: none;">'.$_SESSION['cnt'] .'|||foo|||'.
$imgName.'|||'.count($_SESSION['imageFilenames']).'</div>';

// show the page source, for some reason
highlight_file($_SERVER['SCRIPT_FILENAME']);
echo '
</body>
</html>
';
[Message index]
 
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: time consuming loop
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 01:14:20 GMT 2024

Total time taken to generate the page: 0.04404 seconds