Archive for the ‘php’ Category

Form 小技巧

Posted: 2014 年 04 月 24 日 in php

1.陣列接收

<form

……

<input type="checkbox" name="address[ ]" value="桃園" /> 桃園
<input type="checkbox" name="address[ ]" value="台中" />台中 
<input type="checkbox" name="address[ ]" value="高雄" />高雄 
<input type="checkbox" name="address[ ] " value="台南" />台南

…….

 

接收的話,也是 $_POST[‘address’]
只不過 $_POST[‘address’] 是一個陣列
所以可以用
foreach($_POST[‘address’] as $val){
echo $val;
}
就可以找出所有值。

or

假如選了「台中」、「高雄」
$address=implode(“,",$_POST[‘address’]);
$address的值就是「台中,高雄」

 

2.serialize() 很好用  可以多用

$.ajax({
url: “test1.php",
data: $(‘#applyForm’).serialize(),
type:"POST",
dataType:’json’,

success: function(msg){
alert(“return="+msg);
},

error:function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});

PHP 檔案管理

Posted: 2014 年 03 月 12 日 in php

檔案處理

  PHP也可進行一般檔案的處理,例如文字檔的開啟、讀取、寫人、關閉等等,甚至還可以進行檔案的複製、搬移、刪除,以及目錄的處理功能,唯在處理這些檔案之前,如果要透過Web的方式來處理,得先注意檔案在系統中的權限問題,像是Linux系統,一般檔案的權限可分為擁有者、群組、其他等三種不同的使用者權限,同時又區分「讀、寫、執行」三種屬性權限,所以,在使用時得先留意在那個系統(Linux或Windows)執行,以及上述權限問題等,才能順利操作PHP管理檔案。 以下就以一個文字檔為例,進行所謂的:

  • 開啟檔案 ─ fopen( )函數
  • 顯示檔案 ─ fpassthru ( )函數
  • 檔案內容讀取 ─ fread( ) 、fgets( )函數
  • 寫入檔案 ─ fwrite( ) 、fputs( ) 函數
  • 關閉檔案 ─ fclose ( ) 函數
  • 複製檔案 ─ copy ( ) 函數
  • 刪除檔案 ─ unlink( ) 函數

在操作這些檔案處理函數時,php.ini中的設定會影響這些函式的動作:

參數名稱
預設值
說明
參數可變更性
allow_url_fopen
1
是否允許利用網頁方式開啟檔案
php.ini
user_agent
設定PHP傳送時使用者代理
php.ini
default_socket_timeout
60秒
在使用連線功能時的逾時時間設定(即自動斷線時間)
php.ini
auto_detect_line_endings
Off
自動檢查「行」的結束(換行)
php.ini

 

fopen 開啟檔案
語法
fopen ( string filename, string mode [, int use_include_path [, resource zcontext]])

fopen ( string 檔案名稱, string 開啟模式 [, int use_include_path [, resource zcontext]])

說明
  1. filename可以是檔案名稱或絕對路徑的檔案
  2. 開啟模式如下表說明:

    開啟
    模式

    說明
    'r'
    開啟成唯讀檔
    'r+'
    開啟成可讀寫的檔案
    'w'
    開啟只有寫入的檔案,並將檔案長度設為零;如果檔案不存在,則建立。
    'w+'
    開啟可讀寫,如果檔案存在,則會清除所有內容,長度為零; 如果檔案不存在,則建立。
    'a'
    開啟只有寫入的檔案,資料由檔案尾部加入;如果檔案不存在,則建立。
    'a+'
    開啟可讀寫的檔案,資料由檔案尾部加入;如果檔案不存在,則建立。
    'x'
    建立並開啟只有寫入的檔案,資料由檔案開頭寫入;如果檔案存在,fopen( )函數將回應「false」,並發生錯誤;如果檔案不存在,則建立。這個參數自PHP 4.3.2 以後版本開始支援。.
    'x+'
    建立並開啟可讀寫的檔案,資料由檔案開頭寫入;如果檔案存在,fopen( )函數將回應「false」,並發生錯誤;如果檔案不存在,則建立。這個參數自PHP 4.3.2 以後版本開始支援。.
  3. include_path 是指定檔案可搜尋的路徑位置。
  4. 如果操作成功的話,會回傳一個「handle」值,失敗則傳回「 false 」

 

傳回值
resource
範例
01:
02:
03:
04:
05:
<?php
  if ( !($fp = fopen("file_ex.txt", "r")) ) {
    echo "無法開啟檔案";
  } 
?>
第02行:開啟檔案,「!」表示相反的意思;如果成功,則結果會回傳給$fp。

 

 
fpassthru 顯示檔案
語法
fpassthru ( resource handle)
說明
當fopen( )函數開啟檔案成功的話,使用fpassthru( )函數,會檔案內容全部輸出到網頁上。
傳回值
int
範例
01:
02:
03:
04:
05:
06:
07:
<?php
  if ( !($fp = fopen("file_ex.txt", "r")) ) {
    echo "無法開啟檔案";
  } else {
    fpassthru ($fp) ;
  }  
?>

ch8-1-2.php

執行程式

第02行:開啟檔案,「!」表示相反的意思;如果成功,則結果會回傳給$fp。

 

fread 讀取檔案
語法
fread ( resource handle, int length)
說明
  1. 由開啟的檔案中讀取一定長度的字串。
  2. 可配合 while 將整個檔案讀取出來。
傳回值
string
範例
01:
02:
03:
04:
05:
06:
07:
<?php
  if ( !($fp = fopen("file_ex.txt", "r")) ) {
    echo "無法開啟檔案";
  } else {
    while ( $buffer = fread($fp, 40) ) {
       echo "$buffer <BR>";
    }
  }  
?>

ch8-1-3.php

執行程式

第05行:每次讀取40個字元,一直到讀取完畢為止。
fwrite 寫入檔案
語法
fwrite ( resource handle, string string [, int length])
說明
將指定的字串寫入檔案中,如果有長度設定,則表示要寫入字串的字元數。
傳回值
int
範例
01:
02:
03:
04:
05:
06:
07:
08:

09:
<?php
  $add_str = "台中市教育資訊網路心";
  if ( !($fp = fopen("file_ex.txt", "a")) ) {
    echo "無法開啟檔案";
  } else {
      fwrite ($fp, $add_str); 
      fpassthru ($fp);
  }  
?>

ch8-1-4.php

執行程式

第03行:開啟檔案為可讀寫模式
第06行:寫入預設字串
第07行:顯示結果
fclose 關閉檔案
語法
fclose ( resource handle)
說明
關閉開啟的檔案;
傳回值
bool
copy 複製檔案
語法
copy ( string source, string dest)
說明
複製檔案,成功回傳TRUE失敗回傳FALSE;須注意檔案或目錄的權限
傳回值
bool
範例
01:
02:
03:
04:
05:
<?php
  if ( !copy("file_ex.txt", "file_ex.bak")) ) {
    echo "無法複製檔案";
  }  
?>
unlink 刪除檔案
語法
unlink ( string filename)
說明
刪除檔案,成功回傳TRUE失敗回傳FALSE;須注意檔案或目錄的權限
傳回值
bool
範例
01:
02:
03:
04:
05:
<?php
  if ( !unlink("file_ex.txt")) ) {
    echo "無法複製檔案";
  }  
?>
PHP處理檔案函數 (目前共有75個)
函數名稱
功能簡介
basename Returns filename component of path
chgrp Changes file group
chmod Changes file mode
chown Changes file owner
clearstatcache Clears file status cache
copy Copies file
delete See unlink() or unset()
dirname Returns directory name component of path
disk_free_space Returns available space in directory
disk_total_space Returns the total size of a directory
diskfreespace Alias of disk_free_space()
fclose Closes an open file pointer
feof Tests for end-of-file on a file pointer
fflush Flushes the output to a file
fgetc Gets character from file pointer
fgetcsv Gets line from file pointer and parse for CSV fields
fgets Gets line from file pointer
fgetss Gets line from file pointer and strip HTML tags
file_exists Checks whether a file or directory exists
file_get_contents Reads entire file into a string
file_put_contents Write a string to a file
file Reads entire file into an array
fileatime Gets last access time of file
filectime Gets inode change time of file
filegroup Gets file group
fileinode Gets file inode
filemtime Gets file modification time
fileowner Gets file owner
fileperms Gets file permissions
filesize Gets file size
filetype Gets file type
flock Portable advisory file locking
fnmatch Match filename against a pattern
fopen Opens file or URL
fpassthru Output all remaining data on a file pointer
fputs Alias of fwrite()
fread Binary safe file read
fscanf Parses input from a file according to a format
fseek Seeks on a file pointer
fstat Gets information about a file using an open file pointer
ftell Tells file pointer read/write position
ftruncate Truncates a file to a given length
fwrite Binary safe file write
glob Find pathnames matching a pattern
is_dir Tells whether the filename is a directory
is_executable Tells whether the filename is executable
is_file Tells whether the filename is a regular file
is_link Tells whether the filename is a symbolic link
is_readable Tells whether the filename is readable
is_uploaded_file Tells whether the file was uploaded via HTTP POST
is_writable Tells whether the filename is writable
is_writeable Alias of is_writable()
link Create a hard link
linkinfo Gets information about a link
lstat Gives information about a file or symbolic link
mkdir Makes directory
move_uploaded_file Moves an uploaded file to a new location
parse_ini_file Parse a configuration file
pathinfo Returns information about a file path
pclose Closes process file pointer
popen Opens process file pointer
readfile Outputs a file
readlink Returns the target of a symbolic link
realpath Returns canonicalized absolute pathname
rename Renames a file
rewind Rewind the position of a file pointer
rmdir Removes directory
set_file_buffer Alias of stream_set_write_buffer()
stat Gives information about a file
symlink Creates a symbolic link
tempnam Create file with unique file name
tmpfile Creates a temporary file
touch Sets access and modification time of file
umask Changes the current umask
unlink Deletes a file

php Apache MSSQL 設定

Posted: 2013 年 04 月 24 日 in php

1. 下載 AppServ (Apache 2.0 Handler, 內含PHP Version 5.2.6、MySQL 5.0.51a、Apache 2.2.8  ), 安裝並執行

2. 雖然 php 有內建 mssql 的支援函式, 但這個功能並沒有預設開啟, 因此必須修改 C:\Windows\php.ini 將" ;extension=php_mssql.dll" 前面的 (  ;  ) 去掉

3. 將 AppServ 下的兩個 dll 檔拷貝到 windows\system32 下: [ AppServ\php5\ntwdblib.dll、AppServ\php5\ext\php_mssql.dll ]

4. 由於 php 所附的 dll 可能是舊版的, 只能支援到 MS SQL 2000 所以必須重新下載 ntwdblib.dll( http://webzila.com/dll/1/ntwdblib.zip ),解壓縮之後並貼到 windows\system32 中覆蓋舊有的檔案

5. 重新啟動 appserv 中的 appache

6. 如果是 MS SQL 2005 以上的版本, 預設的 TCP/IP 和 Named 連線功能是關 閉的, 必須到 MS SQL 上將這兩個功能打開

a. 打開 SQL Server Enterprise Manager 編輯該 SQL Server 的參數 (或是到控制台下的 Administrative Tools 打開 SQL 的管理程式也可以)

b. 點選 “網路設定", 將 “具名管道" 與 TCP/IP 加入後確定即可

7. 測試連線

8.php預設的session不能直接使用,必須至php.ini中,將「output_buffering = Off」,改為「output_buffering = 4096」,不設的話,在php檔為UTF-8編碼下可能會出現以下的訊息 「 cannot send session cache limiter-headers already sent (output start at …………) 」

<html>
<head>
<title>MS SQL Connection Test</title>
</head>
<body>
<?php
    $dhcp_dbh = mssql_connect("<your ip>", "<mssql_user>", "<sql_password>") 
                or die("Can't Connect to SQL Server");
    print "connection = ".$dhcp_dbh;
?>
</body>
</html>

====== PART 2 ===============

PHP + MSSQL – Apache 設定

   Apache 及 php.ini
1. 更改 php.ini
mssql.secure_connection = On

extension=php_mssql.dll (移除 ; )2. SQL Server – 伺服器驗證 : SQL Server 及 Windows 驗證模式3. 將 \php5\ext\php_mssql.dll copy 至下列 folder.
C:\Windows\System32\4. 找尋 ntwdblib.dll (版本 2000.80.194.0 或以上), copy 至下列 folder.
C:\Windows\System32\
\php5\
\Apache\bin\5. Restart Apache 服務註 : 設定錯誤出現之 error message.
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to serve
 php 程式碼
<table border="1″ cellpadding="0″ cellspacing="0″><?php
$server = ‘(Host Name),(Port No.)’;$con=mssql_connect($server,'(SQL Server User Account Login Name)’,'(SQL Server User Account Password)’);$msdb=mssql_select_db(‘Soccer’,$con);$query = mssql_query(‘SELECT * FROM dbo.Team’);if (!mssql_num_rows($query)) {
echo ‘No records found’;
}else{
while ($row = mssql_fetch_array($query, MSSQL_NUM)) {
echo “<tr>" ;
echo “<td>" ;
echo $row[0] . “</td><td>" ;
echo $row[1] . “</td><td>" ;
echo $row[2] . “</td></tr>" ;
}
}mssql_free_result($query);?></table>

PHP url 參數

Posted: 2012 年 06 月 22 日 in php
PHP    url 參數
  • $_SERVER[PHP_SELF]:指正在執行的PHP程式名稱
  • $_SERVER[SCRIPT_NAME]:包含目前程式的路徑
  • $_SERVER[QUERY_STRING]:查詢的字串
  • $_SERVER[REQUEST_URI]:連結至目前頁面的URI

例子1:http://homeserver.com.tw/
$_SERVER[PHP_SELF]=’/index.php’
$_SERVER[SCRIPT_NAME]=’/index.php’
$_SERVER[QUERY_STRING]=』
$_SERVER[REQUEST_URI]=’/’

例子2:http://homeserver.com.tw/index.php?u=admin&p=123456
$_SERVER[PHP_SELF]=’/index.php’
$_SERVER[SCRIPT_NAME]=’/index.php’
$_SERVER[QUERY_STRING]=’u=admin&p=123456′
$_SERVER[REQUEST_URI]=’/index.php?u=admin&p=123456′
$_SERVER[QUERY_STRING]:查詢的字串,也就是獲取?後面的查詢字串
$_SERVER[REQUEST_URI]:取得網域名稱後面所有的字串,也就是http://homeserver.com.tw後面所有的字串

如果網址有經過Rewrite轉址過後,要取得目前頁面的網址就有一些差別了:
例子3:
原始的URL: http://homeserver.com.tw/view.php?id=1234
Rewrite轉址後的URL:http://homeserver.com.tw/1234.html

那麼如果要取得原始的URL,則使用(略過網域):
view.php?id=1234  =>  $_SERVER[PHP_SELF].’?’.$_SERVER[QUERY_STRING]
如果要取得轉址後的URL,則使用(略過網域):
/1234.html  =>  $_SERVER[REQUEST_URI]

php $_server

Posted: 2011 年 03 月 02 日 in php

網路上抓下來的

$_SERVER[‘PHP_SELF’] #當前正在執行腳本的文件名,與 document root相關。
$_SERVER[‘argv’] #傳遞給該腳本的參數。
$_SERVER[‘argc’] #包含傳遞給程序的命令行參數的個數(如果運行在命令行模式)。
$_SERVER[‘GATEWAY_INTERFACE’] #服務器使用的 CGI 規範的版本。例如,「CGI/1.1」。
$_SERVER[‘SERVER_NAME’] #當前運行腳本所在服務器主機的名稱。
$_SERVER[‘SERVER_SOFTWARE’] #服務器標識的字串,在響應請求時的頭部中給出。
$_SERVER[‘SERVER_PROTOCOL’] #請求頁面時通信協議的名稱和版本。例如,「HTTP/1.0」。
$_SERVER[‘REQUEST_METHOD’] #訪問頁面時的請求方法。例如:「GET」、「HEAD」,「POST」,「PUT」。
$_SERVER[‘QUERY_STRING’] #查詢(query)的字符串。
$_SERVER[‘DOCUMENT_ROOT’] #當前運行腳本所在的文檔根目錄。在服務器配置文件中定義。
$_SERVER[‘HTTP_ACCEPT’] #當前請求的 Accept: 頭部的內容。
$_SERVER[‘HTTP_ACCEPT_CHARSET’] #當前請求的 Accept-Charset: 頭部的內容。例如:「iso-8859-1,*,utf-8」。
$_SERVER[‘HTTP_ACCEPT_ENCODING’] #當前請求的 Accept-Encoding: 頭部的內容。例如:「gzip」。
$_SERVER[‘HTTP_ACCEPT_LANGUAGE’]#當前請求的 Accept-Language: 頭部的內容。例如:「en」。
$_SERVER[‘HTTP_CONNECTION’] #當前請求的 Connection: 頭部的內容。例如:「Keep-Alive」。
$_SERVER[‘HTTP_HOST’] #當前請求的 Host: 頭部的內容。
$_SERVER[‘HTTP_REFERER’] #鏈接到當前頁面的前一頁面的 URL 地址。
$_SERVER[‘HTTP_USER_AGENT’] #當前請求的 User_Agent: 頭部的內容。
$_SERVER[‘REMOTE_ADDR’] #正在瀏覽當前頁面用戶的 IP 地址。
$_SERVER[‘REMOTE_HOST’] #正在瀏覽當前頁面用戶的主機名。
$_SERVER[‘REMOTE_PORT’] #用戶連接到服務器時所使用的端口。
$_SERVER[‘SCRIPT_FILENAME’] #當前執行腳本的絕對路徑名。
$_SERVER[‘SERVER_ADMIN’] #管理員信息
$_SERVER[‘SERVER_PORT’] #服務器所使用的端口
$_SERVER[‘SERVER_SIGNATURE’] #包含服務器版本和虛擬主機名的字符串。
$_SERVER[‘PATH_TRANSLATED’] #當前腳本所在文件系統(不是文檔根目錄)的基本路徑。
$_SERVER[‘SCRIPT_NAME’] #包含當前腳本的路徑。這在頁面需要指向自己時非常有用。
$_SERVER[‘REQUEST_URI’] #訪問此頁面所需的 URI。例如,「/index.html」。
$_SERVER[‘PHP_AUTH_USER’] #當 PHP 運行在 Apache 模塊方式下,並且正在使用 HTTP 認證功能,這個變量便是用戶輸入的用戶名。
$_SERVER[‘PHP_AUTH_PW’] #當 PHP 運行在 Apache 模塊方式下,並且正在使用 HTTP 認證功能,這個變量便是用戶輸入的密碼。
$_SERVER[‘AUTH_TYPE’] #當 PHP 運行在 Apache 模塊方式下,並且正在使用 HTTP 認證功能,這個變量便是認證的類型。