1

I have a php script (file.php) that is used to counts banner impressions and returns an image, it end like this:

readfile('http://somedomain.com/banner.jpg');
mysql_close();
exit;

This file is used very much, and sometimes when the image is hosted on external server it load slow.

So to reduce my hosting usage i think to add a max execution time only on this script, for example: if 5-10 seconds are passed, the script closes

i think that is possibile, but how?

3

3 Answers 3

2

I just tested set_time_limit() but apparently readfile('http...'); doesn't honor this setting. The script was executed longer than the amount of time set via set_time_limit, but then aborted before the next "in-script" statment was executed.
But since php 5.2.1 the http url wrapper has a context option timeout which seems to do the trick.

mysql_close();
$ctx = stream_context_create( array(
    'http' => array(
        'timeout'  => 4.7 // in seconds
    )
);
readfile('http://somedomain.com/banner.jpg', false, $ctx);
exit;

see http://docs.php.net/context.http

Sign up to request clarification or add additional context in comments.

3 Comments

Very useful! Thank you very much! Just one question, it work with https too?
@ipel : as stated in the documentation: Context options for http:// and https:// transports
just to be sure, to use the stream_context_create function with https pages, in my case, should i use: "steam_context_create(Array("http" => Array(..." or "steam_context_create(array('ssl' => array(.."???
1

You can set the execution time on the top of your script with the set-time-limit function.

1 Comment

much more useful answer than .ini file change, which affects ALL scripts (scary compatibility issue!) and may not be possible in shared hosting.
0

Change you max_execution_time in you php.ini or add to your script ini_set('max_execution_time', 5);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.