顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2012年10月7日 星期日

PHP XML


function http_get($url,$header=array("Content-Type: text/xml;charset=utf-8")){
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_TIMEOUT, 60);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 $ret = curl_exec($ch);
 curl_close($ch);
 return $ret;
}

function http_post($url,$params,$xml_post = true){
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_TIMEOUT, 60);
 curl_setopt($ch, CURLOPT_POST, 1);
 if($xml_post) curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
 $ret = curl_exec($ch);
 curl_close($ch);
 return $ret;
}


出處
http://qxbbs.org/viewtopic.php?p=1039323&sid=d94ea6385045e4b3cc6b32c8a282299c

2010年5月30日 星期日

MySQL和 PHP 採用UTF8的詳細方法

1. 用 vi /etc/httpd/conf/httpd.conf 設定Apache中的語系為:( (記得restart)
AddDefaultCharset UTF-8

2. 用 vi /etc/php.ini 設定php中的語系為:( (記得restart)
default_charset = "utf-8"

3. 用 vi /etc/my.cnf 設定MySQL中的語系為:( (記得restart)
[mysqld]
init_connect='SET NAMES utf8'
default-character-set=utf8
[client]
default-character-set = utf8

4. 建立資料庫時選擇語系: (記得清除DB Cache)
DROP DATABASE IF EXISTS `aa`;
CREATE DATABASE `aa` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `aa`;

CREATE TABLE IF NOT EXISTS `aat` (
`id` char(1) NOT NULL default '1',
`myStr` varchar(200) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

5. 用UltraEdit(v11.20a版) 轉換所有ANSI格式的php檔案轉化為UTF-8格式:
File --> Conversions --> ASCII to UTF-8 (Unicoding Editing)
( 在UltraEdit中按Advanced --> configuration --> File Handling
--> Unicode/UTF-8 Detection --> 剔選Auto detect utf-8 files )
如有需要時,可執行Remove BOM.php. 當用WinXP的Notepad將php檔由ANSI轉為UTF-8時,
因檔頭有BOM,會引起排版問題,故必須移除,執行Remove BOM.php即可自動移除.
Remove BOM.php可由以下網址下載:
http://www.hoyo.idv.tw/hoyoweb/document/view.php?sid=13&author=hoyo&status=view

6. 在php檔中必須加入:

<meta equiv=``Content-Type`` content=``text/html; charset=UTF-8``>

7. 在連接DB的檔中必須加入3行mysql_query才ok:
$host="localhost"; $DBname="aa";
$user= "root"; $passwd = "";
$link = mysql_connect($host,$user,$passwd) or die ("Fail");
$db = mysql_select_db($DBname, $link) or die ("Fail");
// 要在真正query DB取出資料前,加入以下3行
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET_CLIENT=utf8");
mysql_query("SET CHARACTER_SET_RESULTS=utf8");
$sql = "select * from aat where crid='1'";
$rows = mysql_query($sql);

8. 在php檔中, 如有需要須注意: [Optional]
運用htmlentities和htmlspecialchars時,要似 如下:
$chars = htmlentities($chars,ENT_QUOTES,"UTF-8");
$chars = htmlspecialchars($chars,ENT_QUOTES,"UTF-8");
並且在顯示前要用
$chars = html_entity_decode($chars,ENT_QUOTES,"UTF8");
如有用過 addslashes()或mysql_real_escape_string()記得用以下:
$chars = stripslashes($chars);
如有需要可以用以下function將不同編碼轉換:
$chars = iconv('Big5','UTF-8',$chars); //由Big5轉為UTF-8

參考出處:請點我

2009年10月7日 星期三

PHP:隨機產生驗證碼登錄實例

?>session_start();

//不存在imageCreate函数则认为当前环境不支持GD库

if (function_exists('imagecreate')) {

//产生4个字符的随机字符串作为验证码

$str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

$code = array();

for ($i=0; $i<4;>

$code[] = $str[mt_rand(1,strlen($str))];

}

//将验证码写入到Session,忽略大小写

$_SESSION['vdcode'] = strtolower(implode('',$code));

$width = 50; //图片宽度

$height = 20; //图片高度

$im = ImageCreate($width,$height); //创建图形

ImageColorAllocate($im,255,255,255); //填充背景颜色为白色

//用淡色给图形添加杂色

for ($i=0; $i<100;>

$pxcolor = ImageColorAllocate($im,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));

ImageSetPixel($im,mt_rand(0,$width),mt_rand(0,$height),$pxcolor);

}

//用深色调绘制边框

$bordercolor = ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));

ImageRectangle($im,0,0,$width-1,$height-1,$bordercolor);

//用比较明显的颜色写上验证码文字

$offset = 5;

foreach ($code as $char) {

$textcolor = ImageColorAllocate($im,mt_rand(0,250),mt_rand(0,150),mt_rand(0,250));

ImageChar($im,5,$offset,2,$char,$textcolor);

$offset += 10;

}

//禁止缓存

header("pragma:no-cachern");

header("Cache-Control:no-cachern");

header("Expires:0rn");

//检查系统支持的文件类型,优先级为PNG->JPEG->GIF

if (ImageTypes() & IMG_PNG) {

header('Content-Type:image/png');

ImagePNG($im);

} elseif (ImageTypes() & IMG_JPEG) {

header('Content-Type:image/jpeg');

ImageJPEG($im);

} else {

header('Content-Type:image/gif');

ImageGif($im);

}

} else {

//不支持GD库,则输出默认验证码ABCD

$_SESSION['vdcode'] = 'abcd';

header('Content-Type:image/jpeg');

$fp = fopen('vdcode.jpg','rb');

echo fread($fp,filesize('vdcode.jpg'));

fclose($fp);

}

?>

資料來源:請點這

PHP:Call to undefined function: mcrypt_module_open()

windows

当运行php的服务器端缺少libmcrypt.dll时使用函数mcrypt_module_open进行解密会出现此错误。
在服务器上做如下设置可解决。

到网上下载一个php的mcrypt模块安装包,只需要libmcrypt.dll文件即可

1.将libmcrypt.dll复制到system32目录或php安装目录下的extensions目录下
2.将libmcrypt.dll复制到apache安装目录的bin目录下
3.到php安装目录下找到php.ini文件,打开它
4.找到; Directory in which the loadable extensions (modules) reside.
extension_dir = "./"
这两行,要使extension_dir指向的目录下能找到libmcrypt.dll,或系统path下有libmcrypt.dll
5.找到;Windows Extensions 项下面的;extension=php_mcrypt.dll这一行和;extension=php_iconv.dll这两行
6.去掉;extension=php_mcrypt.dll前面的分号和;extension=php_iconv.dll前面的分号
7.到apache安装目录下找到php.ini文件,打开它
8.找到; Directory in which the loadable extensions (modules) reside.
extension_dir = "./"
这两行,要使extension_dir指向的目录下能找到libmcrypt.dll,或系统path下有libmcrypt.dll

9.找到;Windows Extensions 项下面的;extension=php_mcrypt.dll这一行和;extension=php_iconv.dll这两行
10.去掉;extension=php_mcrypt.dll前面的分号和;extension=php_iconv.dll前面的分号
以上由smartdp原创。

linux

显示Call to undefined function: mcrypt_module_open()的错误
原因:缺少libmcrypt-2.5.7.tar.gz
到这里ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/下载libmcrypt-2.5.7.tar.gz,
tar -zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure --prefix=/usr/local/libmcrypt
make
make check
make install
成功后在进入php
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/httpd/bin/apxs --with-gd=/usr/local/gd --enable-gd --enable-gd-native-ttf --with-jpeg-dir=/usr/local/jpeg6 --with-png=/usr/local/lib --with-ttf --with-zlib=/usr/local/zlib --with-freetype-dir=/usr/local/freetype --enable-magic-quotes --with-mysql=/usr/local/mysql --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock --with-iconv --with-mbstring --enable-mbstring --enable-track-vars --enable-force-cgi-redirect --enable-ftp --with-config-file-path=/usr/local/httpd/conf --with-pear=/usr/local/php/pear --with-mcrypt=/usr/local/libmcrypt
配置php
make
make install
重新启动服务器

資料來源:請點這