Re: Demo PHP/Ajax slideshow server crashing [message #170903 is a reply to message #170902] |
Mon, 06 December 2010 16:44 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 12/6/2010 10:45 AM, pittendrigh wrote:
> 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>
> ';
Who knows? What's your Apache error log show?
Your crash may or may not be related to this.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|