Accent Image
Pete on March 25th, 2004

If you don’t have a blog, or you don’t have a blogroll… this probably won’t be of much interest to you. That’s unless, of course, you really like web design and programming despite not having a blog or blogroll. Nevertheless, I’m going to get on with it.

The two ways of including your Blogroll on your site are a) Javascript and b) PHP Include. Wizbang discussed this month’s ago, but I have some “new” insight into the situation.

The PHP Include method is great, except that none of your code will display until the request to blogrolling’s server is complete. That means, of course, if BlogRollingis being slow, or is actually down, your page could take 30 seconds (or more) to begin loading. That’s not a problem very often, but from time to time it is.

The current solution is to just use the Javascript include instead. What’s wrong with that, you might ask? Well… I personally try to avoid javascript when I can. Moreover, though, google (et al) won’t index links included that way, because that would require their spiders to download and execute javascript… which is a really bad idea. That being the case, many, many blogrolls are not indexed by google and that’s hurting everyone’s PageRank.

Also, all other things being equal, pulling the blogroll down on the server side and sending it with the rest of the page is several times faster (when the server is cooperating) for a lot of reasons. One of them is that the code sent from YOUR server is smaller. It also doesn’t have to be executed. It’s also already inline, which seems to render more quickly than code inserted with javascript.

So it’s obvious (to me), that the PHP Include is preferable, but it’s obviously not acceptable to have no control over when your page loads and when it doesn’t. When BlogRolling went down earlier this evening, I snapped to action, implementing an idea I had come up with before. Tech nerds, take note:

When you use the include or fopen/fread/etc functions in PHP with URLs, PHP handles opening connections to the server (etc) behind the scenes. That means you lose control over a lot of details and options. One of those options is the timeout length of the connection. That is, how long does your server wait before giving up on the remote server. It seems like the default is 30 seconds. That means if BlogRolling is down, you’ll have a THIRTY SECOND delay before your data gets sent. This is especially bad considering that when blogrolling is alive, it tends to respond in WELL under ONE second. So, instead of using include and just living with the 30 second timeout, the solution to this problem is to open and handle the connection by hand and set the timeout. This can be accomplished with the fsockopen function. That way we can set a low timeout (say, 0.75 seconds) and just not load the blogroll if it’s slower than that.

Problem solved, right? Maybe. There’s an EVEN BETTER solution. That is, if the connection times out, go ahead and send the Javascript code instead… transfer the long wait to the client to deal with after the rest of the content of the page has loaded. If it times out after that, it will have had no negative effect on the loading.

I know what you’re thinking… GREAT! WONDERFUL! But how? Well… fear not! I wrote this easy-to-use function for my site, and I’m going to provide it for the masses. It comes, obviously, as-is but I’ll try to help where I can.

The highlighted code can be found here… you should be able to copy and paste that into your standard PHP Include file and use it as described.

One more tip: if you put a flush() statement before commands (like remote includes) that might take a while, you can sometimes serve up SOME of the data while you wait on the rest. In some cases it may be enough for the header/sidebar of your page to display.

Questions? Comments?

…this is what I did tonight instead of studying for my Compilers MidTerm. Heh.

12 Responses to “Nerd Info: PHP Includes and Blogrolling”

  1. Kevin says:

    Good code, but should one be defining $br_code somewhere? I don’t see a $br_code= statement except in the commenting.

  2. Pete says:

    $br_code is a parameter to $br_include.

  3. I got that much, but where does one define the blogrolling ID? Or to say, where do I paste in my number, Pete?

  4. Pete says:

    Maybe $br_id would’ve been better… that’s what the “$br_code” variable is supposed to be… so if you include this file (or just add the code to another one of your includes) you would display your code by writing something like this:

    ***********
    <b>My BlogRoll:</b><br>
    <?
        br_include(“e4eBlogRollingIdGoesHere…”);
    ?>

  5. Bobby says:

    Hey Pete, for all of us out there who like to blog but don’t know much about technology, can you answer this:

    What exactly is a blogroll?

    I see them on some people’s sites but have no clue what it really is.

  6. Pete says:

    It’s basically a link manager so you don’t have to edit your templates every time you change a link to another blog/site.

  7. Kevin says:

    Pete – I emailed you to take a look at my code-fu. Apparently, when my code talks, the words come out at the same time and (as we all know) the lips are supposed to move, then a long pause, then the actual sounds. Help!

  8. The br_include.phps file must not have made the trip from old host to new host. Link goes to 404 page.

  9. Pete says:

    Ooh… right you are. Thanks Kevin — all fixed now.

  10. Dave says:

    Code not found :( Any ideas where I can find it?

  11. Klemen says:

    Another (better?) way to fight include timeouts with PHP:

    $fp = @fsockopen (“www.domain.com”, “80″,$errno, $errstr, “2″);
    if ($fp) {
    include ‘http://www.domain.com/file_to_include.php';
    }

    This will make sure the file is not included if domain.com cannot be opened for 2 seconds.

  12. Pete says:

    that’s actually not quite as good, because the problem with blogrolling (at the time this was posted) wasn’t that the domain would time out, it was that the script serving the blogroll would time out.

    The main page (www.domain.com) would come back just fine, but the include would still time-out.

    Also, your method requires two socket connections and allows for two time-outs, the first being the domain check… the second being the actual include.

    The code linked from this post makes one connection and then either grabs the data it needs, or goes on to option b.