Utilize shell scripts,linux commands, open source tools,java, to Maximize the Power of Linux.
Focused on working with linux and shell, search engine technology including Chinese segmenter
Any questions please contact me at gmail: david.ullua

Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

9/19/2008

PHP, MySQL中使用UTF-8编码,以及中文乱码的解决

若在linux/unix服务器上使用
注意如下事项:
1、设置mysql服务使用的编码:
#vi /etc/
在 [mysqld]以及[mysqld_safe]后面都加上以下两行:
default-character-set=utf8
default-collation=utf8_general_ci

然后重启mysqld服务: /etc/init.d/mysqld restart (fedora 9) 为例。
2.php的页面代码,执行sql语句前(或者在数据库连接建立后)设置本次连接的字符集,示例如下:
mysql_query("SET NAMES 'utf8'");
$result=mysql_query("select * from t_userlog");

3.Terminal登录mysql时指定字符集:
mysql --default-character utf8 -u root -pmypass
这样也可以:
mysql --default-character-set utf8 -u root -pmypass

4.apache的配置文件httpd.conf中,找到 AddDefaultCharset一行,注释掉,或者改成如下:
AddDefaultCharset off

(这一步是为了让不同字符集,比如utf-8,gbk的网页放到服务器上都可以正常在客户端显示)

5.每一个php页面中,都在head部分设定meta头的charset:
< meta equiv="Content-Type" content="text/html; charset=UTF-8" >

6. 以终端登录mysql中可以用如下sql语句来显示以character开头的变量:
SHOW VARIABLES LIKE 'character_set_database';

SHOW VARIABLES LIKE 'character_set_client';
show variables like 'character%';

可以通过 show character set; 语句来显示有哪些可用的字符集。

1/11/2007

to implement SVN password changing function by shell and php

We have install SVN as our version control system. It doesn't provide a remote password changing function on the web. I googled on the internet and didn't found a good solution. So I wrote a script in bash and a php front end page to do it.
(if you need a jsp or perl front end page, you can write one yourself)

Here's the shell script to change password for svn:

#!/bin/sh
#changepass.sh , function: change password for svn
#usage: changepass.sh username oldpass newpass newpassretype
if [ ! "$#" = 4 ]
then
echo "Usage:`basename $0` username oldpass newpass newpassretype"
exit 1
fi

passfile="/home/svn/passwd"

username=
oldpass=
newpass1=
newpass2=

username=$1
oldpass=$2
newpass1=$3
newpass2=$4

if [ ! $newpass1 = $newpass2 ]
then
#echo "password does not match!";
echo "password does not match!"
exit 2
fi

export NEWPASS=$newpass1
export USERNAME=$username

i=`sed /^"$username"[" ""\t"]*=[" ""\t"]*$oldpass\$/p $passfile -n|wc -l`
if [ $i = 1 ]
then
awk '{ if(index($0,ENVIRON["USERNAME"])==1) {print ENVIRON["USERNAME"] " = " ENVIRON["NEWPASS"] }else {print $0} }' $passfile > /home/svn/newpass

rm /home/svn/newpass 2>&1 1> /dev/null
cp /home/svn/newpass $passfile

echo 'password changed!'
elif [ `sed /"$username"/p $passfile -n |wc -l` > 0 ]
then
echo ' username and password does not match'
else
echo 'could not find username'
fi


here's the php front end changepass.php , it is for php 5, if you use php 4, maybe you need use $HTTP_POST_VARS instead $_POST ( just guess, i haven't test it):



if(strlen($_POST["pwd1"]) >0 ){
if($_POST["pwd1"]!=$_POST["pwd2"]){
print("Passwords do not match, try again.

\n");


}else{

$LoginName = $_POST["loginname"];
$OldPassword = $_POST["oldpwd"];
$NewPass1 = $_POST["pwd1"];
$NewPass2 = $_POST["pwd2"];
$retval='';
#echo $LoginName;
#echo $OldPassword;
// user apache or nobody(who runs httpd daemon) needs the permission of passwd file and temp file
$command = "/mnt/apache/changepass.sh ".$LoginName." ".$OldPassword." ".$NewPass1." ".$NewPass2 ;

#$command = "/bin/cat /mnt/apache/changepass.sh "." 2>&1";
$command = $command." 2>&1";

#system("/bin/echo abc",$retval);
system($command, $retval);

echo "
" ;echo $retval;echo '
';


if($retval!=0){
print("
Error while setting password:
\n");

}

}
}
else{
;#echo "parameters not set!";
}
?>





Login Name:
Old Password:
New Password:
Repeat Password:


the php page and shell scripts should works ok, but the php page shows nothing after click "submit" in fedora, however in slackware it is ok.

After debug step by step, found that shell script error message would not be ouput on the web page, so i add this line: $command = $command." 2>&1";

And finally found that SELinux would block some function on the web server. I disabled SELinux in the firewall settings, and it works.



difference on class between php version 4.4.4 and 5.2.0

Different PHP versions behalves differently. The only two distribution on php.net is 4.4.4 and 5.2.0. The experience of the article describes class difference between 4.4.4 and 5.2.0 is that:
1). class members variable is decorated by "private" in 5.2.0, as "var" in 4.4.4
2). class functions is decorated by "public function" in 5.2.0, as "function" in 4.4.4


Several days ago, i wrote a php proxy for wap.roboo.com to transfer jsp response to a php server. And grabed a HTTPRequest class from google, the class code is as follows:

class HTTPRequest
{
var $_fp; // HTTP socket
var $_url; // full URL
var $_host; // HTTP host
var $_protocol; // protocol (HTTP/HTTPS)
var $_uri; // request URI
var $_port; // port

// scan url
function _scan_url()
{
....
}

// constructor
function HTTPRequest($url)
{
$this->_url = $url;
$this->_scan_url();
}

// download URL to string
function DownloadToString()
{
....
}
}

I use Slackware 11 to do most works, slack use php 4.4.4 by default. the HTTPRequest works ok on my machine. After deploy it on another fedora machine which has php 5.0 by default, it fails. Then I updated it to php 5.2.2, with the help of one colleague, I changed the class as follows to support 5.2.2:

class HTTPRequest
{
private $_fp; // HTTP socket
private $_url; // full URL
private $_host; // HTTP host
private $_protocol; // protocol (HTTP/HTTPS)
private $_uri; // request URI
private $_port; // port

// scan url
public function _scan_url()
{
....
}

// constructor
public function HTTPRequest($url)
{
$this->_url = $url;
$this->_scan_url();
}

// download URL to string
public function DownloadToString()
{
....
}
}

By the way, i use vi(vim actually) to edit source, just use :0,$s/var/private/g , :0,$s/function/public function/g to do the substitution.

After disabled SELinux on fedora , it works ok.

About Me

I am a senior developer and a team leader with 3 years development experience in Suzhou, China, focus on mobile web search, linux, Java and machine learning in NLP (natural language processing). My goal is to improve people's life with computer technology.