Changed to worker mpm

Today I changed my server to from prefork mpm to worker mpm.

There are only some issues: in my phpmyadmin I had to set auth from http to cookie. And PHP_ADMIN_VALUE don’t work in the vhosts.

How to install it:

sudo apt-get install apache2-mpm-worker libapache2-mod-fcgid

In the single vhosts

Options Indexes ExecCGI
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php

So that isn’t hard to do.

Well I would like to have a windows server with apache which is also threaded like worker mpm, but there is no cheap hoster for that yet. At home have that server is not an option cause of the small upload I have with my DSL, the coast of energie and where the hell should I put that server in my small apartment to that I still can sleep?

PHP benchmark class

< ?php
/**
 * benchmark
 * @access public
 * @var $timing_start_times float
 * @var $timing_stop_times float
 */
class benchmark{

	public $timing_start_times;
	public $timing_stop_times;


	/**
	* benchmark::timer_start()
	* @access public
	* @param string $name
	* @return int
	*/
	public function timer_start($name = 'default'){
		$this->timing_start_times[$name] = explode(' ', microtime());
	}

	/**
	* benchmark::timer_stop()
	* @access public
	* @param string $name
	* @return
	*/
	public function timer_stop($name = 'default'){
		$this->timing_stop_times[$name] = explode(' ', microtime());
	}

	/**
	* benchmark::timer_result()
	* @access public
	* @param string $name
	* @return int
	*/
	public function timer_result($name = 'default'){
		if (!isset($this->timing_start_times[$name])) {
        	return 0;
    	}
    	if (!isset($this->timing_stop_times[$name])) {
        	$stop_time = explode(' ', microtime());
    	}
    	else
		{
        	$stop_time = $this->timing_stop_times[$name];
    	}
	    // do the big numbers first so the small ones aren't lost
	    $current = $stop_time[1] - $this->timing_start_times[$name][1];
	    $current += $stop_time[0] - $this->timing_start_times[$name][0];
	    return $current;
	}

}
?>

example code

< ?php
require_once "benchmark.class.php";
$bm = new benchmark();
?>

Test Inline Tags vs echo

< ?php $bm->timer_start('echo'); ?> < ?php for ($i=0; $i<1000; $i++) { echo $i."
"; } ?> < ?php $bm->timer_stop('echo'); ?>

< ?php $bm->timer_start(str); ?> < ?php $str = ''; for ($i=0; $i<1000; $i++) { $str .= $i."
"; } echo $str; ?> < ?php $bm->timer_stop(str); ?>

< ?php $bm->timer_start(inline); ?> < ?php for ($i=0; $i<1000; $i++) { ?> 123
< ?php } ?> < ?php $bm->timer_stop(inline); ?>


Result

echo - < ?php echo $bm->timer_result('echo'); ?>

str - < ?php echo $bm->timer_result(str); ?>

inline - < ?php echo $bm->timer_result(inline); ?>

remove /MACHINE:X86 from make files with php

I use PHP on the command line cause I know it the syntax better than any other scriting language and it works on linux and windows. This is about removing the x86 to be able to build apache in x64 (64 bit) on windows.

<?php
$files=array(
 "srclib/apr/libapr.mak",
 "srclib/apr-iconv/build/modules.mk.win",
 "srclib/apr-iconv/libapriconv.mak",
 "srclib/apr-util/dbd/apr_dbd_freetds.mak",
 "srclib/apr-util/dbd/apr_dbd_mysql.mak",
 "srclib/apr-util/dbd/apr_dbd_odbc.mak",
 "srclib/apr-util/dbd/apr_dbd_oracle.mak",
 "srclib/apr-util/dbd/apr_dbd_pgsql.mak",
 "srclib/apr-util/dbd/apr_dbd_sqlite2.mak",
 "srclib/apr-util/dbd/apr_dbd_sqlite3.mak",
 "srclib/apr-util/dbm/apr_dbm_db.mak",
 "srclib/apr-util/dbm/apr_dbm_gdbm.mak",
 "srclib/apr-util/ldap/apr_ldap.mak",
 "srclib/apr-util/libaprutil.mak"
);
foreach($files as $file){
 if(file_exists($file)){
 echo "Replace in: ". $file ."\n\r";
 file_put_contents($file,str_ireplace(" /MACHINE:X86","",file_get_contents($file)));
 }
 else
 {
 echo "FAILED: ". $file ."\n\r";
 }
}
?>

PHP age calculator / Alter berechnen

<?php
/**
 * age_calculator()
 *
 * @param int $day DD
 * @param int $month MM
 * @param int $year YYYY
 * @return int $age years
 */
function age_calculator($day,$month,$year){
	if(date("n") > $month){
		$age = (date("Y") - $year);
	}
	elseif(date("n") == $month){
 		if(date("j") < $day){
			$age = (date("Y") - $year) - 1;
		}
 		else
		{
		 	$age = (date("Y") - $year);
		}
 	}
	else
	{
		$age = (date("Y") - $year) - 1;
	}

	return $age;
}
?>

PHP crypt command line

crypt

#!/usr/bin/php
< ?php
require "crypt.php";

$type = $argv['1'];
$string = $argv['2'];
$key = $argv['3'];
if($type !='' &&  $string != '' && $key != ''){
        if($type=="e"){
                echo encrypt($string,$key);
                echo "\n";
        }
        elseif($type=="d")
        {
                echo decrypt($string,$key);
                echo "\n";
        }
        else
        {
                die('WRONG TYPE');
        }
}
else
{
        echo 'crypt TYPE STRING KEY';
        echo "\n\n";
        echo "TYPE:\n";
        echo "e encrypt\n";
        echo "d decrypt\n";
        echo "\n\n";
        echo "STRING Your string\n";
        echo "KEY Crypt key\n\n";
}
?>

crypt.php

< ?php
/**
 * encrypt()
 *
 * @param mixed $string
 * @param mixed $key
 * @return mixed $retrun
 */
function encrypt($string, $key){

        $result = '';
        $lentgh = strlen($string);
        for($i = 0; $i < $lentgh; $i++) {
                $char = substr($string, $i, 1);
                $keychar = substr($key, ($i % strlen($key))-1, 1);
                $char = chr(ord($char) + ord($keychar));
                $result .= $char;
        }

        return base64_encode($result);
}

/**
 * decrypt()
 *
 * @param mixed $string
 * @param mixed $key
 * @return mixed $return
 */
function decrypt($string, $key){

        $result = '';
        $string = base64_decode($string);
        $lentgh = strlen($string);

        for($i = 0; $i < $lentgh; $i++) {
                $char = substr($string, $i, 1);
                $keychar = substr($key, ($i % strlen($key))-1, 1);
                $char = chr(ord($char) - ord($keychar));
                $result .= $char;
        }

        return $result;
}
?>

PHP cache class

< ?php
/**
*
* @var $cache_file_name string file name
* @var $age int age in seconds
*/
class cache {

	public $cache_file_name;
	public $age;

	/**
	* cache::__construct()
	* @access public
	*/
	public function __construct(){
		$this->cache_start();
		register_shutdown_function(array($this, "cache_end"), "inside");
	}

	/**
	* cache::__descruct()
	* @access public
	* @return
	*/
	public function __descruct(){
		$this->cache_end();
	}

	/**
	* cache::cache_start()
	* @access public
	* @return
	*/
	public function cache_start(){

		global $cache_file_name, $age;
		$cache_file_name = 	$_SERVER["DOCUMENT_ROOT"].$_SERVER['REQUEST_URI'] . '_cache';

		if (empty($age)){
			$age = 600;
		}

		if(file_exists($cache_file_name)){
			if (filemtime($cache_file_name) + $age > time()) {
				readfile($cache_file_name);
				unset($cache_file_name);
				exit;
			}
		}
		ob_start();
	}

	/**
	* cache::cache_end()
	* @access public
	* @return
	*/
	public function cache_end()
	{
		global $cache_file_name;
		if (empty($cache_file_name)){
			return;
		}
		$str = ob_get_clean();
		echo $str;
		fwrite(fopen($cache_file_name . '_tmp', "w"), $str);
		rename($cache_file_name . '_tmp',$cache_file_name);
	}

}

?>

Posts Tagged php

Archives by Month: