php面試常用知識_第1頁
php面試常用知識_第2頁
php面試常用知識_第3頁
php面試常用知識_第4頁
php面試常用知識_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、1、在 php中如何修改session 的生存時間答: 方法 1: 將 php.ini中的 session.gc_maxlifetime設(shè)置為 9999 重啟 apache 方法 2:$savepath = ./session_save_dir/; $lifetime = 小時 * 秒; session_save_path($savepath); session_set_cookie_params($lifetime); session_start(); kuin*do g 方法 3:setcookie() and session_set_cookie_params($lifetime); 2

2、、在 php 中顯示客戶端ip 與服務(wù)器 ip 的的方法打印客戶端ip:echo $_serverremote_addr; 或者: getenv(remote_addr); 打印服務(wù)器ip:echo gethostbyname() 如果想記錄一次訪問者ip, 我們可以把客戶端的ip 地址獲取 , 寫入到 session中, 加上判斷語句就可以了3、淺談如何優(yōu)化mysql 數(shù)據(jù)庫1、 選取最適用的字段屬性, 盡可能減少定義字段長度, 盡量把字段設(shè)置not null,例如 省份 ,性別 , 最好設(shè)置為enum 2、使用連接(join)來代替子查詢: a. 刪除沒有任何訂單客

3、戶:delete from customerinfo where customerid not in(select customerid from orderinfo) b. 提取所有沒有訂單客戶:select from customerinfo where customerid not in(select customerid from orderinfo) c. 提高 b 的速度優(yōu)化 :select from customerinfo left join orderid customerinfo.customerid=orderinfo.customerid where orderinfo

4、.customerid is null 3、使用聯(lián)合 (union)來代替手動創(chuàng)建的臨時表a. 創(chuàng)建臨時表 :select name from nametest union select username from nametest2 4、事務(wù)處理 : a. 保證數(shù)據(jù)完整性,例如添加和修改同時, 兩者成立則都執(zhí)行, 一者失敗都失敗mysql_query(begin); mysql_query(insert into customerinfo (name) values ($name1); mysql_query(select * from orderinfo where customerid=

5、.$id); mysql_query(commit); 5、鎖定表 , 優(yōu)化事務(wù)處理: a. 我們用一個 select 語句取出初始數(shù)據(jù),通過一些計算, 用 update 語句將新值更新到表中。包含有 write 關(guān)鍵字的 lock table 語句可以保證在 unlock tables 命令被執(zhí)行之前,不會有其它的訪問來對 inventory 進行插入、更新或者刪除的操作mysql_query(lock table customerinfo read, orderinfo write); mysql_query(select customerid from customerinfo wher

6、e id=.$id); mysql_query(update orderinfo set ordertitle=$title where customerid=.$id); mysql_query(unlock tables); 6、使用外鍵 , 優(yōu)化鎖定表 b-a a. 把 customerinfo里的 customerid映射到 orderinfo里的 customerid, 任何一條沒有合法的customerid的記錄不會寫到orderinfo里create table customerinfo ( customerid int not null, primary key(custome

7、rid) )type = innodb; create table orderinfo ( orderid int not null, customerid int not null, primary key(customerid,orderid), foreign key (customerid) references customerinfo (customerid) on delete cascade )type = innodb; 注意 :on delete cascade, 該參數(shù)保證當(dāng)customerinfo表中的一條記錄刪除的話同時也會刪除 order 表中的該用戶的所有記錄,

8、注意使用外鍵要定義事務(wù)安全類型為innodb; 7、建立索引 : a. 格式 : ( 普通索引 ) 創(chuàng)建 :create index on tablename (索引字段 ) 修改 :alter table tablename add index 索引名 ( 索引字段 ) 創(chuàng)表指定索引 :create table tablename(.,index索引名 ( 索引字段 ) ( 唯一索引 ) 創(chuàng)建 :create unique on tablename (索引字段 ) 修改 :alter table tablename add unique 索引名 (索引字段 ) 創(chuàng)表指定索引 :create

9、table tablename(.,unique索引名 ( 索引字段 ) ( 主鍵 ) 它是唯一索引 , 一般在創(chuàng)建表是建立, 格式為 : creata table tablename (.,primary key索引字段 ) 8、優(yōu)化查詢語句a. 最好在相同字段進行比較操作, 在建立好的索引字段上盡量減少函數(shù)操作例子 1: select * from order where year(orderdate)2008;(慢) select * from order where orderdate2008-01-01;(快) 例子 2: select * from order where addt

10、ime/724;(慢) select * from order where addtime=good and namegood; 4、如何處理php的錯誤信息如果錯誤信息中包含了服務(wù)器的信息,我們就會需要使用php中的字符“ ”隱藏這條函數(shù)代碼。例如: 當(dāng)然,錯誤信息如下:warning: fopen() expects at least 2 parameters, 1 given in e:wamppcycfphptest.php on line 2 warning: fclose() expects parameter 1 to be resource, boolean given in

11、e:xampphtdocsphptest9.4.1.php on line3 如果我們是在服務(wù)器上出錯,這樣輸出錯誤信息就會暴漏服務(wù)器的路徑。我們就會想到, 要隱藏這個路徑。打開 php的報錯功能:修改 php.ini文件,用查找方法找到 display_errors=off 并修改成display_errors=on 然后重新啟動web 服務(wù),如果不知道如何重啟服務(wù),那就重新啟動計算機好了, 雖說這是比較可笑的做法,但對于初學(xué)者來說,比較好用例如: 一個 屏蔽一條語句的錯誤信息,可以發(fā)現(xiàn)什么都木有。定制錯誤的信息“ ”雖然可以隱藏信息,但是這樣不利于用戶的友好體驗,因為加了這條php代碼之后

12、,phpdo 的當(dāng)前狀態(tài)也被屏蔽了。所以,我們就會想,如果能夠定制網(wǎng)站的錯誤信息就好了。例如: 結(jié)果:不存在“exit ”有什么用呢?如果,不在代碼加入這個語句,結(jié)果是什么呢?不存在warning: fclose() expects parameter 1 to be resource, boolean given in e:xampphtdocsphptest9.4.3.php on line11 可以看到flos函數(shù)被執(zhí)行了,執(zhí)行的結(jié)果當(dāng)然是輸出錯誤。由此得知, exit函數(shù)會退出當(dāng)前腳本的執(zhí)行。網(wǎng)站 404 頁面就是這么來的。php 處理超時錯誤在 php中,可以使用set_time_l

13、imit函數(shù)延長腳本的運行時間。語法如下:void set_time_limit(int seconds) seconds 表示要延長的秒數(shù)。例如:?php / 使用 $_get獲取循環(huán)此時$times = (int)$_gettimes; set_time_limit(1); /剛開始的腳本時間for($i = 0;$i $times;$i+) echo $i. ” ”; if($times%100=0) set_time_limit(1+$i); ? 5、php 文章詳細頁面分頁方法我們在 php開發(fā)中 , 經(jīng)常用到將php字符串轉(zhuǎn)換為數(shù)組進行操作, 用的最多的就是在文章內(nèi)容分頁時所用函數(shù)

14、explode() php函數(shù) explode 的格式如下:array explode(string separator, string string,int limit); 2. 參數(shù)中的separator為分隔符, string為待分割的字符串,limit為返回的數(shù)組中元素的最大個數(shù)。6、什么是 smarty 模板引擎smarty 是一個 php模板引擎,它是使用php語言開發(fā)的,發(fā)展到今天已經(jīng)成為一個非常流行的模板引擎。smarty 提供了一種易于管理和使用的方法,將php代碼從 html 頁面中分離出來,這種做法使得 php程序員和頁面設(shè)計者之間的分工更加明確,php程序員只需專注于業(yè)

15、余邏輯的實現(xiàn),頁面設(shè)計者只需專注于頁面美工的設(shè)計。因此,團隊的整體開發(fā)效率會得到很大程序的提高。samrty 優(yōu)點:速度快:和其他模板引擎相比,使用smarty 的程序運行速度更快豐富的標簽:smarty 提供了一系列豐富的標簽來處理數(shù)據(jù)。編譯型: 默認情況下smarty 會將模板編譯成php文件, 再次訪問該模板時,請求會被轉(zhuǎn)換到已經(jīng)編譯的php文件中,因此訪問速度會很快。緩存: smarty 提供了緩存技術(shù),可緩存靜態(tài)html頁面,還可指定緩存時間。安全:在smarty 中可設(shè)置安全選項,用于防止惡間的攻擊。插件:插件實質(zhì)上是一些自定義函數(shù)smarty 可以自定義插件7、smarty 的緩

16、存操作smarty 緩存機制smarty 中一個非常有用的特性就是它提供緩存在機制, 利用緩存很多程序上提高web 應(yīng)用程序性能開啟緩存 :設(shè)置 smarty 緩存屬性$smarty-caching=true;/將 caching設(shè)置為 true 或者為 1 緩存的生命周期在默認情況下 , 每一個緩存的頁面的生命周期為3600 秒, 如果想修改緩存的生命周期, 可通過設(shè)計 $cache_lifetime屬性的值來設(shè)置$smarty-caching=true;/開啟緩存$smarty-cache_lifetime=200;/設(shè)置緩存的生命周期清除緩存在 smarty 中 , 可使用 clear_

17、all_cache()方法來清除所有的緩存, 或者使用clear_cache()方法來清除指定的緩存文件$smarty-clear_all_cache();/清除所有的緩存文件$smarty- clear_cache( “index.html ”);/只清除index.html模板的緩存8、php 如何刪除目錄下的文件和文件夾?php function deldirandfile( $dirname ) /定義函數(shù)名 if ( $handle = opendir( $dirname ) ) /打開文件夾 while ( false != ( $item = readdir( $handle )

18、 ) ) if ( $item != . & $item != . ) if ( is_dir( $dirname/$item ) ) / rmdir( $dirname/$item ); else if( unlink( $dirname/$item ) )echo 成功刪除文件: $dirname/$itemn; closedir( $handle ); if( rmdir( $dirname ) )echo 成功刪除目錄: $dirnamen; / 下面是使用此函數(shù)刪除php目錄下的所有文件和文件夾,包含php 文件夾的deldirandfile(php); ? 9、php 解決中文亂碼問題首先我們要連接數(shù)據(jù)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論