我遇到了一个非常奇怪的问题。我的应用程序导致我的服务器疯狂。
我的iOS应用程序使用tfhple来解析服务器上php页面的数据。它从页面上抓取一个名称,并检查该活动还剩多少时间。
NSString *URL1 = @"http://alefdev.com/tzedakah/current.php";
NSData *webPage = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat: URL1]]];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:webPage];
NSArray *elements = [xpathParser search:@"//name"];
if([elements count] == 0)
{
NSLog(@"array is empty");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Cannot connect to server."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[adupdate invalidate];
rc.enabled = YES;
rc.hidden = NO;
}
else{
TFHppleElement *element = [elements objectAtIndex:0];
NSString *ttl = [element content];
NSLog(@"Charity Name: %@", ttl);
todayscharity.text = ttl;
}
每隔10秒,它调用服务器,获取所需的信息,并将其放入数组中。如果数组为空,则定时器无效,并出现"Reconnect"按钮。"重新连接"按钮重新启动计时器。
计时器:
-(void)starttimer{
adupdate = [NSTimer scheduledTimerWithTimeInterval:(10.0) target:self selector:@selector(updateAd) userInfo:nil repeats:YES];
NSLog(@"starttimer called");}
重启功能
- (IBAction)reconnect:(id)sender
{
[self starttimer];
}
如果它失败(空数组),然后重新启动,它再次尝试从我的服务器抓取信息,但不能。整个站点给出一个内部服务器错误(500)。
php脚本获取当前日期
$today = getdate();
$todayfull = $today['year'].$today['mon'].$today['mday'];
查看日期:
if(($todayfull == 201192)||($todayfull == 201193))
然后回显一些文本并在匹配日期后运行倒计时功能。
function countdown($year, $month, $day, $hour, $minute)
{
$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
$today = time();
$difference = $the_countdown_date - $today;
if ($difference < 0) $difference = 0;
$days_left = floor($difference/60/60/24);
$hours_left = floor(($difference - $days_left*60*60*24)/60/60);
$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
if($minutes_left < 1)
{
echo "<ends>Less than 1 minute</ends>";
}
else{
echo "<ends>".$days_left." day ".$hours_left." hours ".$minutes_left." minutes</ends>";
}
Error Logs show:
[Sun Sep 4 14:01:53 2011] [error] [client 184.100.79.143] File does not exist: /home/alefdev2/public_html/error_500.htm
[Sun Sep 4 14:01:53 2011] [error] [client 184.100.79.143] Premature end of script headers: /home/alefdev2/public_html/tzedakah/current.php
[Sun Sep 4 14:01:43 2011] [error] [client 184.100.79.143] File does not exist: /home/alefdev2/public_html/error_500.htm
[Sun Sep 4 14:01:43 2011] [error] [client 184.100.79.143] Premature end of script headers: /home/alefdev2/public_html/tzedakah/current.php
[Sun Sep 4 14:01:43 2011] [error] [client 184.100.79.143] File does not exist: /home/alefdev2/public_html/error_500.htm
[Sun Sep 4 14:01:43 2011] [error] [client 184.100.79.143] Premature end of script headers: /home/alefdev2/public_html/tzedakah/current.php
[Sun Sep 4 14:00:25 2011] [error] [client 184.100.79.143] File does not exist: /home/alefdev2/public_html/error_500.htm
完整的php脚本如下:
<?php
$today = getdate();
$todayfull = $today['year'].$today['mon'].$today['mday'];
echo "Todayfull: $todayfull";
if(($todayfull == 201192)||($todayfull == 201193))
{
echo "<name>The Lone Soldier Center in memory of Michael Levin</name>
<description>Lone Soldier Center desc.</description>
";
countdown(2011,9,4,0,0);
}
else if(($todayfull == 201194)||($todayfull == 201195)){
echo "<name>Friends of Itamar</name>
<description>Friends of Itamar desc.</description>
";
countdown(2011,9,6,0,0);
}
else{
echo "Error: Could not match dates.";
}
?>
<?php
function countdown($year, $month, $day, $hour, $minute)
{
$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
$today = time();
$difference = $the_countdown_date - $today;
if ($difference < 0) $difference = 0;
$days_left = floor($difference/60/60/24);
$hours_left = floor(($difference - $days_left*60*60*24)/60/60);
$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
if($minutes_left < 1)
{
echo "<ends>Less than 1 minute</ends>";
}
else{
echo "<ends>".$days_left." day ".$hours_left." hours ".$minutes_left." minutes</ends>";
}
}
?>
我的主机(SiteGround)说我只是有太多的IMAP进程在运行,但只有当我试图通过这个应用程序重新连接时,网站才会崩溃。
有两件事在发生。首先,您对服务器的调用太多,因此我认为您的计时器失效工作不正确。其次,PHP脚本没有输出正确的标头,因此会出现服务器错误。