一、php如何在文章列表中任意位置或固定位置插入新的文章?好比:三、6位置
二、php如何刪除兩個數組中有交集的元素?
三、php如何在數組頭部和尾部及任意位置插入元素?
四、php如何將二位數組按某一個或多個字段值(升序/降序)排序?數字索引被重置,關聯索引保持不變
五、php如何實現APP版本號的比對?
六、php如何獲取視頻封面圖?
七、php中的六種加密解密算法
八、php如何方式SQL注入?
九、php如何將模板標籤替換爲指定內容?
十、php如何獲取當前頁面的完整url?
十一、php如何強制下載文件?
十二、php截取字符串長度(含中文)
1三、php如何獲取客戶端真實IP?
1四、php如何記錄日誌信息到文件中?
1五、php如何防止重複提交表單?令牌方式javascript
一、如何在文章列表中任意位置或固定位置插入新的文章?好比:三、6位置php
- <?php
-
-
- $pageNumber = $this->input->get_post("pageNumber",true);
-
- $contentList = $this->article_model->getArticleByCategory2($cateId, 0, $offset);
- $contentList = $this->_getContentList($contentList);
-
- $cache_num = 10;
- $size = 2;
- $max_times = $cache_num/$size;
-
- if($pageNumber <= $max_times){
- $offset_1 = ($pageNumber - 1) * $size;
- }else{
- if($pageNumber % $max_times){
- $num = $this->article_model->getPublishCountArticleByCategory($cateId);
- if($num >= $cache_num){
- $offset_1 = ($pageNumber % $max_times - 1) * $size;
- }else{
- $offset_1 = $max_times * 2;
- }
- }else{
- $num = $this->article_model->getPublishCountArticleByCategory($cateId);
- if($num >= $cache_num){
- $offset_1 = $cache_num - $size;
- }else{
- $offset_1 = $max_times * 2;
- }
-
- }
- }
-
- $publishList = $this->article_model->getPublishArticleByCategory($cateId, $offset_1,$size,$cache_num);
- $publishList = $this->_getPublishList($publishList);
- $content_count = count($contentlist);
- $publish_count = count($publishlist);
- if(!empty($publishList)){
- if( ($content_count >= 3) && ($publish_count >= 1) ){
- $publishList_new[0] = $publishList[0];
- array_splice($contentList,3-1,0,$publishList_new);
- }
-
- if( ($content_count >= 6) && ($publish_count >= 2) ){
- $publishList_new[0] = $publishList[1];
- array_splice($contentList,6-1,0,$publishList_new);
- }
- }
-
- ?>
二、如何刪除兩個數組中有交集的元素?html
- foreach($content_list_temp_recommend as $k=>$v){
- $kk=array_search($v['aid'], $aid_arr_temp);
- $msg.=$aid_arr_temp[$kk].",";
- if($kk !== false){
- unset($aid_arr_temp[$kk]);
- }
- }
- $aid_arr= array_values($aid_arr_temp);
三、如何在數組頭部和尾部及任意位置插入元素?前端
- ①插入元素
- array_unshift();
- array_push();
- array_splice($arr,$start,0,$arr1);
- ②刪除元素
- array_shift();
- array_pop();
四、如何將二位數組按某一個或多個字段值(升序/降序)排序?數字索引被重置,關聯索引保持不變java
- $arr=array(
- array('id'=>1,'name'=>'will','age'=>23),
- array('id'=>2,'name'=>'myth','age'=>32),
- array('id'=>3,'name'=>'allen','age'=>27),
- array('id'=>4,'name'=>'martin','age'=>23)
- );
-
- foreach($arr as $k=>$v){
- $tag1[]=$v['age'];
- $tag2[]=$v['id'];
- }
- array_multisort($tag1,SORT_DESC,$tag2,SORT_ASC,$arr);
-
- echo "<pre>";print_r($arr);exit;
- ?>
- function arrSortByField(&$list, $field, $call_func=NULL, $sort_type=SORT_ASC){
- $sort_filed = array();
- foreach ($list as $val) {
- if (!isset($val[$field])) return false;
- $sort_filed[] = is_null($call_func) ? $val[$field] : call_user_func($call_func,$val[$field]);
- }
- return array_multisort($sort_field,$sort_type,$list);
- }
-
- $list= array(
- array('id' =>3, 'name' => 'asdfsdf'),
- array('id' =>1, 'name' => '12'),
- array('id' =>4, 'name' => '10sdf'),
- array('id' =>2, 'name' => 'ada'),
- array('id' =>5, 'name' => 'aasdfbc')
- );
-
- arrSortByField($list,'name','strlen');
- echo "<pre>";print_r($list);
- arrSortByField($list,'id');
- echo "<pre>";print_r($list);
-
-
- ?>
五、APP版本號的比web
- <?php
- header("content-type:text/html;charset=utf-8");
- date_default_timezone_set('Asia/Shanghai');
-
- function _diffVersion($current,$update){
- if($current == "null"){
- return false;
- }
-
- $currentVersion = getVersion($current);
- $updateVersion = getVersion($update);
-
- if($currentVersion['mainVersion'] < $updateVersion['mainVersion']){
- return true;
- }else if($currentVersion['mainVersion'] == $updateVersion['mainVersion']){
- if($currentVersion['minVersion'] < $updateVersion['minVersion']){
- return true;
- }else if($currentVersion['minVersion'] > $updateVersion['minVersion']){
- return false;
- }
-
- if($currentVersion['fixVersion'] < $updateVersion['fixVersion']){
- return true;
- }
- }
-
- return false;
- }
-
- function getVersion($version){
- $result = array();
- if(strstr($version,".")){
- $data = explode(".",$version);
- $result['mainVersion'] = $data[0];
- if(isset($data[1])){
- $result['minVersion'] = $data[1];
- }else{
- $result['minVersion'] = 0;
- }
-
- if(isset($data[2])){
- $result['fixVersion'] = $data[2];
- }else{
- $result['fixVersion'] = 0;
- }
- }
-
- return $result;
- }
-
-
- echo "<pre>";print_r(_diffVersion("2.0.0","2.0.01"));
-
- ?>
六、獲取視頻封面圖面試
- <?php
- header("content-type:text/html;charset=utf-8");
- date_default_timezone_set('Asia/Shanghai');
-
- function getCoverImages($fileUrl){
- $result = array();
-
- if(!empty($fileUrl)){
- $filePath = str_replace("http://img.baidu.cn/", "/data/images/", $fileUrl);
- if(is_file($filePath)){
- $result = execCommandLine($filePath);
- }
- }
- return json_encode($result);
- }
-
- function execCommandLine($file){
- $result = array();
-
- $pathParts = pathinfo($file);
- $filename = $pathParts['dirname']."/".$pathParts['filename']."_";
-
- $times = array(8,15,25);
- foreach ($times as $k => $v) {
- $destFilePath = $filename.$v.".jpg";
- $command = "/usr/bin/ffmpeg -i {$file} -y -f image2 -ss {$v} -vframes 1 -s 640x360 {$destFilePath}";
- exec($command);
-
- $destUrlPath = str_replace("/data/images/", "http://img.baidu.cn/", $destFilePath);
- $selected = $k == 0 ? "1" : "0";
- array_push($result,array($destUrlPath,$selected));
- }
-
- return $result;
- }
-
- $fileUrl="http://img.baidu.cn/14221916FLVSDT1.mp4"
- getCoverImages($fileUrl);
-
- ?>
七、php加密解密:php加密和解密函數一般能夠用來加密一些有用的字符串存放在數據庫裏或做爲各個子系統間同步登錄的令牌,而且經過解密算法解密字符串,該函數使用了base64和MD5加密和解密。算法
①第一種加密解密算法sql
- <?php
- function encryptDecrypt($key, $string, $decrypt){
- if($decrypt){
- $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");
- return $decrypted;
- }else{
- $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
- return $encrypted;
- }
- }
-
- echo encryptDecrypt('password', 'Helloweba歡迎您',0);
- echo encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);
- ?>
②第二種加密解密算法:數據庫
- <?php
- function lock_url($txt,$key='www.zhuoyuexiazai.com'){
- $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $nh = rand(0,64);
- $ch = $chars[$nh];
- $mdKey = md5($key.$ch);
- $mdKey = substr($mdKey,$nh%8, $nh%8+7);
- $txt = base64_encode($txt);
- $tmp = '';
- $i=0;$j=0;$k = 0;
- for ($i=0; $i<strlen($txt); $i++) {
- $k = $k == strlen($mdKey) ? 0 : $k;
- $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
- $tmp .= $chars[$j];
- }
- return urlencode($ch.$tmp);
- }
- function unlock_url($txt,$key='www.zhuoyuexiazai.com'){
- $txt = urldecode($txt);
- $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $ch = $txt[0];
- $nh = strpos($chars,$ch);
- $mdKey = md5($key.$ch);
- $mdKey = substr($mdKey,$nh%8, $nh%8+7);
- $txt = substr($txt,1);
- $tmp = '';
- $i=0;$j=0; $k = 0;
- for ($i=0; $i<strlen($txt); $i++) {
- $k = $k == strlen($mdKey) ? 0 : $k;
- $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
- while ($j<0) $j+=64;
- $tmp .= $chars[$j];
- }
- return base64_decode($tmp);
- }
- ?>
③第三種加密解密算法:
- <?php
-
- function lock_url($txt,$key='zhuoyuexiazai'){
- $txt = $txt.$key;
- $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $nh = rand(0,64);
- $ch = $chars[$nh];
- $mdKey = md5($key.$ch);
- $mdKey = substr($mdKey,$nh%8, $nh%8+7);
- $txt = base64_encode($txt);
- $tmp = '';
- $i=0;$j=0;$k = 0;
- for ($i=0; $i<strlen($txt); $i++) {
- $k = $k == strlen($mdKey) ? 0 : $k;
- $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
- $tmp .= $chars[$j];
- }
- return urlencode(base64_encode($ch.$tmp));
- }
- function unlock_url($txt,$key='zhuoyuexiazai'){
- $txt = base64_decode(urldecode($txt));
- $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
- $ch = $txt[0];
- $nh = strpos($chars,$ch);
- $mdKey = md5($key.$ch);
- $mdKey = substr($mdKey,$nh%8, $nh%8+7);
- $txt = substr($txt,1);
- $tmp = '';
- $i=0;$j=0; $k = 0;
- for ($i=0; $i<strlen($txt); $i++) {
- $k = $k == strlen($mdKey) ? 0 : $k;
- $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
- while ($j<0) $j+=64;
- $tmp .= $chars[$j];
- }
- return trim(base64_decode($tmp),$key);
- }
-
- ?>
④第四種加密解密算法:
- <?php
-
- function passport_encrypt($txt, $key = 'www.zhuoyuexiazai.com') {
- srand((double)microtime() * 1000000);
- $encrypt_key = md5(rand(0, 32000));
- $ctr = 0;
- $tmp = '';
- for($i = 0;$i < strlen($txt); $i++) {
- $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
- $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
- }
- return urlencode(base64_encode(passport_key($tmp, $key)));
- }
-
- function passport_decrypt($txt, $key = 'www.zhuoyuexiazai.com') {
- $txt = passport_key(base64_decode(urldecode($txt)), $key);
- $tmp = '';
- for($i = 0;$i < strlen($txt); $i++) {
- $md5 = $txt[$i];
- $tmp .= $txt[++$i] ^ $md5;
- }
- return $tmp;
- }
-
- function passport_key($txt, $encrypt_key) {
- $encrypt_key = md5($encrypt_key);
- $ctr = 0;
- $tmp = '';
- for($i = 0; $i < strlen($txt); $i++) {
- $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
- $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
- }
- return $tmp;
- }
-
-
- $txt = "1";
- $key = "testkey";
- $encrypt = passport_encrypt($txt,$key);
- $decrypt = passport_decrypt($encrypt,$key);
-
- echo $encrypt."<br>";
- echo $decrypt."<br>";
-
- ?>
⑤第五種加密解密算法:discuz中使用的加密解密算法
項目中有時咱們須要使用PHP將特定的信息進行加密,也就是經過加密算法生成一個加密字符串,這個加密後的字符串能夠經過解密算法進行解密,便於程序對解密後的信息進行處理。最多見的應用在用戶登陸以及一些API數據交換的場景。最多見的應用在用戶登陸以及一些API數據交換的場景。加密解密原理通常都是經過必定的加密解密算法,將密鑰加入到算法中,最終獲得加密解密結果。
- <?php
- function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
-
- $ckey_length = 4;
-
-
- $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);
-
-
- $keya = md5(substr($key, 0, 16));
-
- $keyb = md5(substr($key, 16, 16));
-
- $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
-
- $cryptkey = $keya.md5($keya.$keyc);
- $key_length = strlen($cryptkey);
-
-
-
- $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
- $string_length = strlen($string);
- $result = '';
- $box = range(0, 255);
- $rndkey = array();
-
- for($i = 0; $i <= 255; $i++) {
- $rndkey[$i] = ord($cryptkey[$i % $key_length]);
- }
-
- for($j = $i = 0; $i < 256; $i++) {
- $j = ($j + $box[$i] + $rndkey[$i]) % 256;
- $tmp = $box[$i];
- $box[$i] = $box[$j];
- $box[$j] = $tmp;
- }
-
- for($a = $j = $i = 0; $i < $string_length; $i++) {
- $a = ($a + 1) % 256;
- $j = ($j + $box[$a]) % 256;
- $tmp = $box[$a];
- $box[$a] = $box[$j];
- $box[$j] = $tmp;
-
- $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
- }
- if($operation == 'DECODE') {
-
- if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
- return substr($result, 26);
- } else {
- return '';
- }
- } else {
-
-
- return $keyc.str_replace('=', '', base64_encode($result));
- }
- }
-
- $str = 'abcdef';
- $key = 'www.helloweba.com';
- echo authcode($str,'ENCODE',$key,0);
- $str = '56f4yER1DI2WTzWMqsfPpS9hwyoJnFP2MpC8SOhRrxO7BOk';
- echo authcode($str,'DECODE',$key,0);
-
- ?>
⑥第六種加密解密算法:
- <?php
- function encrypt($string,$operation,$key=''){
- $key=md5($key);
- $key_length=strlen($key);
- $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string;
- $string_length=strlen($string);
- $rndkey=$box=array();
- $result='';
- for($i=0;$i<=255;$i++){
- $rndkey[$i]=ord($key[$i%$key_length]);
- $box[$i]=$i;
- }
- for($j=$i=0;$i<256;$i++){
- $j=($j+$box[$i]+$rndkey[$i])%256;
- $tmp=$box[$i];
- $box[$i]=$box[$j];
- $box[$j]=$tmp;
- }
- for($a=$j=$i=0;$i<$string_length;$i++){
- $a=($a+1)%256;
- $j=($j+$box[$a])%256;
- $tmp=$box[$a];
- $box[$a]=$box[$j];
- $box[$j]=$tmp;
- $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));
- }
- if($operation=='D'){
- if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){
- return substr($result,8);
- }else{
- return'';
- }
- }else{
- return str_replace('=','',base64_encode($result));
- }
- }
-
- $str = 'abc';
- $key = 'www.helloweba.com';
- $token = encrypt($str, 'E', $key);
- echo '加密:'.encrypt($str, 'E', $key);
- echo '解密:'.encrypt($str, 'D', $key);
-
- ?>
八、php如何方式SQL注入?咱們在查詢數據庫時,出於安全考慮,須要過濾一些非法字符防止SQL惡意注入
- <?php
- function injCheck($sql_str) {
- $check = preg_match('/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/', $sql_str);
- if ($check) {
- echo '非法字符!!';
- exit;
- } else {
- return $sql_str;
- }
- }
-
- echo injCheck('1 or 1=1');
-
- ?>
九、php如何將模板標籤替換爲指定的內容?
- <?php
-
- function stringParser($string,$replacer){
- $result = str_replace(array_keys($replacer), array_values($replacer),$string);
- return $result;
- }
-
- $string = 'The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself';
- $replace_array = array('{b}' => '<b>','{/b}' => '</b>','{br}' => '<br />');
-
- echo stringParser($string,$replace_array);
-
- ?>
十、php如何獲取當前頁面的url?如:"https://www.baidu.com/index.php?username=xiaoqiang"
- <?php
- function curPageURL() {
- $pageURL = 'http';
- if (!empty($_SERVER['HTTPS'])) {$pageURL .= "s";}
- $pageURL .= "://";
- if ($_SERVER["SERVER_PORT"] != "80") {
- $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- } else {
- $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
- }
- return $pageURL;
- }
-
- echo curPageURL();
-
- ?>
十一、php如何強制下載文件?
- <?php
- function download($filename){
- if ((isset($filename))&&(file_exists($filename))){
- header("Content-length: ".filesize($filename));
- header('Content-Type: application/octet-stream');
- header('Content-Disposition: attachment; filename="' . $filename . '"');
- readfile("$filename");
- } else {
- echo "Looks like file does not exist!";
- }
- }
-
- download('/down/test_45f73e852.zip');
-
- ?>
十二、php截取字符串長度(含中文)
- <?php
-
- function cutStr($string, $sublen, $start = 0, $code = 'UTF-8'){
- if($code == 'UTF-8'){
- $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
- preg_match_all($pa, $string, $t_string);
-
- if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
- return join('', array_slice($t_string[0], $start, $sublen));
- }else{
- $start = $start*2;
- $sublen = $sublen*2;
- $strlen = strlen($string);
- $tmpstr = '';
-
- for($i=0; $i<$strlen; $i++){
- if($i>=$start && $i<($start+$sublen)){
- if(ord(substr($string, $i, 1))>129){
- $tmpstr.= substr($string, $i, 2);
- }else{
- $tmpstr.= substr($string, $i, 1);
- }
- }
- if(ord(substr($string, $i, 1))>129) $i++;
- }
- if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
- return $tmpstr;
- }
- }
-
- $str = "jQuery插件實現的加載圖片和頁面效果";
- echo cutStr($str,16);
-
- ?>
1三、php如何獲取客戶端真實IP?
- <?php
-
- function getIp() {
- if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
- $ip = getenv("HTTP_CLIENT_IP");
- else
- if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
- $ip = getenv("HTTP_X_FORWARDED_FOR");
- else
- if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
- $ip = getenv("REMOTE_ADDR");
- else
- if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
- $ip = $_SERVER['REMOTE_ADDR'];
- else
- $ip = "unknown";
- return ($ip);
- }
-
- echo getIp();
-
- ?>
1四、php如何記錄日誌信息到文件中?
- <?php
-
- function logResult($str='') {
- $fp = fopen("log.txt","a");
- flock($fp, LOCK_EX) ;
- fwrite($fp,"執行日期:".strftime("%Y%m%d%H%M%S",time())."\n".$str."\n");
- flock($fp, LOCK_UN);
- fclose($fp);
- }
-
- logResult('獲取數據reselt=xxx');
-
- ?>
1五、php如何防止重複提交表單?
咱們提交表單的時候,不能忽視的一個限制是防止用戶重複提交表單,由於有可能用戶連續點擊了提交按鈕或者是攻擊者惡意提交數據,那麼咱們在提交數據後的處理如修改或添加數據到數據庫時就會惹上麻煩。
那麼如何規避這中重複提交表單的現象出現呢?咱們能夠從不少方面入手,首先從前端作限制。前端JavaScript在按鈕被點擊一次後禁用,即disabled,這個方法簡單的防止了屢次點擊提交按鈕,可是缺點是若是用戶禁用了javascript腳本則失效。第二,咱們能夠在提交後作redirect頁面重定向,即提交後跳轉到新的頁面,主要避免F5重複提交,可是也有不足之處。第三,就是數據庫作惟一索引約束。第四,就是作session令牌驗證。
咱們如今來了解下簡單的利用session token來防止表單重複提交的方法。
咱們在表單中加一個input隱藏域,即type="hidden",其value值用來保存token值,當頁面刷新的時候這個token值會變化,提交後判斷token值是否正確,若是前臺提交的token與後臺不匹配,則認爲是重複提交。
- <?php
-
- session_start();
- header("Content-Type: text/html;charset=utf-8");
- function set_token() {
- $_SESSION['token'] = md5(microtime(true));
- }
-
- function valid_token() {
- $return = $_REQUEST['token'] === $_SESSION['token'] ? true : false;
- set_token();
- return $return;
- }
-
- if(!isset($_SESSION['token']) || $_SESSION['token']=='') {
- set_token();
- }
-
- if(isset($_POST['web'])){
- if(!valid_token()){
- echo "token error,請不要重複提交!";
- }else{
- echo '成功提交,Value:'.$_POST['web'];
- }
- }else{
- ?>
- <form method="post" action="">
- <input type="hidden" name="token" value="<?php echo $_SESSION['token']?>">
- <input type="text" class="input" name="web" value="www.helloweba.com">
- <input type="submit" class="btn" value="提交" />
- </form>
- <?php
- }
- ?>
以上是一個簡單的防止重複提交表單的例子,僅供參考。那麼實際項目開發中,會對錶單token作更復雜的處理,即咱們說的令牌驗證。可能要作的處理有:驗證來源域,即來路,是否爲外部提交;匹配要執行的動做,是添加、修改or刪除;其次最重要的是構建token,token能夠採用可逆的加密算法,儘量複雜,由於明文仍是不安全的。令牌驗證的具體算法能夠參考各大PHP框架,如ThinkPHP提供了很好的令牌驗證功能。