جند سوال در مورد معتبر سازی و فیل.تر داده ها و جلوگیری از sql injection

iran-TEC

Member
سلام دوست عزیز
من بطور کلی از تابع زیر در برنامه هام برای فیلتر ورودی استفاده می کنم که بسیار عالی هم کار می کنه

PHP:
public function getSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    switch ($theType) {
       case "text":
         $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
         break;    
       case "long":
       case "int":
         $theValue = ($theValue != "") ? intval($theValue) : "NULL";
         break;
       case "double":
         $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
         break;
       case "date":
        $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
        break;
      case "defined":
        $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
        break;
    }
    return $theValue;
  }
که بصورت زیر استفاده می شه مثلا برای دستور insert

PHP:
$sql = sprintf("INSERT INTO tbladvertisment (name,address,link,position,status) VALUES (%s,%s,%s,%s,%s)",
                         getSQLValueString($_POST['name'],'text'),
                         getSQLValueString($address,'text'),
                         getSQLValueString($_POST['link'],'text'),
                         getSQLValueString($_POST['position'],'int'),
                         getSQLValueString($_POST['status'],'int'));
موفق و پیروز باشید .
 
آخرین ویرایش:

P.H.P

Member
سلام

استفاده از توابع filter_var به شکل صحیح میتونه کافی باشه

تو وبلاگم یه پست در این مورد دادم و نحوه کار

در مورد سوال 4، برای textarea باید کاری کنی که هیچگونه تگی ثبت نشه، یا تگ های اضافه فیلتر بشه، برای \r\n میتونی از تابع nl2br برای افزودن خط جدید استفاده کنی

موفق
 

جدیدترین ارسال ها

بالا