2013年6月26日 星期三

XAMPP - 變更Apache網站目錄

XAMPP網站預設安裝目錄為C:\xampp\htdocs\,我們可修改C:\xampp\apache\conf\httpd.conf檔案內容,變更網站目錄。假設要將網站目錄變更到D:\projects\wwwroot,步驟如下:

首先找到DocumentRoot設定,在原來設定項前插入#,使該項設定變為註解
# DocumentRoot "C:/xampp/htdocs"
之後加入下列設定項
DocumentRoot "D:/projects/wwwroot"

接著尋找<Directory>設定,同樣在設定項前插入#,使該項設定變為註解
#<Directory "C:/xampp/htdocs">
之後加入下列設定項
<Directory "d:/projects/wwwroot">


PHP變數

PHP變數命名時須符合以下規則:

  • 使用$符號開頭
  • 變數名稱起始字元:_或英文字母
  • 變數名稱後續字元:_或英文字母或數字
  • 區分大小寫
  • 不能使用保留字

宣告時不需要設定資料型別,例如:
$username
$_passswd

程式中使用=來指派變數值,例如:
$user1 = "Mike";

我們也可以使用參照指派方式,讓不同變數使用用一空間,彼此連動。例如下面程式變數$user2指向變數$user1,之後只要更改其中一個變數值,另一個變數就一起連動。
$user1 = "Mike";
$user2 = &$user1; //$user2指向$user1變數
echo $user1.'';
echo $user2.'';
$user1 = "Tina";
echo $user1.'';
echo $user2.'';

PHP還允許使用可變動變數,也就是變數名稱本身為動態。例如
$school = "nkut";
$nkut = "nankai university of technology";
echo $school.'';
echo $$school;

設定Eclipse檔案存檔編碼

使用Eclipse開發PHP應用程式,PHP存檔時建議使用utf-8編碼。
我們可由功能表命令Window/Preferences,設定工作區檔案預設使用utf-8編碼存檔:

另外也可以由功能表命令File/Properties,設定個別檔案的存檔編碼方式:

2013年6月14日 星期五

Android程式設計 - HTTP資料傳遞(2)使用POST

假設伺服端httppost.php網頁如下,:
<?php
//宣告使用utf-8編碼
header("Content-Type:text/html; charset=utf-8");
$user=$_POST['login_user'];
$pass=$_POST['login_pw'];
echo "login_user=".$user.";login_pw=".$pass;
?>

我們可以先使用以下表單來驗證網頁功能
<html>
<body>
測試POST資料傳送:
<form method="POST" action="http://163.22.249.40/myPHP/httppost.php">
帳號:
<input type="text" name="login_user" size="20"value="sswu" /><br />
密碼:
<input type="text" name="login_pw" size="20" value="1234" /><br />
<input type="submit" name="btnSend" value="送出" /><br />
</form>
</body>
</html>

HTTP POST與HTTP GET主要差異在於參數資料傳遞方式,使用POST方式時必須使用setEntity()設定POST參數,程式如下:

String Url = "http://my_webserver/httppost.php";
//設定POST參數
List postData = new ArrayList();
postData.add(new BasicNameValuePair("login_user", "sswu"));
postData.add(new BasicNameValuePair("login_pw", "0123"));
String result = httpGET(Url, postData);
if (result == null) {
//顯示錯誤訊息
}
        ...

    /**
     * Issue a POST request to the server.
     * @param url POST address.
     * @param params request parameters.
     */
    private String httpPOST(String url, List params){ 
        HttpPost post = new HttpPost(url);
        try {
            //送出HTTP request
            post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            //取得HTTP response
            HttpResponse httpResponse = new DefaultHttpClient().execute(post);
            //檢查狀態碼,200表示OK
            if (httpResponse.getStatusLine().getStatusCode()==200){
                //取出回應字串
                String strResult = EntityUtils.toString(httpResponse.getEntity());
                return strResult;
            }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Android程式設計 - HTTP資料傳遞(1)使用GET

假設伺服端httpget.php網頁如下,:
<?php
//宣告使用utf-8編碼
header("Content-Type:text/html; charset=utf-8");
$user=$_GET['login_user'];
$pass=$_GET['login_pw'];
echo "login_user=".$user.";login_pw=".$pass;
?>

我們可以先使用以下表單來驗證網頁功能
<html>
<body>
測試GET資料傳送:
<form method="GET" action="http://163.22.249.40/myPHP/httpget.php">
帳號:
<input type="text" name="login_user" size="20"value="sswu" /><br />
密碼:
<input type="text" name="login_pw" size="20" value="1234" /><br />
<input type="submit" name="btnSend" value="送出" /><br />
</form>
</body>
</html>

HTTP GET是將參數附加在URL之後,例如:http://my_webserver/httpget.php?login_user=sswu&login_pw=1234,Android中可使用HttpGet 物件進行HTTP GET資料傳遞,程式如下:

String Url = "http://my_webserver/httpget.php";
//設定GET參數
List getData = new ArrayList();
getData.add(new BasicNameValuePair("login_user", "sswu"));
getData.add(new BasicNameValuePair("login_pw", "0123"));
String result = httpGET(Url, getData);
if (result == null) {
//顯示錯誤訊息
}
        ...

    /**
     * Issue a GET request to the server.
     * @param url GET address.
     * @param params request parameters.
     */
    private String httpGET(String url, List params){
        //組合成http get格式
        StringBuilder sb = new StringBuilder();
        sb.append(url).append("?");
        for (NameValuePair nvp : params) {
            try{
                sb.append(nvp.getName()).append("=").append(URLEncoder.encode(nvp.getValue(), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            sb.append('&');
        }
        sb.deleteCharAt(sb.length()-1);
        HttpGet get = new HttpGet(sb.toString());
        try {    
            //送出HTTP request並取得HTTP response
            HttpResponse httpResponse = new DefaultHttpClient().execute(get);
            //檢查狀態碼,200表示OK
            if (httpResponse.getStatusLine().getStatusCode()==200){
                //取出回應字串
                String strResult = EntityUtils.toString(httpResponse.getEntity());
                return strResult;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }