Posts Tagged ‘PHP’

memo: install nginx on ubuntu and use sock

October 9th, 2013

$ sudo apt-get install nginx
$ sudo apt-get install php5 php5-fpm ...

edit /etc/php5/fpm/pool.d/www.conf, and chenge
listen = 127.0.0.1:9000
to
listen = /var/run/php-fpm.sock

problem with multi fastcgi_finish_request requests in nginx

May 1st, 2013

The php server nginx has a very handy function fastcgi_finish_request(). If fastcgi_finish_request is called in a function, the function will return immidiately but still do the job in background. So it is intuitive to think of using fastcgi_finish_request() to do some jobs in parallel when needed.

However, when a process call the fastcgi_finish_request() many times in a row, the later calls have to wait for the previous call to finish. So it’s not such a big help when you call fastcgi_finish_request() many times in a row, because all these requests will be executed in sequence. Rather you should put all the long calls in a fastcgi_finish_request() call.

For example:

function a() {
     long_work(); // 1st call
     // if we write a log here, we'll find 1st call immediately returns.
     long_work(); // 2nd call
     // if we write a log here, we'll find this 2nd call will have to wait for 10s before it returns.
}

function long_work() {
    sleep(5);
    fastcgi_finish_request();
}

A workaround to mimic multithread with fastcgi_finish_request() is to use curl. We put the long work in a php file, and then use curl to request the php file.

// ------ long_work.php ----------
long_work();
function long_work() {
    sleep(5);
    fastcgi_finish_request();
}


And then in function a(), we can request long_work many times.

function a() {
    // curl( 'http://localhost/long_work.php' ); // 1st call
    // curl( 'http://localhost/long_work.php' ); // 2nd call
}

ThinkPHP cofigure (配置文件)

April 25th, 2013

* 在首页定义 define(‘APP_DEBUG’, TRUE); 的情况下:
在Application/Conf/config.php中 定义 ‘APP_STATUS’ => ‘debug’, 或者 ‘APP_STATUS’ => ‘production’, 会加载Application/Conf/debug.php 或者 在Application/Conf/production.php. 在这两个文件中可以定义配置覆盖config.php中的配置。

* 在首页定义 define(‘APP_DEBUG’, FALSE); 的情况下:
Application/Conf/config.php中定义’APP_STATUS’无效, 因此debug.php和production.php都不会加载.
系统只会将config.php编译至 Application/Runtime/~runtime.php 中.

PHP Hash Chinese Character or String

April 22nd, 2013

Hash a Chinese character or string into a integer number. This might be useful when one wants to split a huge table which is indexed by Chinese strings horizontally into many tables.

	/**
	 * Hash a chinese charactor into an int number.
	 * @param string $c A chinese character
	 * @return number
	 */
	public static function hashZhChar($c) {
		return (ord(substr($c, 0 , 1)) -176)*94 + ord(substr($c, 1, 1)) - 161;
	}
	/**
	 * Hash a chinese string into an integer number.
	 *
	 * @param string $s A chinese string.
	 * @return number
	 */
	static function hashZh($s) {
		$first = mb_substr($s, 0, 1, 'UTF-8');
		$last = mb_substr($s, -1, 1, 'UTF-8');
		$middle = mb_substr($s, intval(mb_strlen($s, 'UTF-8')/2), 1, 'UTF-8');
		return self::hashZhChar($first) + self::hashZhChar($last) + self::hashZhChar($middle);
	}
	

How to install MemCache on Windows 7 X64 for PHP with WAMP

August 22nd, 2011

Basics

The memcache sonsists of 2 parts: the server MemCached and the client MemCache.

  • MemCached is the cache server, the same as Mysqld (mysql server), which save and feed cache to clients.
  • MemCache is a client. With php, we need to install it as a mod, just the same as pdo_mysql (php_pdo_mysql.dll on windows).

Install MemCached

  • Download memcached from http://code.jellycan.com/memcached/ (get the win32 binary version). Here we suppose you extract the memcached in d:\memcached\.
  • If you are on Sindows Vista/7, rightclick on memcached.exe and select Properties; click on the Compatibility tab. At the bottom you’ll see Privilege Level, check “Run this program as an administrator”.
  • Open the command line by Win + R (or Start -> All Programs -> Accessories -> Command Prompt), and then run: d:\memcached\memcached.exe -d install to install the service.
  • Still in the command line, run d:\memcached\memcached.exe -d start, or net start "memcached Server" to start the service.
  • Check whether the service is running. Open Windows Task Manager, go to Services tab, you should find status of “memcached server” is “Running”.

Install MemCache php module

Note

  • To change MemCached memory size (default is 64mb), go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to:
    “C:\memcached\memcached.exe” -d runservice -m 512
  • More details see: memcached -help

SQLSTATE[HY000]: General error: 2053 in ZF

May 15th, 2011

After updating the PDO, my program began to see some weird problems. One of them is “SQLSTATE[HY000]: General error: 2053″. After some time,  I finally found that it is because I passed NULL to the second parameter ($bind) of Zend_Db_Adapter_Abstract::fetchAll($sql, $bind = array(), $fetchMode = null).

So, instead of this:

$popList = $this->getAdapter()->fetchAll($sql, null, Zend_Db::FETCH_OBJ);

one should:

$popList = $this->getAdapter()->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);

when you don’t need to bind any parameter with the SQL.

show / download image with PHP

March 9th, 2010

To show a image in browser:

$file_path = ICON_DIR . $_GET['f'];
// TODO check file_path security
$file_name = basename($_GET['f']);
$fileLineArray = file($file_path);
header(‘Content-Type: image/jpeg’);
header(“Content-Disposition:filename=$file_name”);
foreach($fileLineArray as $fileLine)
echo $fileLine;

To force the browser to show the download dialogue, add a attachment header:

header(“Content-Disposition:filename=$file_name”);
header(“Content-disposition: attachment; filename=$file_name”);

php session operations

February 26th, 2010

* get session id: session_id(), but remember to session_start() before getting the id.

* session_decode($data), this will decode the data and store them into $_SESSION.

reference: http://www.php.net/manual/en/ref.session.php