Ⅰ php過濾上傳類型,只上傳圖片類型文件
你可以這樣,如果說你的上傳文件文件名是$uploadfilename,那麼可以用
$str = end(explode(".",$uploadfilename));獲得上傳文件的擴展名,專然後再進行判屬斷if($str=="jpg" or $str=="jpeg" or $str=="gif" or $str=="png")的時候才執行上傳,否則返回錯誤提示信息,這樣子來限制上傳文件類型
Ⅱ php 如何過濾用戶提交的javascript代碼
進行html標簽轉義,例如傳入內容是$content=「<script>alert('sd');</script>」,運行
$content=htmlspecialchars($content);就會將你的內容轉換成
(這回個是轉換後插進資料庫里的答數據)
這個東西放在瀏覽器顯示就是
<script>alert('sd');</script>,但是不會運行裡面的函數。
Ⅲ 用php過濾html部分標簽
$str=preg_replace("/\s+/", " ", $str); //過濾多餘回車
$str=preg_replace("/<[ ]+/si","<",$str); //過濾<__("<"號後面帶空格)
$str=preg_replace("/<\!--.*?-->/si","",$str); //注釋
$str=preg_replace("/<(\!.*?)>/si","",$str); //過濾DOCTYPE
$str=preg_replace("/<(\/?html.*?)>/si","",$str); //過濾html標簽
$str=preg_replace("/<(\/?head.*?)>/si","",$str); //過濾head標簽
$str=preg_replace("/<(\/?meta.*?)>/si","",$str); //過濾meta標簽
$str=preg_replace("/<(\/?body.*?)>/si","",$str); //過濾body標簽
$str=preg_replace("/<(\/?link.*?)>/si","",$str); //過濾link標簽
$str=preg_replace("/<(\/?form.*?)>/si","",$str); //過濾form標簽
$str=preg_replace("/cookie/si","COOKIE",$str); //過濾COOKIE標簽
$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str); //過濾applet標簽
$str=preg_replace("/<(\/?applet.*?)>/si","",$str); //過濾applet標簽
$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str); //過濾style標簽
$str=preg_replace("/<(\/?style.*?)>/si","",$str); //過濾style標簽
$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str); //過濾title標簽
$str=preg_replace("/<(\/?title.*?)>/si","",$str); //過濾title標簽
$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str); //過濾object標簽
$str=preg_replace("/<(\/?objec.*?)>/si","",$str); //過濾object標簽
$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str); //過濾noframes標簽
$str=preg_replace("/<(\/?noframes.*?)>/si","",$str); //過濾noframes標簽
$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str); //過濾frame標簽
$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str); //過濾frame標簽
$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str); //過濾script標簽
$str=preg_replace("/<(\/?script.*?)>/si","",$str); //過濾script標簽
$str=preg_replace("/javascript/si","Javascript",$str); //過濾script標簽
$str=preg_replace("/vbscript/si","Vbscript",$str); //過濾script標簽
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //過濾script標簽
$str=preg_replace("//si","&#",$str); //過濾script標簽,如javAsCript:alert(
清除空格,換行
function DeleteHtml($str)
{
$str = trim($str);
$str = strip_tags($str,"");
$str = ereg_replace("\t","",$str);
$str = ereg_replace("\r\n","",$str);
$str = ereg_replace("\r","",$str);
$str = ereg_replace("\n","",$str);
$str = ereg_replace(" "," ",$str);
return trim($str);
}
過濾HTML屬性
1,過濾所有html標簽的正則表達式:
復制代碼 代碼如下:
</?[^>]+>
//過濾所有html標簽的屬性的正則表達式:
$html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$html);
3,過濾部分html標簽的正則表達式的排除式(比如排除<p>,即不過濾<p>):
復制代碼 代碼如下:
</?[^pP/>]+>
4,過濾部分html標簽的正則表達式的枚舉式(比如需要過濾<a><p><b>等):
復制代碼 代碼如下:
</?[aApPbB][^>]*>
5,過濾部分html標簽的屬性的正則表達式的排除式(比如排除alt屬性,即不過濾alt屬性):
復制代碼 代碼如下:
\s(?!alt)[a-zA-Z]+=[^\s]*
6,過濾部分html標簽的屬性的正則表達式的枚舉式(比如alt屬性):
復制代碼 代碼如下:
(\s)alt=[^\s]*
Ⅳ php多條件查詢問題,怎麼過濾空值
是的正如你所說有兩種解決方案,第一種是採用PHP的方式,先判斷數據的合法性,比如是否提交了空值(推薦使用,沒有垃圾數據);第二種不做任何判斷,空值也能提交到資料庫,只是在SQL查詢時過濾空值數據(不推薦使用,有垃圾數據)。
解決方案1(通過PHP過濾空值數據):
if($_POST['欄位']=='')exit('<scripttype="text/javascript">alert("數據不合法!");history.back();</script>');//依此類推,逐一判斷表單$_POST數據
解決方案2(通過SQL查詢來過濾空值數據):
SELECT`欄位名`FROM`表名`WHERE`欄位1`NOTIN('',NULL)AND`欄位2`NOTIN('',NULL)...;
以上就是兩種解決方案,我推薦兩種方案組合使用,這樣確保萬無一失,如有問題歡迎追問~
Ⅳ PHP if ($_GET['key'] 過濾
如果是特殊的需要key匹配的特點去寫正則匹配.例如
$w='kjhgerz123z1iuhiugerhhg';
$s='kjhgerz2z1iuhiugerhhg';
$m='/z[0-9]{2,}/';
if(preg_match($m,$w)){
echo"出現關鍵字";
}else{
echo"關鍵字未出現過";
}
if(preg_match($m,$s)){
echo"出現關鍵字";
}else{
echo"關鍵字未出現過";
}
Ⅵ 分別在Javascript和php下過濾+-=._/外
var reg=/^[\w\+\-\=\.\/]+$/;
var str="123";
alert(reg.test(str));
這個正則應該可以了。
經過javascript的簡單測試。
Ⅶ php怎樣過濾span標簽之後的所有內容
$str="111<span>asdfsadfsafsd</span>";
//$str=preg_replace("/<(span.*?)>(.*?)<(\/span.*?)>/si","",$str); //過濾<span>及其中內間的內容容
$str=preg_replace("/<span>(.*?)<span>/si","",$str); //只過濾<span>中間的內容不過濾<span>
echo $str;
Ⅷ php怎樣過濾非法字元防止sql注入
htmlspecialchars($_POST['欄位']),用這個函數就可以將一些特殊字元進行過濾轉義。你可以去看看這個函數的說明。