2011年5月23日 星期一

PHP, 路徑問題

找個地方放設定檔。例如,在網站的根目錄建一個 system 資料,檔名叫 config.php 假設網站根目錄是 D:\xampp\website

完整路徑:D:\xampp\website\system\config.php

內容:

<?php

//不含檔名的完整路徑,本例 :D:\xampp\website\system

$fullpath = __DIR__;



//用 system 這個資料夾名稱當做分割字元。其實就是刪除 system 這個字本身。

//得到 D:\xampp\website\

$rootpath = explode("system",$fullpath);

define('ROOT', $rootpath[0]); //

?>



在網站任何一個層級的資料夾,只要引用這個 config.php即可。

例如: D:\xampp\website\aaa\bbb\ccc.php

ccc.php的內容:

<?php

require_once('../../system/config.php');

echo ROOT; //  取得網站根目錄的路徑: D:\xampp\website\

?>

2011年1月22日 星期六

網頁編碼



















apache
AddDefaultCharset UTF-8
html
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
php:
1.<?php

header('Content-type: text/html; charset=utf-8');

?>
2.<?php

$new_str = iconv("BIG5", "UTF-8", $old_str);  //big5轉utf8

$new_str = iconv("UTF-8", "BIG5", $old_str); //utf85轉big5

?>
3.<?php

//mysql

mysql_query("SET NAMES 'utf8'");

?>
4.<?php

//sqlsrv(MSSQL)

$connectionInfo = array( "UID"=>"帳號", "PWD"=>"密碼", "Database"=>"資料庫名稱","CharacterSet" => "UTF-8");

?>
5.<?php

//sqlsrv(MSSQL)

while(sqlsrv_fetch($stmt)){

    echo sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING("UTF-8"))."</br>";

?>