+# Test a memcached server
+function testMemcachedServer( $server ) {
+ $hostport = explode(":", $server);
+ $errstr = false;
+ $fp = false;
+ if ( !function_exists( 'fsockopen' ) ) {
+ $errstr = "Can't connect to memcached, fsockopen() not present";
+ }
+ if ( !$errstr && count( $hostport ) != 2 ) {
+ $errstr = 'Please specify host and port';
+ var_dump( $hostport );
+ }
+ if ( !$errstr ) {
+ list( $host, $port ) = $hostport;
+ $errno = 0;
+ $fsockerr = '';
+
+ $fp = @fsockopen( $host, $port, $errno, $fsockerr, 1.0 );
+ if ( $fp === false ) {
+ $errstr = "Cannot connect to memcached on $host:$port : $fsockerr";
+ }
+ }
+ if ( !$errstr ) {
+ $command = "version\r\n";
+ $bytes = fwrite( $fp, $command );
+ if ( $bytes != strlen( $command ) ) {
+ $errstr = "Cannot write to memcached socket on $host:$port";
+ }
+ }
+ if ( !$errstr ) {
+ $expected = "VERSION ";
+ $response = fread( $fp, strlen( $expected ) );
+ if ( $response != $expected ) {
+ $errstr = "Didn't get correct memcached response from $host:$port";
+ }
+ }
+ if ( $fp ) {
+ fclose( $fp );
+ }
+ if ( !$errstr ) {
+ echo "<li>Connected to memcached on $host:$port successfully";
+ }
+ return $errstr;
+}