RSS
 

Archive for the ‘PHP’ Category

30 Second Delay with PHP and Memcache Sessions

02 Feb

We upgraded the servers at NeatoShop.com and Neatorama.com a couple of weeks ago, and in the past week we started noticing and receiving reports that on “some” page loads there was a 30-second delay where the page seemed to hang before finally returning any content.

The common things we could put a finger on were 1) it would seem to happen with a raw/initial page load (e.g., in a brand new incognito window), 2) it would not hang up anymore after that first page load, and 3) it seemed to happen only if we loaded neatoshop.com which was then redirected to www.neatoshop.com.

It took a ton of code profiling and countless attempts to reproduce the problem (because it didn’t happen for me on every new incognito page load), but I finally traced the delay down to session_start(). This didn’t really make sense to me because we’ve used php5-memcache (obviously with Memcached) for handling sessions with redundancy across our 7 web servers, and nothing regarding sessions nor Memcached changed on our end any time recently. So what could the problem be?

Read the rest of this entry »

 
No Comments

Posted in Bugs, PHP

 

Generate UUID in PHP

23 Feb

I posted this last year to PHP.net and thought I’d just share it here as well. It’s a short write-up and demonstration of a PHP class you can use to generate various versions of a Universally-Unique Identifier, or UUID.

The php5-uuid functions could definitely use some documentation to clarify how they should be used, but here’s what I’ve gleaned by examining the OSSP source code (found here: http://ossp-uuid.sourcearchive.com/documentation/1.5.1-1ubuntu1/php_2uuid_8c-source.html).

The uuid_make() function takes two arguments when generating v1 or v4, but four arguments are required when generating v3 or v5. The first two arguments have been thoroughly demonstrated and are straightforward, so I’ll skip to the as-yet non-described arguments.

Read the rest of this entry »

 
3 Comments

Posted in PHP

 

Text Beautify WordPress Plugin

09 Feb

I’ve just submitted my first ever plugin to the WordPress repository. It’s called Text Beautify and can be found at the following URL:
http://wordpress.org/extend/plugins/text-beautify/

The purpose of the plugin is to beautify post titles, post contents, and comments of your WordPress blog. In post contents and comments, the text is made sentence case. In post titles, it is made title case, so all words are capitalized except for a user-defined list of exceptions, such as: of, an, the, etc. It is really targeted at persons with a refined sense of text aesthetics and people who just want their content to look nicer.

Read the rest of this entry »

 
 

PHP Human File Size Function

19 Nov

Just found myself needing to display bytes in a human readable format. There are countless examples that use a lot of complex techniques to achieve this, but I wanted a simpler solution. This is the method that came to mind. Rather than basing my algorithm on the numeric value, it is based on the length of the bytes.

Read the rest of this entry »

 
1 Comment

Posted in PHP

 

PHP Uppercase Sentences

14 Nov

I’ve never really had a need to write code to uppercase only sentences in a string, but I now am dealing with large blocks of text content that is often written all uppercase by the author. So I popped on over to the PHP manual. I was pretty certain no such function existed in the language, but thought I’d check if one was added in a newer version.

Alas, no such function existed, so I wrote one myself Read the rest of this entry »

 
2 Comments

Posted in PHP

 

PHP 5.3 Class Friendship Support

23 Apr

One of the useful features of OOP languages like C++ is class friendship via the friend keyword. What this allows you to do is define a class that permits other explicitly named classes to touch its private parts.

For an overly simple example, let’s say you have a User class that carries its user info privately, but you also have a Logger class for writing error and info message to a log. If you tried the following, you’d hit errors telling you that the User properties “id” and “nick” are private:

class User {
  private $id;
  private $nick;
  // ...
}

class Logger {
  // ...
  public static function write($str) {
    $user = User::get_current_user();
    fwrite(self::$handle, "User: {$user->nick} ({$user->id}): {$str}\n");
  }
}

In C++ you could add a simple line to class User like:

friend class Logger;

This would permit the Logger class to access any of its private or protected properties and methods. Despite there being numerous people who demand that you should redesign your code instead of using class friendship, there are many cases where friendship is not only useful but necessary. This is all good and fine except for the fact that the PHP language does not have support for friend classes, nor do its developers have any intention of adding it (the last I heard from the PHP team is they long ago decided against such functionality).

This led me to develop a workaround, which wouldn’t have been possible in the past, but with the flexibility of PHP5.3 and late-static binding, it is now possible. So, without further ado, here’s my implementation of class Friendship.
Read the rest of this entry »

 
No Comments

Posted in PHP

 

PHP 5.3 Dynamic Namespace Resolution

10 Apr

As PHP continues evolving, it just keeps getting better and better. One of the features added in version 5.3 is support for namespaces. If you’ve started trying to actually use PHP namespaces, you’ve probably run into some of the more obvious quirks that require you to either retrofit your code or implement a workaround. I started with the former before realizing that thanks to the namespace feature itself, a simple set of workarounds could do the job.

Specifically, I’m referring to the fact that namespace resolution happens at compile time, which means that if you attempt to reference a namespaced class or method as a string, it won’t be recognized unless that string explicitly includes the namespace name. The most apparent case is when calling class_exists(). For example, let’s say you define class Settings in namespace OssumCMS:

namespace OssumCMS;

class Settings {
  const MEMCACHED_ADDR = 'none';
  public static function do_something() {
  }
}

In your code, you might want to do some things like:

// ...
if (class_exists('Settings'))
  if (@constant('Settings::MEMCACHED_ADDR') != 'none')
    call_user_func(array('Settings', 'do_something'));

But that would never work because class Settings is defined within namespace OssumCMS. PHP resolves namespaces at compile time, so when class_exists(), constant(), and call_user_func() are called at run time, they won’t ever find class Settings because it’s checking only the global namespace. Read the rest of this entry »

 
2 Comments

Posted in PHP

 

PHP Shared Session Encoding Solution

06 Feb

I came up against a really baffling problem the other day. I was tasked with adding a Wiki for VideoSift. Rather than just dumping MediaWiki onto the primary VideoSift web server, we wanted to keep it self-contained for security and load reasons, so we installed into one of our other back-end servers which was being used as a MySQL Slave and not hosting any web content. To start with I just did a quick, basic install of Apache2 and PHP5 (I would prefer and attempted to use Nginx with PHP5-FPM, but MediaWiki complains about implementation bugs in the packaged versions available to me and I don’t want to recompile from source).

Read the rest of this entry »

 
9 Comments

Posted in PHP