Skip to main content

Posts

Showing posts from October, 2009

Pop Quiz

Here's a little test to separate the serious coders from the cut-and-paste script kiddies. Given the need to generate an arbitrarily long string consisting of random alpha-numeric characters, which solution is best? Solution A: function randomString($len) { $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" . "abcdefghijklmnopqrstuvwxyz" . "0123456789"; $rndMax = strlen($chars) - 1; $str = ""; while ($len-- != 0) { $str .= $chars[rand(0, $rndMax)]; } return $str; } $str = randomString(8); echo "$str\n"; Solution B: class RandomSequenceIterator implements Iterator { protected $seqMembers; protected $key; protected $limit; public function __construct() { $this->setMembers(null) ->setLimit(0) ->rewind(); } protected function setMembers($strValue) { $this->seqMembers = $strValue; return $this; }...

Setting Up SFTP From PHP

PHP's ftp_ssl_connect() function is for SSL-FTP, where as what I need for a client's application is SFTP. Isn't life grand! Well, it's not really too much trouble... PHP can handle that too with functions from the ssh2 PECL extension. I'm just glad I caught it early on instead of at the 11th hour. I figured though I might as well continue my previous post about setting up this project with a brief description on installing ssh2 and testing it to ensure everything is in working order. Installation The ssh2 extension provides bindings to libssh2 which must be installed first on the system. My target platform is CentOS 5.3, so I was able to install libssh2 and libssh2-devel via yum (using the RPMForge repository). yum install libssh2 libssh2-devel The ssh2 extension is available from the pecl.php.net website . There is a minor bug in the current version (0.11.0) which prevents it from compiling against PHP 5.3 so I needed to apply this patch . wget http://remi.fed...