Tag Archives: PHP

Changed method of displaying random Gallery2 image to bypass DNS performance issues

Yeah, I know this probably will not make much sense to most people other than those dealing with something similar.

In the past I had been using PHP’s CURL functionality to obtain and display a random gallery image on both my main website and blog. The code I used was similar to the following:

$ch = curl_init();
$timeout = 10; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://gallery.markheadrick.com/main.php?g2_view=imageblock.External&g2_blocks=randomImage&g2_linkTarget=_blank&g2_show=title|date|views';);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
if ($file_contents != false) {
   echo $file_contents;
} else {
   echo '
<div class="small">Error, exceeded '
.$timeout.' second connection timeout.</div>

'
."\n";
}

This method worked fine; however, every now and then it would take several seconds to complete causing the webpages to display that much slower. After much investigation from when I started noticing this many months ago, I determined that the delay was being caused by the amount of time it was taking the server to look-up the IP address of gallery.markheadrick.com. I used the following bit of code to test this with:

function getmicrotime()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

$port_starttime = getmicrotime();
$fp = @fsockopen('gallery.markheadrick.com', 80, $errno, $errstr, 10);
if (!$fp)
{
    echo "Offline ".number_format((getmicrotime() - $port_starttime), 4)." seconds\n";
}
else
{
    echo "Online ".number_format((getmicrotime() - $port_starttime), 4)." seconds\n";
    fclose($fp);
}

All this does is show the amount of time it takes to make a connection. Normally this should be a fraction of a second (0.0843 seconds for example). Well, on occasion this would take 4, 5 or more seconds to complete. If I used the server’s IP address instead of gallery.markheadrick.com, it would always take a fraction of a second (0.0001 seconds). So, the only difference was the system trying to determine what the IP address of gallery.markheadrick.com was. Similar behavior was displayed when I connected to google.com vs its IP address. In order for CURL to work, I had to supply it with gallery.markheadrick.com as the server has many domains and sub-domains associated with the same IP address.

Over time I’ve submitted numerous support tickets concerning this hostname look-up delay as the problem has surfaced, been fixed, and then resurfaced for whatever reason.

A couple days ago it started happening again and I submitted a new support ticket concerning it which more or less went nowhere and told them to just forget about it and close it.

Since my gallery is on the same server as my website and blog, I thought I should be able to obtain an image directly using PHP and MySQL; however, after looking at Gallery2’s database schema, this was going to be more difficult than I had thought. I then wondered if I could simply include some part of the Gallery code that dealt with the ImageBlock code that displays a random image. Then, I wondered if I could just include Gallery’s main PHP file and somehow feed it the variable values it needed to work. The following is what I eventually ended up with:

$_GET['g2_view'] = 'imageblock.External';
$_GET['g2_blocks'] = 'randomImage';
$_GET['g2_show'] = 'title|date|views';
$_GET['g2_linkTarget'] = '_blank';
ob_start();
include_once ($_SERVER['DOCUMENT_ROOT'].'/gallery/main.php');
$gallery_ob = ob_get_contents(); // Gets the contents of the output buffer
ob_end_clean(); // Ends capture and cleans the buffer so that it will not be displayed
$gallery_ob = str_replace('http://www.markheadrick.com/d/','http://gallery.markheadrick.com/d/',$gallery_ob);
$gallery_ob = str_replace('http://www.markheadrick.com/v/','http://gallery.markheadrick.com/v/',$gallery_ob);
echo $gallery_ob;

Hallelujah!! It worked! 🙂 During my search for answers the following two articles/threads helped me out the most: I want “include” to return a string instead of writing directly into the page. and [SOLVED] Get arguments in include function. Because I was including the code under a different sub-domain, it was using the wrong URL for the image and link. This is why I needed to add the two str_replace functions. On my blog, it was trying to load the image from blog.markheadrick.com instead of gallery.markheadrick.com. I also had to increase the amount of memory PHP could use for scripts on my blog from 32M to 64M.

Blog has been upgraded to WordPress v2.8.1

I have updated my blog to WordPress v2.8.1 which was just released. I did the auto upgrade option again which seems to have worked well. Even so, once again I had to edit the /wp-includes/vars.php file to force $is_apache to true (see below) since the SERVER_SOFTWARE variable comes back as WebServerX instead of Apache.

// Server detection
/**
 * Whether the server software is Apache or something else
 * @global bool $is_apache
 */

//$is_apache = ((strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) || (strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false)) ? true : false;
$is_apache = true;
/**
 * Whether the server software is IIS or something else
 * @global bool $is_IIS
 */

$is_IIS = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false) ? true : false;

Other than the modification to vars.php which I have always had to do, the upgrade went smoothly. 🙂

Of course, I did perform a complete database and file backup before upgrading! 🙂

Blog has been upgraded to WordPress v2.8

I have updated my blog to WordPress v2.8 which was just released. This time I tried the auto upgrade option. It seems to have worked well. Even so, once again I had to edit the /wp-includes/vars.php file to force $is_apache to true (see below) since the SERVER_SOFTWARE variable comes back as WebServerX instead of Apache.

// Server detection

/**
 * Whether the server software is Apache or something else
 * @global bool $is_apache
 */

//$is_apache = ((strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) || (strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false)) ? true : false;
$is_apache = true;
/**
 * Whether the server software is IIS or something else
 * @global bool $is_IIS
 */

$is_IIS = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false) ? true : false;

There was some code I modified in the RSS Feed widget in /wp-includes/rss.php in previous versions; however, so far it looks like I may not have to reapply that code. After completing the upgrade I did notice that I had to upgrade the TinyMCE Advanced plug-in for WordPress 2.8 compatibility.

Other than the modification to vars.php which I have always had to do, the upgrade went smoothly. 🙂

Of course, I did perform a complete database and file backup before upgrading! 🙂

Blog has been upgraded to WordPress v2.7.1

I have updated my blog to WordPress v2.7.1 which was just released. This time I tried the auto upgrade option. It seems to have worked well. Even so, once again I did have to edit the /wp-includes/vars.php file to force $is_apache to true (see below) since the SERVER_SOFTWARE variable comes back as WebServerX instead of Apache.

// Server detection

/**
 * Whether the server software is Apache or something else
 * @global bool $is_apache
 */

//$is_apache = ((strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) || (strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false)) ? true : false;
$is_apache = true;
/**
 * Whether the server software is IIS or something else
 * @global bool $is_IIS
 */

$is_IIS = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false) ? true : false;

I also had to reapply some code modifications, which I knew I would have to, related to the RSS Feed widget in /wp-includes/rss.php; however, I’ll make a separate post about why I modified it (didn’t I say that before?). Other than that the upgrade went smoothly. 🙂

As always, I did perform a complete database and file backup before upgrading! 🙂

My continuing experiences and thoughts concerning Twitter, Twitpic, and Audiotwit.

After a few more days of using Twitter, I decided to try Twitpic and Audiotwit that I had noticed others using.

Twitpic

This just seems to be a basic service for storing and commenting on images that sends out a tweet when you add a new photograph or comment on one. It has no provisions for creating albums or anything like that. Since I have my own photo gallery, I have no real need to use this as a place to store images. Same reason I don’t store images at my MySpace page or using something like Flickr. I will admit that the main page that shows where new images are coming from is kind of cool. I did upload one picture and here’s my twitpic page.

Audiotwit

To use this relatively new service, it appears, you send a reply tweet to @listensto with the artist and song title of what you are listening to.  An example tweet would be “@listensto Metallica – One“. Then, it creates an Audiotwit page for you at (username).audiotwit.com.  In my case this was mrheadrick.audiotwit.com. It then adds a Lyrics link next to the song to look-up the song’s lyrics.

Winamp Now PlayingSince I was using the Winamp Now Playing Plug-In version 2.1.2 together with a PHP script I wrote to generate now-playing images and a song history on my entertainment page, I wondered if there was a Winamp Twitter plug-in and there was one to my amazement. While it did work, it had problems dealing with some characters, such as “&” where it would just chop-off everything after the “&”.  The RSS feed of the audiotwit page is also a bit buggy.  “&” causes problems with the feed and the feed itself does not validate with Feedvalidator.org.  During all this testing, I decided I better create another twitter account just for this purpose and keep all the “now listening to” spam out of my main twitter account.  (Update 8/11/2010: @mrheadrick_np was the account I made at the time; however, it got flagged to not show in searches and hasn’t been used in months and has been deleted. The audiotwit page at mrheadrick_np.audiotwit.com still exists for the time being.)

During this time I had also discovered that many others used the phrase “listening to” to announce what they were listening to. There’s even a search link at the Twitter search results page for this phrase. At this point I decided to stop using the Winamp Twitter plug-in which was a bit limiting and use my Winamp PHP script to send a tweet via the Twitter API using both “@listensto” and “listening to” methods.  After some searching around, I used the improved script by Scott Sloan I found here and adapted it to my own needs.

So now, mrheadrick_np.audiotwit.com and this “listening to” search will show what I am playing. The feed you see in my blog is the feed that the “listening to” search produces, which does not have problems with “&” and other characters being in the artist and song titles.

Twitter

I also added a Twitter widget to my main website. I used the HTML version and modified the CSS to create the appearance I wanted.

Having done all this, does anyone else really care what I’m doing or have been up to or what the most recent songs I’ve listened to are? Beyond family and close friends (most of which do not use Twitter), there might be a few, such as those from Surmunity, but I suspect most could care less. Several of the people that are following me on Twitter I think are doing so more to increase their own page ranks and points of entry for the various search bots to find their websites and content than to really follow what I am doing or saying. Some strangers that follow me, especially those with goobldygook usernames, are clearly doing so for spamming purposes. These I block when I come across them and many of these type of accounts are usually suspended by Twitter. Even so, I still think Twitter is kind of cool. 🙂