PHP

Lightweight, but Powerful PHP5 XML-RPC Server

I have written a XML-Server in PHP5.
It is very easy to use. Just include the file and run the server.
It has some restrictions, it accepts only functions and static methods. you don't have to to change your code if your methods return a php type. (string, array, integer)
Example

<?php
$ws 
= new XML_RPC_Server();
$ws->registerFunction('getTime');
$ws->registerMethod('Product::getPrice');
$ws->registerMethod('Product::getDetail');
$ws->registerMethod('Product::getBinaryPicture');
$ws->run();
?>

Example Source,
http://www.pure-php.com/php/service.source.php
The Server
http://www.pure-php.com/php/xmlrpc.php.txt

add new comment | read more

Security matters, specially for PHP-Magazine, some source code publicly accessible!


PHP-Magazine has fixed this secutrity leak, it is not possible to read any source code. This posting ist not up to date anymore

------------------------------------------------------
It is not an April joke! I read frequently PHP-Magazine (http://php-mag.net http://php-mag.de), Yesterday I discovered some interesting things, because I was able, to read some source code and they are still publicly available. I don’t want to write, how I could read the source code, because there are always some people out side, who want play with it. Very important, they have register_globals on. I hope, they fix it as soon as possible.

add new comment | read more

include_once / require_once Wrapper Class optimized

When I posted my Idea of building a wrapper class for include_once and require_once, I didn't think, that the many php developer would like it. But I was wrong, fortunately.

Thanks to Mathias Taylor for benchmarking. Special thanks goes to Cristian Strian for his suggestion to optimize the class, and make it faster. That is the optimized wrapper class.

Usage:

<?php
require("includeWrapper.class.php");
includeWrapper::includeOnce("test.php");
includeWrapper::includeOnce("test.php");
includeWrapper::includeOnce("test.php");
print_r(includeWrapper::getPaths());
?>

Output:
Array
(
[test.php] => 1
)

The class itself.

<?php
class includeWrapper{
    public static 
$paths = array();
    public static function 
includeOnce($path_file){
        if(!isset(
self::$paths[$path_file])){
            include(
$path_file);
            
self::$paths[$path_file] = true;
        }
    }
    public static function 
requireOnce($path_file){
        if(!isset(
self::$paths[$path_file])){
            require(
$path_file);
            
self::$paths[$path_file] = true;
        }
    }
    
// just for testing
    
public static function getPaths(){
        return 
self::$paths;
    }
}
?>

2 comments

Fatal Error is the Fatal Error of PHP - How to avoid it

One of the disadvantages of PHP, and one of the things I don't like about the php, is the fatal error, because there isn't any way to avoid it. Well PHP is actually thought to be used in web environment, therefor it is not so important, if your script causes a fatal error, then only the execution of the script for a single request would be terminated.

In PHP command line apps, it is really fatal, if your app causes a fatal error, because you have to restart your app. Well you can avoid this, if you write your app very carefully. You should always use if(function_exists("a_func")) and if(method_exists("a_method")). Now you can be pretty sure, that the script won't cause any fatal error.
If you dynamically load third party modules in your app, you have a real problem, because the included module could cause a fatal error, your app would be terminated. There is on way to avoid it. Use call_user_func. You can replace in third party modules every function call and method call with call_user_func. For example.

<?php
function add($a,$b){
 echo 
$a+$b;
}
// call the function with a small mistake
ad(2,3); // causes a fatal error
//replace it with
call_user_func("ad",2,3); // causes a warning
?>

add new comment

PHP Command Line - Avoid several Instances of your App.

Since PHP 4, you can also write command line apps in php. If you have a critical app, and you want to avoid several, or mor than one instance of your app, to run, you can use the following class. It works only on unix and linux operating sytems.

Usage:

<?php
inlude
("ProcessHandler.class.php");
if(
ProcessHandler::isActive()){
   die(
"Already running!\n");
}else{
   
ProcessHandler::activate();
   
//run my app
}
?>

3 comments | read more

include_once Wrapper Class

There hase been some discussion in the recend days about the performance of include_once. Inlude_once is really slower than include. For the inclusion of the classes you can use
class_exists('PEAR') or require_once 'PEAR.php' (www.akbkhome.com/blog...);

You can also use this wrapper class for include_once and require_once, it is faster.

<?php
class includeWrapper{
    private static 
$paths = array();
    public static function 
includeOnce($path_file){
        if(!
in_array($path_file,self::$paths)){
            include(
$path_file);
            
self::$paths[] = $path_file;
        }
    }
    public static function 
requireOnce($path_file){
        if(!
in_array($path_file,self::$paths)){
            require(
$path_file);
            
self::$paths[] = $path_file;
        }
    }
}
includeWrapper::includeOnce("Class1.class.php");
includeWrapper::requireOnce("Class1.class.php");
includeWrapper::includeOnce("Class2.class.php");
?>

10 comments

PHP Command Line rocks

Recently I wrote a PHP Command line app, I was amazed, how easy it was, to write command line app in PHP, and it really rocks.

Here is very simple Command Line handler class, it may be usefull for your apps.

<?php
class CliHandler{
    private 
$in;
    private 
$out;
    private 
$handler;
    public function 
CliHandler($handler){
        
$this->in fopen("php://stdin","r");
        
$this->out fopen("php://stdout""w");
        if(
is_object($handler)){
            
$this->handler $handler;
        }
    }    
    public function 
run(){
        
$str "Try these commands:\n"
        
.implode("\n",get_class_methods($this->handler));
        
$this->out($str);
        while(
$line rtrim(fgets($this->in1024))){
            if(
method_exists($this->handler,$line)){
                
$out $this->handler->$line();
                if(
$out){
                    
$this->out($out);
                }
            }
        }
    }
    public function 
out($str){
        
fwrite($this->out,$str."\n");
    }
}
class 
AnyClass{
    public function 
start(){
        return 
"started";
    }
    public function 
stop(){
        return 
"stoppded";
    }
}
$cli = new CliHandler(new AnyClass());
$cli->run();
?>

CliHandler accepts any class als argument.
Try this.

/usr/local/php/PHP5 CliHandler.class.php
output: Try these command:
start
stop
enter "start"
output: started

1 comment

PHP must free itself from Apache Server

There has been a lot of Discussions about Enterprise PHP, specially after the release of PHP 5. PHP has a much better Object Model now, butt PHP is still NOT enterprise ready.

The first step toward enterprise is PHP's emancipation from Apache Web server. Since 4.1.0 Version, PHP supports sockets. Therefore PHP itself can serve HTTP Requests. Although PHP sockets are experimental, but it really rocks. I have written a simple IRC Sever in PHP using PHP sockets and it is up since about 70 days.

5 comments | read more

Pure-PHP goes online

Pure-PHP goes online

add new comment
Syndicate content