my life

day to day

Archive for the 'coding' Category

Forcing Wordpress to use HTTP_HOST

Wednesday, November 21st, 2007

At times, it is convenient to force Wordpress to use the hostname of the current virtual host context. By default, new versions of wordpress rely on DB configured values for siteurl, home, and url. If for instance, you run multiple codebases for testing, it might be convenient to not have it force the values to the configured ones. To accomplish this, add the following code to a custom wp plugin or just add it to the wp-config.php at the very bottom.



function set_codebase()
{
return 
'http://' $_SERVER['HTTP_HOST'];
}
remove_action('template_redirect''redirect_canonical');
add_filter 'pre_option_siteurl''set_codebase' );
add_filter 'pre_option_home''set_codebase' );
add_filter 'pre_option_url''set_codebase' );


This code can further be wrapped in a conditional so it only gets executed under explicit circumstances.

add to del.icio.us    add to technorati favs   email this

Using strtotime(…) in PHP to calculate the last day of the month

Wednesday, August 15th, 2007



$unixtime 
strtotime(strftime('%Y-%m-01 - 1 second',
strtotime('next month')));

add to del.icio.us    add to technorati favs   email this

VIM Not Restoring Position

Friday, August 10th, 2007

For some reason, vim stopped recording the last position in the files I was editting. I think it is related to a recent upgrade. Regardless, adding these lines to my ~/.vimrc restored the functionality



set viminfo
='10,\\"100,:20,%,n~/.viminfo
    au BufReadPost * if line("'""
) > 0|if line("'\\""

add to del.icio.us    add to technorati favs   email this

Robust PHP MogileFS Client

Saturday, June 30th, 2007

We’ve just released our MogileFS Client for PHP (5.1). It does a very good job of handling errors and is being used in production on Socialverse.

You might also want to consider this PECL module that implements the protocol. Personally, I don’t see many performance advantages to using this module. The native PHP MogileFS class above uses CURL for almost everything, which is an efficient library for HTTP communication all in C. CURL also comes standard with almost every PHP packing system that I’ve seen, so you don’t need to maintain yet another PECL.

add to del.icio.us    add to technorati favs   email this

PHP Exception Throwing Eval

Wednesday, June 13th, 2007



  
function eeval($string)
  {
    $result = @eval($string);
    if( isset($php_errormsg) )
      throw new Exception($php_errormsg);
    return $result;
  }

add to del.icio.us    add to technorati favs   email this

PHP CURL PUT String

Wednesday, June 13th, 2007

In the process of writing a MogileFS client in PHP, I discovered there’s no no straight forward way to issue a PUT request using CURL where the body of the request comes from a string, rather than a file. Luckily, PHP 5.2 introduces memory based streams…

 

   $fh 
fopen('php://memory''rw');
    fwrite($fh$dataToPut);
    rewind($fh);
   $ch curl_init();
    curl_setopt($chCURLOPT_INFILE$fh);
    curl_setopt($chCURLOPT_INFILESIZE$length);
    curl_setopt($chCURLOPT_TIMEOUT10);
    curl_setopt($chCURLOPT_PUT4);
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_HTTPHEADER, Array('Expect: '));
    $response curl_exec($ch);
    fclose($fh);

add to del.icio.us    add to technorati favs   email this

PHP set_error_handler wierdism with $this

Friday, June 16th, 2006

Note, if you are setting an error handler for a class method within your own object, such as
Array( $this'error_handler')
you will effectively make your current object global in scope. Thus, it will only pass out of scope when the script exits or the object manually unset. As a result the deconstructor of your object will not be called. This can be a problem if in your deconstructor you call restore_error_handler.

add to del.icio.us    add to technorati favs   email this