Jump to content

Recommended Posts

I'm not even sure what scripting language I need to use.

 

I'm using php to retrieve some chat data. This works fine, but every now and then it takes forever to load, which causes my main page to take forever.

 

Is there a way to stop reading data off of the site if it takes too long?

I tried some time out methods, but no luck so far.

Link to comment
https://forums.phpfreaks.com/topic/155581-stop-part-of-the-script/
Share on other sites

how advanced are you in php?

 

If you're more advanced, you could try a socket, and after x amount of loops if you receive 0 bytes of data, then you could break; the loop..

 

or uhm..

 

set_time_limit

 

but that is for the ENTIRE php script's processing.. not just for 1 specific thing..

 

or just use file fuctions, and do the same thing as the socket would.. but that would probably give you the same problem as file_get_contents considering fopen and file_get_contents go thru the same initial processes

 

anyway goodluck

The script is fine, trust me.

 

<?php
$inbox_text_only = true;
include "global.php";
if(isset($inbox_msg))
{
header("Content-type: image/png");
$im    = imagecreatefrompng("images/msgbox.PNG");
$color = imagecolorallocate($im, 220, 0, 0);//0,0,0
imagestring($im, 4, 5, 3, $inbox_msg, $color);
imagepng($im);
imagedestroy($im);
return;
}
//ignore everything above

$data = "http://stats.thegaminguniverse.nl/inchat.php?chan=oz";
$contents = file_get_contents($data);

$str = explode("\n",$contents);

$count = $str[0];
$users = $str[1];


$users = str_replace("&Zelda", "", $users); 
$users = str_replace(",","", $users);
$ozcount = $count - 1;//ignore zelda
if($ozcount == -1)
$ozcount = 0;
$title = "Members in #OZ (" . $ozcount . ")"; 

if($count >= 2)
{
$advertise[0] = "Just ask anyone of the members with ~ or &"; 
$advertise[1] = "beside their name for some help with Open Zelda.";
$advertise[2] = "Click on this image to join the #oz chat!";
}
else
{
$advertise[0] = "Sorry there are currently no members in the OZ chat."; 
$advertise[1] = "at this time.";
$advertise[2] = "Click on this image to join the #oz chat!";
}

$users = wordwrap($users, 45, "*"); //edit this line if the users stretch too far or to little on image.
$users = explode('*',$users, -1);

header("Content-type: image/gif");
if(isset($_GET['pressed']))
$im    = imagecreatefromgif("images/pinchat.gif");
else
$im    = imagecreatefromgif("images/inchat.gif");
$bg    = imagecolorallocate($im, 255, 255, 255);
$color = imagecolorallocate($im, 0, 0, 0);//0,0,0
$color2 = imagecolorallocate($im, 0, 0, 0);//0,0,0

$px    = (imagesx($im) - 6.3 * strlen($title)) / 2;
imagestring($im, 3, $px, 5 , $title, $color2);

$px    = (imagesx($im) - 7.8 * strlen($users[0])) / 2;
imagestring($im, 4, $px, 25 , $users[0], $color2);
imagestring($im, 4, $px + 1, 26 , $users[0], $color2);
//imagestring($im, 4, $px + 2, 27 , $users[0], $color);

$px    = (imagesx($im) - 7.8 * strlen($users[1])) / 2;
imagestring($im, 4, $px, 40 , $users[1], $color);
imagestring($im, 4, $px + 1, 41 , $users[1], $color);
//imagestring($im, 4, $px + 2, 42 , $users[1], $color2);

$px    = (imagesx($im) - 7.8 * strlen($users[2])) / 2;
imagestring($im, 4, $px, 55 , $users[2], $color);
imagestring($im, 4, $px + 1, 56 , $users[2], $color);
//imagestring($im, 4, $px + 2, 57 , $users[2], $color2);

$px    = (imagesx($im) - 7.8 * strlen($users[3])) / 2;
imagestring($im, 4, $px, 70 , $users[3], $color);
imagestring($im, 4, $px + 1, 71 , $users[3], $color2);
//imagestring($im, 4, $px + 2, 72 , $users[3], $color1);

$px    = (imagesx($im) - 7.8 * strlen($users[4])) / 2;
imagestring($im, 4, $px, 85 , $users[4], $color);
imagestring($im, 4, $px + 1, 86 , $users[4], $color2);

$px    = (imagesx($im) - 6.3 * strlen($advertise[0])) / 2;
imagestring($im, 2, $px, 95 , $advertise[0], $color2);
$px    = (imagesx($im) - 6.3 * strlen($advertise[1])) / 2;
imagestring($im, 2, $px, 105 , $advertise[1], $color2);
$px    = (imagesx($im) - 6.3 * strlen($advertise[2])) / 2;
imagestring($im, 2, $px, 130 , $advertise[2], $color2);


imagegif($im);
imagedestroy($im);
break;

?>

 

Everything above the line "//ignore everything above"

is just a special script for telling us admins if we got an inbox message.

 

Ok, I'm using file_get_contents to get the data. Can I tell it to only do this for so long?

I don't think you can do that with file get content but you can with cUrl

 

<?php
$timeout = 10;
$ch = curl_init("http://www.somesite.com/somepage.php");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.64');
$output = curl_exec($ch);
curl_close($ch);

echo $output;
?>

 

The $timeout is in seconds.

I don't think you can do that with file get content but you can with cUrl

 

<?php
$timeout = 10;
$ch = curl_init("http://www.somesite.com/somepage.php");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.64');
$output = curl_exec($ch);
curl_close($ch);

echo $output;
?>

 

The $timeout is in seconds.

 

sweet, didn't know that (didn't test it either) :)

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.64');

 

^ what's that for?

 

That tells the other server what the browser it is - which is easily faked as seen above, mostly used for just statistics or for showing different content for different browsers

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.64');

 

^ what's that for?

 

I've got error from some website that block the curl default useragent. Now i always include a fake one, just in case.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.