Submit: Tips | News

Tutorials & Articles

Penggunaan PHP Accelerator

Tergerak menggunakan PHP accelerator menyebabkan saya mencuba pindah turun dari http://www.php-accelerator.co.uk/ dan memasangnya di 'development server'.
Proses pemasangan amat mudah. Pindah turun binari dari halaman web dan letakkan file tersebut di server. Cadangan dari dokumentasi meletakkan di /usr/local/lib

[/b]
Spesifikasi Server :
Fedora Core 4
Apache 1.3.33 / PHP 4.4.0
[/b]

Pindah turun dari http://www.php-accelerator.co.uk/download.php untuk PHP 4.4.0 iaitu 1.3.3r2.

Cara pemasangan
Edit di php.ini (cara nak tau mana lokasi buat phpinfo() ) dan letakkan

; PHP Accelerator extension
zend_extension="/usr/local/lib/php_accelerator_1.3.3r2.so"

di mana-mana tempat di php.ini

Bagaimana saya mengukur sebelum dan selepas pemasangan php accelerator ?

Menggunakan fungsi PHP yang biasa letakkan dia di atas sekali skrip

function getmicrotime()
{
  list($usec, $sec) = explode(" ",microtime()); 
  return ((float)$usec + (float)$sec); 
} 
$script_start = getmicrotime();

dan letakkan ini di bawah sekali skrip

echo sprintf("%.4f",(getmicrotime() - $script_start));

Saya menggunakan PHPCollab, satu skrip Project Management untuk mengukur kelajuan PHP Accelerator di http://www.php-collab.org

Hasilnya

Tanpa php accelerator

PhpCollab v2.5 - Connected users: 1 - 0.092 secondes - databaseType mysql - select requests 20 - w3c (in progress)
PhpCollab v2.5 - Connected users: 1 - 0.091 secondes - databaseType mysql - select requests 20 - w3c (in progress)
PhpCollab v2.5 - Connected users: 1 - 0.097 secondes - databaseType mysql - select requests 20 - w3c (in progress)
PhpCollab v2.5 - Connected users: 1 - 0.094 secondes - databaseType mysql - select requests 20 - w3c (in progress)

Dengan php accelerator

PhpCollab v2.5 - Connected users: 1 - 0.165 secondes - databaseType mysql - select requests 20 - w3c (in progress)
PhpCollab v2.5 - Connected users: 1 - 0.062 secondes - databaseType mysql - select requests 20 - w3c (in progress)
PhpCollab v2.5 - Connected users: 1 - 0.061 secondes - databaseType mysql - select requests 20 - w3c (in progress)
PhpCollab v2.5 - Connected users: 1 - 0.061 secondes - databaseType mysql - select requests 20 - w3c (in progress)

Beza kali pertama, menggunakan PHP Accelerator agak perlahan berbanding tanpa PHP Accelerator, tapi untuk akses halaman yang sama kali kedua, lebih kurang penjimatan sebanyak 0.03 saat. Sungguh baik !!. Untuk orang lain ? Saya juga mencadangkan penggunakan PHP Accelerator untuk halaman web anda.

Di syorkan Wink

Setting Up CVS Server On Redhat

Artikel ini ditulis dalam bahasa Inggeris supaya dapat di munafaatkan oleh lebih banyak pembaca. Jika anda ingin menterjemahkan kepada bahasa Melayu, sila email saya di amran (at) php.net.my .

Assumptions: -

Redhat 9 -
Repository is in /home/cvs/cvsroot - You want to update remotely to the server
Software: cvs-1.11.2-10 or any

To check wether you have this cvs install

root@localhost# rpm -qa | grep cvs cvs-1.11.2-10 Setting Up the server
a. You need to set one user for running cvs user, login as root

root@localhost># useradd cvs
b. Redhat 9 using xinetd for the tcp connection.

You need to
1. Edit /etc/services
cvspserver 2401/tcp # CVS client/server operations
cvspserver 2401/udp # CVS client/server operations

Uncomment the line with this service, if you don't have it just add it to this file

2. Add one file to /etc/xinetd.d/ folder name cvsserver /etc/xinetd.d/cvsserver

service cvspserver
{
port = 2401
socket_type = stream
protocol = tcp
wait = no
user = root
passenv = PATH
server = /usr/bin/cvs
env = HOME=/home/cvs
server_args = -f --allow-root=/home/cvs/cvsroot pserver
}

You just change the HOME to your CVS repository, for example if you want to setup the cvs directory on /home/cvs where the the home of the cvs server owner put the HOME=/home/cvs (default user for redhat) and put the --allow-root=/where/the/cvsroot for example I like to put the cvs in user folder for easier maintainance and permission. So put the path /home/cvs/cvsroot.

 c. Restart the xinetd server
root@localhost># service xinetd restart

You'll see the server up and running using netstat -a

[root@tender xinetd.d]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 *:cvspserver                *:*                         LISTEN

Now your CVS server is running and you can setup the repository remotely or locally. This tutorial is using to setup the file locally. FTP/SCP the file to the server for example your package folder name "mysoftware" upload it to /home/cvs/mysoftware

d. Setup the repository for mysoftware Change to user cvs to setup the repository

 root@localhost># su - cvs
cvs@localhsot>$ cvs -d /home/cvs/cvsroot init
[cvs@localhost> $ ls -al /home/cvs/cvsroot/
total 12
drwxrwxr-x 3 cvs cvs 4096 Feb 17 19:27
. drwx------ 6 cvs cvs 4096 Feb 17 19:27
.. drwxrwxr-x 3 cvs cvs 4096 Feb 17 19:27 CVSROOT

This will setup the repository in /home/cvs/cvsroot

e. Put the project file into repository

cvs@localhost>$ cd /home/cvs/mysoftware
cvs@localhost>$ cvs -d /home/cvs/cvsroot import -m 'The -m is for specifying the message' mysoftware 'vendor tag' start

The 'vendor tag' is required for cvs to setup the repository for labeling or tagging in cvs language. You'll see a lot of message that cvs is putting your software to the repository.

 f. Enable remote user to use the cvs server
1. You need to setup one password file (it's different from shell user) on /home/cvs/cvsroot/CVSROOT/passwd put on this format

user1:Encrypted Password:cvs
user2:Encrypted Password:cvs

... put more if many user you want to setup

How to generate the Encrypted Password ?
There's many way to generate the encrypted password. I'm using the htpassword located at apache binary directory. In my case I'm installing it on /usr/local/apache/bin/htpassword cvs@localhost>$ /usr/local/apache/bin/htpasswd -nb username pass123 username:f2.O2FRh7Pyfo

Copy this output and put it on /home/cvs/cvsroot/CVSROOT/passwd file, append the :cvs on the output. If you don't have the htpasswd, you can copy some hash password from /etc/passwd that you know and put it on the Encrypted Password location. g. Login and checkout to the cvs If you're on windows, I recommend you to use WinCVS http://www.wincvs.org. install the wincvs and cvsnt to enable the command line from wincvs. I'll show you command line check out.

1. Command line
user@remote>$ cvs :pserver:username@example.org:/home/cvs/cvsroot login password: pass123
user@remote>$ cvs :pserver:username@example.org:/home/cvs/cvsroot co

This command will put all of the remote file into your folder and in this case, this will put all the content of mysoftware to your local folder

2. WinCVS If you're using WinCVS, you can use the WinCVS GUI

Admin > Login Put the CVSROOT :pserver:username@example.org:/home/cvsroot and OK button

This will send the login command to the cvs server after that you need to checkout Remote > Checkout Module

Put the module name or the directory you want to check out. For example I want to checkout everything for the first time, so I just put "mysoftware" on the field. Local folder to checkout , just put the local folder you want to put your checkout file, just browse or put manually e.g C:/EasyPHP/www/ and lastly put the CVSROOT same as the login :pserver:username@example.org:/home/cvsroot

You need to change the username to user you setup in /home/cvs/cvsroot/CVSROOT/password and the /home/cvsroot to your cvsroot folder, example.org you need to change your cvs domain or IP number. For more about using cvs, you can go to http://www.cvshome.org

About the Author
================

Ahmad Amran is a webmaster for Malaysia PHP Group http://www.php.net.my .He has worked on many large PHP driven sites and does freelance web development . He currently working on a helpdesk software at http://www.hedesu.com .You can contact him directly at amran (at) php.net.my .


Internalize your website with IntSmarty and Smarty

IntSmarty is an extension from Smarty Templating Engine (http://smarty.php.net). A working knowledge of PHP, PHP's OOP structure, and HTML is assumed. This article also assumes the reader has understand PHP Templating with Smarty and has a working Smarty setup. You can read at PHP Templating with Smarty and Smarty: A closer look for reference.Overview
========
IntSmarty short form for Internationalization Smarty created by John Coggeshall which allows you to implement multi-lingual web sites in a very effective manner. You can visit his IntSmarty website at http://www.coggeshall.org/oss/intsmarty/.
How it works
============
You can download it from http://www.coggeshall.org/oss/intsmarty/. There is only one properties that need to be setup, $lang_path. Wether you can hardcode it in the IntSmarty Class or you can extend this class like an example below:

Class Translate extends IntSmarty {
var $lang_path = '/put/to/your/lang/dir/'; //include trailing slash

function Translate($lang = null) {
parent::IntSmarty($lang);
    }
}


Example 1.0

IntSmarty use your default $_SERVER["HTTP_ACCEPT_LANGUAGE"] if you not specify your default $lang in IntSmarty contructor or Translate in Example 1.0

Preparing the localization file
===============================
IntSmarty is using md5 for creating the translation file. It is because it will shortened the string into 32 bit string. Currently the format for the language file is {$lang}.php where $lang is you localization file.If you want to support english localization you need to create en.php and save it on your $lang_path directory (Example 1.Innocent



<?php
$__LANG
['8b1a9953c4611296a827abf8c47804d7'] = 'Hello';
$__LANG['628dd18721ddcaa880f8e46f9a3fa2ac'] = 'Welcome to my tutorial';
$__LANG['76779fb3d56344c10128e90225823a30'] = 'Tutorial for Internationalization your PHP with IntSmarty';
?>


Code 1.0 : en.php

Looks difficult isn't it ? This file is created from the html that you want to internationalize it (Code 1.1) . In int_smarty.html you only need to specify the [bold]{l}the word you need to translate{/l}[/bold] block. I've created a little PHP code to generate the language file for you. Use the grep_lang.php (Code 1.2) to generate you localization file.

<html>
<head>
<title>Internationalization Smarty</title>
</head>
<body>
{l}Hello{/l} Guest<br>
{l}Welcome to my tutorial{/l}<br>
{l}Tutorial for Internationalization your PHP with IntSmarty{/l}
</body>
</html>


Code 1.1 : int_smarty.html



<?php
/*
Generate localization for IntSmarty
*/
$file = $_GET['file'];
$fp = fopen($file, 'r');
$content = fread($fp, filesize($file));
$regex = '!{l}(.*?){/l}!s';
preg_match_all($regex, $content, $matches);
foreach(
$matches[1] as $m) {
echo
'$__LANG[\'' . md5($m) . '\'] = \'' . $m . '\';<br>';
}
?>


Code 1.2 : grep_lang.php
You only need to specify you $_GET['file'] using the url e.g http://example.org/grep_lang.php?file=int_smarty.html where the int_smarty.html located at the same directory with your grep_lang.php file.

Using IntSmarty
================



<?php
include 'lib/Smarty/libs/Smarty.class.php'; //path to your Smarty Class
include 'lib/intsmarty/intsmarty.class.php'; // path to your IntSmarty Class
Class Translate extends IntSmarty {
var
$lang_path = 'lang/'; //include trailing slash

function Translate($lang = null) {
parent::IntSmarty($lang);
    }
}
//specifying the default language to use
$IntSmarty = &amp; new Translate('en');
$IntSmarty->display('int_smarty.html');
?>


Code 1.3 : int_smarty.php
The Code 1.2 is using the following directory structure

int_smarty.php
----------------/templates/int_smarty.html <-- default Smarty template directory
----------------/lang/en.php
----------------/templates_c <-- default Smarty compile directory

You will get the same output as the int_smarty.html because you're using the same translation. If you want to use other languages you just change localization file to your language. For example, if you want to translate your website to Malay language.



<?php
include 'lib/Smarty/libs/Smarty.class.php'; //path to your Smarty Class
include 'lib/intsmarty/intsmarty.class.php'; // path to your IntSmarty Class
Class Translate extends IntSmarty {
var
$lang_path = 'lang/'; //include trailing slash

function Translate($lang = null) {
parent::IntSmarty($lang);

    }
}
//specifying the default language to use
$IntSmarty = &amp; new Translate('ms'); //change to ms.php language
$IntSmarty->display('int_smarty.html');
?>


Code 1.4 : Change to Malay localization file
Save the en.php as ms.php and translate the file to Malay. Save it to your $lang_path directory.



<?php
$__LANG
['8b1a9953c4611296a827abf8c47804d7'] = 'Apa khabar';
$__LANG['628dd18721ddcaa880f8e46f9a3fa2ac'] = 'Selamat datang ke tutorial saya';
$__LANG['76779fb3d56344c10128e90225823a30'] = 'Tutorial untuk i18nl PHP and dengan IntSmarty';
?>


You will get the output as below.

Apa khabar Guest
Selamat datang ke tutorial saya
Tutorial untuk i18nl PHP and dengan IntSmarty

Summary
=======
IntSmarty provide you with an alternative to have an Internationalization layer to your PHP powered website. This tutorial provide you the basic how to use IntSmarty Class. The rest is up to your imagination.
About the Author
================
Ahmad Amran is a webmaster for Malaysia PHP Group http://www.php.net.my . He has worked on many large PHP driven sites and does freelance web development . He currently working on a helpdesk software at http://www.hedesu.com .You can contact him directly at sameon (at) php.net.my .

Data abstration

Ada artical disini membincangkan kenapa data abstration perlu.Satu contoh mudah data class abstration.Ini adalah contoh php dan mysql connection
class menghubungkan_pengkalan_data {              
var alamat_server="":
//biasa guna localhost atau alamat ip address
var nama_pengguna="";
var nama_kata_laluan_pengguna="";
var nama_pengkalan_data="":
function  menghubung_dengan_server() {
$connection=mysql_connect($this->alamat_server,$this->nama_pengguna,$this->nama_kata_laluan_pengguna) or die($this->masalah());
return ($connection);
}
function menghubung_dengan_pengkalan_data {
mysql_select_db($this->nama_pengkalan_data,$this->menghubungkan_dengan_server()) or die($this->masalah())l
}
function masalah() {
print"Tidak boleh menghubungkan dengan server";
}
}
Script di atas adalah sedikit contoh class data abstration.Misal php ada memberi tahu nombor kesalahan mysql_error.Oleh itu anda boleh buat satu switch untuk kembangkan function masalah untuk membuat error handling di dalam bahasa melayu.Dalam php 5 ada try catch,tetapi saya tidak mahir.Lebih baik guna die untuk buat error handling.
** Kalau guna data abstration kena berhati hati kerana var output adalah  lebih pantas keluar dari  buat echo atau print.
Contoh di atas boleh lagi dikembangkan seperti
class navigasi extend class menghubung_pengkalan_data  {
function queri() {}
function laman_sebelum() {}
function laman_selepas() {}
}
class menghubung_pengkalan_data {
// contoh seperti di atas
}
Data abstration adalah kreativiti dan untuk menjimatkan coding.Tetapi makin lama class akan menjadi complex.Cuma perlu berhati hati kerana php tidak support multi -inheritance seperti language c.PHP hanya support single inhertance sahaja
Author http://www.gamesdept.com

Serious about PHP ?
Subscribe to our mailing list

Latest Event
  • No upcoming events available
Sites Built using PHP
Highest Users
UserPoints
amin0072050
aku_tak_tau1524
hymns975
alien3d630
mosfet83602
Twitter
  • 50% discount on your VPS at ZenClouds as low as RM 30 / month. Limited time only ! . http://ow.ly/5TQ61
  • Zenclouds VPS soft launching. Discount 50% on all package. VPS as low as RM 30 / month. Put ZENPROMO promotion code http://ow.ly/5Qr02
  • 5 reasons to joins a startup after graduating . http://ow.ly/58J7y
  • Interested to become beta tester for Nodechatter ? Get FREE live chat support for your website. Read here http://ow.ly/4TRA2
  • My company is launching a live chat support for your website. Accepting beta tester. Tweet me for request. http://ow.ly/4RYvq #nodechatter
Navigation
User login