使用PHP获取条目进程重载以动态设置元标记

I'm using RapidWeaver to create a blog page at www.chocchoc.club/blog/index.php

Because of how the program works I cannot manually set the meta tags for each page so have tried to do it using some PHP, so that I can set some OG an Twitter Cards meta information. In this case I'm using a function to get the URL and Site Title from the HTML. Here's what I've got so far..

At the very start of the php page:

<?php

    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    function getTitle($url){
      $str = file_get_contents($url);
      if(strlen($str)>0){
        preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
      return $title[1];
        }
      }
?>

This should set the variable $url to the URL of the page and set the function getTitle to get the text within the <title> tags of the page.

Here's the HTML and PHP I'm using to put this into the document:

<meta property="og:url" content="<?php echo $url; ?>" />
<meta property="og:title" content="<?php echo getTitle("$url"); ?>" />

Now this works and produces the HTML that I want...

<meta property="og:url" content="http://www.chocchoc.club/blog/files/important-news.php" />
<meta property="og:title" content="'We're stockpiling Flakes!' Brits in US aghast at Cadbury's ban | #ChocChoc Blog" />

However, I am hosting on Godaddy shared hosting and since implementing this code I've gotten a lot of Service Unavailable errors.

When checking the CPU and concurrent connections page I get this message:

Your site has been limited within the past 24 hours You have reached entry processes (number of simultaneously running php and cgi scripts, as well as cron jobs and shell sessions) limit 712 times

Here's the entry processes graph for the last 24 hours (I'm writing this in the morning after I was working on site and viewing it so they have dropped off).

http://www.chocchoc.club/images/ep_godaddy.tiff

(Can't post images as this is my first post)

Here's the detail from the log:

http://www.chocchoc.club/images/cpu_connections_usage_details.tiff

Strange is that 15:00 was midnight for me and when I left it alone. But processes kept occurring for a few hours after...

Any ideas if this PHP code could be the culprit?! A friend of mine says that it could be a function not terminated correctly?

Thanks for any help received!

-Neil

Any ideas if this PHP code could be the culprit? Yes, your code is in an loop

Whats causing your problem

Your function is using file_get_contents this function is requesting the content of an file or an webpage and put this in an string. In case of an webpage its taking the html out of this website and runs it like an normal http request.

So you are requesting the same website you are currently on. When you load the website it loads the website again and again. So they are in an loop.

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; « loads website over and over.

An Fix?

When I understand it correctly the RapidWeaver software does not have the possibility to get the page title with an easy function. When you have access to there core you can change it from there, maybe write an hook/extention that makes it possible.

If you do wanna use the file_get_contents way you can do it like this. So only the request from the user is parsed. But be aware that its an 'dirty' way, that can result in memory overloads and DDOS vulnerability

$url = "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI] . "?mode=skip";

function getTitle($url){
  if($_GET['mode'] != 'skip') {
    $str = file_get_contents($url);
    if(strlen($str)>0){
        preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
        return $title[1];
      }
    }
  }