Link to home
Avatar of Colin Brazier
Colin BrazierFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Internal server error 500 caused by timeout

Hi experts,

OK, here's the deal:

php/mysql website.  

User clicks button to send sms out, about 15 to 70 at a time.
php on my site sends data to sms company's site which processes the message sending
meanwhile, my site is left inactive and waiting for a response.
After timeout of 30 seconds, I get an internal 500 error.

I cannot change timeout period (shared server).

Is there any way I can fool my website into thinking it is doing something, until all the messages have been sent?

Thanks,
    Col

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You can use set_time_limit().  It should work on a shared server.  If your hosting company prohibits this, get a new hosting company!

If you want to post the code that you're using now, maybe we can suggest a new way to send the SMS.
Avatar of Colin Brazier

ASKER

Hi Ray,

I think we've been down this road before and up til now I have found a way to stick with my present company - I think you have guessed who that is previously.

I'll see if I can dig out my code.

Col
Hope this is enough.  The most I would like to send at a time is currently 80, but I have had to tell my users to send just one team at a time (about 15).  Even this low number causes problems occasionally.

Cheers text-send.php
Avatar of rationalboss
rationalboss

Just create an <iframe> in your website that would call the script that will process the message. In that script, make sure you use ignore_user_abort(true); see http://www.php.net/ignore_user_abort

Then show a loading image while the iframe is loading. Or maybe set an auto-reload the page to check if the messages have been sent. It would be better if you use an AJAX request to do this.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
As a sidebar note, I think we know where the delay is occurring in this script.  But if you are not sure and you want to time part or all of a script this class comes in handy.
<?php // RAY_oop_stopwatch.php
error_reporting(E_ALL);


// DEMONSTRATE A SCRIPT TIMER FOR ALL OR PART OF A SCRIPT PHP 5+
// MAN PAGE http://us.php.net/manual/en/function.microtime.php


class StopWatch
{
    protected $a, $z;
    public function __construct()
    {
        $this->a = array();
        $this->z = array();
    }

    // A METHOD TO CAPTURE A START TIME
    public function start($name='TIMER')
    {
        $this->a[$name] = microtime(TRUE);
    }

    // A METHOD TO CAPTURE AN END TIME
    public function stop($name='ALL')
    {
        if ($name == 'ALL')
        {
            foreach ($this->a as $name => $start_time)
            {
                if (!isset($this->z[$name])) $this->z[$name] = microtime(TRUE);
            }
        }
        else
        {
            $this->z[$name] = microtime(TRUE);
        }
    }

    // A METHOD TO READ OUT THE TIMER(S)
    public function readout($m=1000, $eol=PHP_EOL)
    {
        $str = NULL;
        foreach ($this->a as $name => $start_time)
        {
            $str .= $name;
            if (!isset($this->z[$name]))
            {
                $str .= " IS STILL RUNNING";
            }
            else
            {
                $lapse_time = $this->z[$name] - $start_time;
                $lapse_msec = $lapse_time * $m;
                $lapse_echo = number_format($lapse_msec, 1);
                $str .= " $lapse_echo";
            }
            $str .= $eol;
        }
        return $str;
    }
}


// INSTANTIATE THE STOPWATCH OBJECT
$sw  = new Stopwatch;

// SET STOPWATCH NAMES
$go = 'GOOGLE ONLY';
$gy = 'GOOGLE AND YAHOO!';
$yo = 'YAHOO! ONLY';

// START SOME TIMERS
$sw->start($go);
$sw->start($gy);

// PERFORM SOME ACTIVITY THAT YOU WANT TO TIME
$page = 'http://google.com';
$html = file_get_contents($page);

// STOP ONE OF THE STOPWATCHES AND START THE OTHER
$sw->stop($go);
$sw->start($yo);

// PERFORM SOME OTHER ACTIVITY THAT YOU WANT TO TIME
$page = 'http://yahoo.com';
$html = file_get_contents($page);

// REPORT THE STOPWATCHES CONTENT (TWO WILL BE INCOMPLETE)
echo nl2br($sw->readout());

// STOP ALL OF THE REMAINING STOPWATCHES
$sw->stop();

// REPORT THE STOPWATCHES CONTENT AGAIN
echo nl2br($sw->readout());

Open in new window

Ray, thanks.  I have never come across cURL before and have been reading it up.  I don't think I have the ability to write such a script though.  

Looking at my script again (taken from a Clickatell forum example if I remember rightly) I see that it connects to clickatell for every message to be sent.  Is there a way of just connecting once and scooting thru all the texts in that one session?  If so, would that help?
Rationalboss thanks too.  I'm not sure I'd be allowed to set ignore_user_abort(true); Again, AJAX is new to me (I have a lot to learn don't I!).
<<a way of just connecting once and scooting thru all the texts in that one session>>

I did that and it has speeded things up.