อยากเขียนสคริป PHP ไว้กรองข้อความต่างๆ ที่ไม่ต้องการครับ ช่วยที

เริ่มโดย หูกาง, 26 กันยายน 2008, 00:51:57

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

หูกาง

คือว่าจะรับค่ามาจากอีกหน้าหนึ่ง ( รับข้อมูลแบบ $_GET[] ) ที่ไม่แน่ใจว่ามันจะส่งอะไรบ้าๆ บอๆ มาบ้าง เลยอยากจะกรองเอาเฉพาะคำที่อยากจะได้ครับ โดยเราจะกรองแบบมีเงื่อนไขดังนี้ครับ

- ไม่เอาข้อมูลที่มีอักขระต่างๆ บนมา เช่น /,\,[,],@,#,!,{,},&,฿ เป็นต้น
- ไม่เอาข้อมูลที่มีข้อความขึ้นต้นด้วย http

ซึ่งถ้าเป็นข้อมูลพวกนี้ส่งมาจะไม่สนใจเลยครับ (ไม่เก็บลงฐานข้อมูล) แต่ถ้านอกเหนือนี้เก็บหมดครับ...ผมลองเขียน (ตามที่พอรู้) แล้วแต่มันไม่ยอมกรองทั้งหมดที่บอกไปด้านบนนั้นครับมีหลุดๆ มาเยอะมากต้องตามไปลบทุกวัน เหนื่อย...อิอิ

ขอบคุณครับ
:-[
[direct=http://easylotto.in.th]สูตรเลขยี่กี สูตรหวย สูตรเลขหุ้น[/direct]

tommy009

[direct=http://www.lokiyarok.com/]E-Books English learning[/direct] | [direct=http://americanfootballfin.blogspot.com/]Football[/direct] | [direct=http://www.google.com/]I love Google[/direct] |[direct=http://superstarcute.blogspot.com/]Super star[/direct]

ThaNaButS

<?
$input = "/,\,[,],@,#,!,{,},&,฿,http";
$output = str_replace('http', '', $input);
$output = str_replace('@', '', $output);
$output = str_replace('#', '', $output);
$output = str_replace('/', '', $output);
$output = str_replace('\\', '', $output);
$output = str_replace('[', '', $output);
$output = str_replace(']', '', $output);
$output = str_replace('!', '', $output);
$output = str_replace('{', '', $output);
$output = str_replace('}', '', $output);
$output = str_replace('&', '', $output);
$output = str_replace('฿', '', $output);
echo $output; //,,,,,,,,,,,
?>


ประมาณนี้ได้ไหมคับ  ;D ;D ;D วิธีดิบๆเลยยยย

ลองดู http://php.deeserver.net/download/get/21/banword.zip อาจช่วยได้คับ

ขออภัยมือใหม่หัดเขียน

หูกาง

อ้างถึงจาก: ThaNaButS ใน 26 กันยายน 2008, 09:58:45
<?
$input = "/,\,[,],@,#,!,{,},&,฿,http";
$output = str_replace('http', '', $input);
$output = str_replace('@', '', $output);
$output = str_replace('#', '', $output);
$output = str_replace('/', '', $output);
$output = str_replace('\\', '', $output);
$output = str_replace('[', '', $output);
$output = str_replace(']', '', $output);
$output = str_replace('!', '', $output);
$output = str_replace('{', '', $output);
$output = str_replace('}', '', $output);
$output = str_replace('&', '', $output);
$output = str_replace('฿', '', $output);
echo $output; //,,,,,,,,,,,
?>


ประมาณนี้ได้ไหมคับ  ;D ;D ;D วิธีดิบๆเลยยยย

ลองดู http://php.deeserver.net/download/get/21/banword.zip อาจช่วยได้คับ

ขออภัยมือใหม่หัดเขียน

พอไหวๆ อิอิ ขอบคุณครับ
[direct=http://easylotto.in.th]สูตรเลขยี่กี สูตรหวย สูตรเลขหุ้น[/direct]

ball6847

<?php

function sanitized_data($text){
   if(
preg_match('/^http/' $text)) return false;
   return 
preg_replace('/[^0-9a-z\.\_\-]/i','',$text);
}


////////////////////////////////////////

// USAGE EXAMPLE
if($_GET['xxx'] = sanitized_data($_GET['xxx'])) {
    
// INSERT TO DATABASE
}

?>


การทำงานของฟังก์ชั่นคือ ขั้นแรกเช็คข้อความก่อนว่าขึ้นต้นด้วย http หรือไม่ ถ้าใช่ return false
ต่อมาทำการตัดอักขระที่ไม่ใช่ตัวที่ต้องการออกให้หมด แล้ว return ผลลัพธ์กลับไป


เพิ่มเติม

preg_replace($pattern,$replace,$original);

preg_replace() เอาไว้แทนคำหรือตัดคำด้วย regular expression
$pattern คือ regular expression

'/[^0-9a-z\.\_\-]/i' หมายความว่า ทุกอักขระที่ไม่ใช่ เลข 0-9 ไม่ใช่ a-z ไม่ใช่ . ไม่ใช่ _ ไม่ใช่ -
ตัว i ท้าย pattern หมายถึง ตรวจสอบ pattern ในโหมด case insensitive

$replace คือ ตัวที่จะเอามาแทน ในกรณีนี้จะตัดออกเลยใช้ ''
$original คือ ข้อความที่เอามาดำเนินการ

ฟังก์ชั่นที่เกี่ยวข้อง

preg_match
preg_replace
และก็วิธีใช้ regex pattern เบื้องต้นน้องโจ้เอามาสอนแล้วใน webdev หาเอานะครับ


ปล. คงไม่เห็นว่าผมประเคนมากเกินไปนะ ขอให้รวยๆคับ
We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]

ball6847

ไหนๆแล้วขออีก 1

เกี่ยวกับการใช้ฟังก์ชั่น str_replace()

อากิวเม้นท์ที่เอามาใช้ใน str_replace สามารถเป็น array ได้ เพราะงั้นเราก็ไม่จำเป็นต้องใช้ str_replace หลายๆครั้ง

แบบนี้

<?php

function remove_bad_chars($text){
    
$bad_chars = array('/' '\\' '[' ']' '@' '#' '!' '{' '}' '&' '฿' 'http');
    return 
str_replace($bad_chars '' $text);
}

?>


ทำงานได้เหมือนด้านบนแต่สั้นขึ้นเป็นกอง

เทคนิคนี้ได้มาจากคุณ payu ขอขอบคุณอีกทีคับ
We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]

หูกาง

[direct=http://easylotto.in.th]สูตรเลขยี่กี สูตรหวย สูตรเลขหุ้น[/direct]

หูกาง

สคริปท่าน ball6847 ใช้ได้ดีเลยครับ :'(

แต่ตอนนี้มีปัญหาใหม่คือมันไม่ยอมกรองช่องว่างครับ เช่น เวลา user ป้อนช่องว่างปล่าวมา แล้วกด  submit มันก็ยังบันทึกลงฐานข้อมูลทั้งๆ ที่ผมก็กรองในระดับหนึ่งแล้วนะครับด้วยสคริปนี้


$a=$_GET[xxx];

if ($a=="")

{ echo "no data";}

else

{ add to database}


งง ครับว่าหลุดมาได้อย่างใด?
[direct=http://easylotto.in.th]สูตรเลขยี่กี สูตรหวย สูตรเลขหุ้น[/direct]

ball6847


<?php

function sanitized_data($text){
   if(empty(
$text) || preg_match('/^http/' $text)) return false;
   return 
preg_replace('/[^0-9a-z\.\_\-]/i','',$text);
}


////////////////////////////////////////

// USAGE EXAMPLE
if(!empty($_GET['xxx']) && $_GET['xxx'] = sanitized_data($_GET['xxx'])) {
    
// INSERT TO DATABASE
}

?>



มะได้บอกนี่นา เอิ้กๆ  :D
กรอง 2 ชั้นดูซิว่ามันจะผ่านมาได้มั้ย อิอิ   ;D
We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]

หูกาง

ขอบคุณอีกครั้งครับ
ไว้ผมจะลองไปประยุคใช้งานดู แหมะ...ถ้าไม่ได้ท่านเข้าช่วยนี่ผมงมโข่งเป็นเดือนๆ แน่เลยกว่าจะหาวิธีได้.

:'(
[direct=http://easylotto.in.th]สูตรเลขยี่กี สูตรหวย สูตรเลขหุ้น[/direct]

awat

.NET Developer, Cloud computing Developer
รับทำ component joomla, wordpress, drupal

EThaiZone

อ้างถึงจาก: หูกาง ใน 27 กันยายน 2008, 10:17:39
สคริปท่าน ball6847 ใช้ได้ดีเลยครับ :'(

แต่ตอนนี้มีปัญหาใหม่คือมันไม่ยอมกรองช่องว่างครับ เช่น เวลา user ป้อนช่องว่างปล่าวมา แล้วกด  submit มันก็ยังบันทึกลงฐานข้อมูลทั้งๆ ที่ผมก็กรองในระดับหนึ่งแล้วนะครับด้วยสคริปนี้


$a=$_GET[xxx];

if ($a=="")

{ echo "no data";}

else

{ add to database}


งง ครับว่าหลุดมาได้อย่างใด?

ใช้ trim กรองค่าที่รับมาก่อนนำมาใช้ครับ
ถ้ามีแต่ช่องว่าง มันจะกรองทิ้งจนไม่มี
พอเราไปใช้กับที่คุณบอลเขียน มันก็จะ false ไม่ผ่านให้ครับ