这导致我的页面加载速度非常慢或根本不加载?
我刚刚从 php 5.3.4 更新到 5.3.8。更新后,似乎我所有包含 try{ 的代码都会导致我的页面挂起并耗尽我的所有服务器内存。
if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');
try{
$response = @new SimpleXMLElement($xml);
foreach($response as $field=>$value){
$result[(string)$field] = (string)$value; }
return $result;
}
catch(Exception $e){ $this->errors[] = $e->getMessage();
return;
}
}
这也引发了一个重大问题
<?php
try{
$gt = new Gtranslate;
$gt->setRequestType('curl');
$SQL = "SELECT * FROM PAGE_CONTENT WHERE live_page = '1'";
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ) ) {
$page_id_sub = $row["page_id"];
$page_title = $row["page_title"];
$page_permalink = $row["page_permalink"];
if(empty($mylang)){
echo "<a href='/$permalink/$page_permalink.html'>$page_title</a> |";
}
else {
$page_trans = $gt->$mylang("$page_title");
echo "<a href='/$permalink/$page_permalink.html'>$page_trans</a> |";
}
}
}
catch (GTranslateException $ge){
echo $ge->getMessage();
}
?>
I just updated from php 5.3.4 to 5.3.8. After the update it seems that all my code that includes try{ causes my page to hang and use up all my server memory.
if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');
try{
$response = @new SimpleXMLElement($xml);
foreach($response as $field=>$value){
$result[(string)$field] = (string)$value; }
return $result;
}
catch(Exception $e){ $this->errors[] = $e->getMessage();
return;
}
}
This also causes a major issues
<?php
try{
$gt = new Gtranslate;
$gt->setRequestType('curl');
$SQL = "SELECT * FROM PAGE_CONTENT WHERE live_page = '1'";
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ) ) {
$page_id_sub = $row["page_id"];
$page_title = $row["page_title"];
$page_permalink = $row["page_permalink"];
if(empty($mylang)){
echo "<a href='/$permalink/$page_permalink.html'>$page_title</a> |";
}
else {
$page_trans = $gt->$mylang("$page_title");
echo "<a href='/$permalink/$page_permalink.html'>$page_trans</a> |";
}
}
}
catch (GTranslateException $ge){
echo $ge->getMessage();
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能与
try
关系不大,而与您的远程请求关系更大(即file_get_contents()
和$gt->$mylang() )。
作为基准,删除这些行并查看页面的性能。如果他们确实是罪魁祸首,您可能需要考虑缓存他们的响应或其他一些方法,这样您就不会在每个页面加载时发出请求。
This probably has very little to do with
try
and much more to do with your remote requests (i.e.file_get_contents()
and$gt->$mylang()
).As a benchmark, remove those lines and see how your page performs. If they are indeed the culprit, you may want to consider caching their response or some other approach so your not making the request on every page load.