idocsidocs
پنج شنبه 11 آذر 1389, 21:27 عصر
لطفا بگید چطور می تونیم این کد رو بهینه تر کنیم؟
سوال اولی که برای خودم پیش اومد این بود که آیا این کد می تونه باعث کاهش سرعت اجرای اسکریپت بشه؟
$string = strtolower($_POST[textarea]);
/*
در اين قسمت از اسکريپت، تمام کاراکترهايي که براي مای اس کیو ال معني خاصي دارن به معادل اچ تی ام ال خودشون تبديل مي شن تا نتونن به پايگاه داده آسيب برسونن.
*/
//$old[]=NULL; $new[]=oo;
$old[]='!'; $new[]='!';
$old[]='"'; $new[]='"';
//$old[]="#"; $new[]='#';
$old[]='$'; $new[]='$';
$old[]='%'; $new[]='%';
//$old[]='&'; $new[]='&';
$old[]='\''; $new[]=''';
$old[]='('; $new[]='(';
$old[]=')'; $new[]=')';
$old[]='*'; $new[]='*';
$old[]='+'; $new[]='+';
$old[]=','; $new[]=',';
$old[]='-'; $new[]='-';
$old[]='.'; $new[]='.';
$old[]='/'; $new[]='/';
$old[]=':'; $new[]=':';
//$old[]=';'; $new[]='&#59;';
$old[]='<'; $new[]='&#60;';
$old[]='='; $new[]='&#61;';
$old[]='>'; $new[]='&#62;';
$old[]='?'; $new[]='&#63;';
$old[]='@'; $new[]='&#64;';
$old[]='['; $new[]='&#91;';
$old[]='\\'; $new[]='&#92;';
$old[]=']'; $new[]='&#93;';
$old[]='^'; $new[]='&#94;';
$old[]='`'; $new[]='&#96;';
$old[]='{'; $new[]='&#123;';
$old[]='|'; $new[]='&#124;';
$old[]='}'; $new[]='&#125;';
$old[]='~'; $new[]='&#126;';
/*
در اين قسمت از اسکريپت، حروف اول کلمات رزرو شده (پر استفاده) که براي مای اس کیو ال معني خاصي دارن به معادل اچ تی ام ال خودشون تبديل مي شن تا اين کلمات برای مای اس کیو ال بی معنی بشن و نتونن به پايگاه داده آسيب برسونن.
*/
$old[]='alter'; $new[]='&#97;lter';
$old[]='and'; $new[]='&#97;nd';
$old[]='change'; $new[]='&#99;hange';
$old[]='create'; $new[]='&#99;reate';
$old[]='delete'; $new[]='&#100;elete';
$old[]='drop'; $new[]='&#100;rop';
$old[]='grant'; $new[]='&#103;rant';
$old[]='group'; $new[]='&#103;roup';
$old[]='index'; $new[]='&#105;ndex';
$old[]='insert'; $new[]='&#105;nsert';
$old[]='key'; $new[]='&#107;ey';
$old[]='like'; $new[]='&#108;ike';
$old[]='limit'; $new[]='&#108;imit';
$old[]='order'; $new[]='&#111;rder';
$old[]='or'; $new[]='&#111;r';
$old[]='rename'; $new[]='&#114;ename';
$old[]='revoke'; $new[]='&#114;evoke';
$old[]='schema'; $new[]='&#115;chema';
$old[]='select'; $new[]='&#115;elect';
$old[]='set'; $new[]='&#115;et';
$old[]='show'; $new[]='&#115;how';
$old[]='sql'; $new[]='&#115;ql';
$old[]='table'; $new[]='&#116;able';
$old[]='then'; $new[]='&#116;hen';
$old[]='to'; $new[]='&#116;o';
$old[]='union'; $new[]='&#117;nion';
$old[]='update'; $new[]='&#117;pdate';
$old[]='usage'; $new[]='&#117;sage';
$old[]='values'; $new[]='&#118;alues';
$old[]='write'; $new[]='&#119;rite';
$old[]='xor'; $new[]='&#120;or';
$newstring = str_replace($old,$new,$string);
سوال اولی که برای خودم پیش اومد این بود که آیا این کد می تونه باعث کاهش سرعت اجرای اسکریپت بشه؟
$string = strtolower($_POST[textarea]);
/*
در اين قسمت از اسکريپت، تمام کاراکترهايي که براي مای اس کیو ال معني خاصي دارن به معادل اچ تی ام ال خودشون تبديل مي شن تا نتونن به پايگاه داده آسيب برسونن.
*/
//$old[]=NULL; $new[]=oo;
$old[]='!'; $new[]='&#33;';
$old[]='"'; $new[]='&#34;';
//$old[]="#"; $new[]='&#35;';
$old[]='$'; $new[]='&#36;';
$old[]='%'; $new[]='&#37;';
//$old[]='&'; $new[]='&#38;';
$old[]='\''; $new[]='&#39;';
$old[]='('; $new[]='&#40;';
$old[]=')'; $new[]='&#41;';
$old[]='*'; $new[]='&#42;';
$old[]='+'; $new[]='&#43;';
$old[]=','; $new[]='&#44;';
$old[]='-'; $new[]='&#45;';
$old[]='.'; $new[]='&#46;';
$old[]='/'; $new[]='&#47;';
$old[]=':'; $new[]='&#58;';
//$old[]=';'; $new[]='&#59;';
$old[]='<'; $new[]='&#60;';
$old[]='='; $new[]='&#61;';
$old[]='>'; $new[]='&#62;';
$old[]='?'; $new[]='&#63;';
$old[]='@'; $new[]='&#64;';
$old[]='['; $new[]='&#91;';
$old[]='\\'; $new[]='&#92;';
$old[]=']'; $new[]='&#93;';
$old[]='^'; $new[]='&#94;';
$old[]='`'; $new[]='&#96;';
$old[]='{'; $new[]='&#123;';
$old[]='|'; $new[]='&#124;';
$old[]='}'; $new[]='&#125;';
$old[]='~'; $new[]='&#126;';
/*
در اين قسمت از اسکريپت، حروف اول کلمات رزرو شده (پر استفاده) که براي مای اس کیو ال معني خاصي دارن به معادل اچ تی ام ال خودشون تبديل مي شن تا اين کلمات برای مای اس کیو ال بی معنی بشن و نتونن به پايگاه داده آسيب برسونن.
*/
$old[]='alter'; $new[]='&#97;lter';
$old[]='and'; $new[]='&#97;nd';
$old[]='change'; $new[]='&#99;hange';
$old[]='create'; $new[]='&#99;reate';
$old[]='delete'; $new[]='&#100;elete';
$old[]='drop'; $new[]='&#100;rop';
$old[]='grant'; $new[]='&#103;rant';
$old[]='group'; $new[]='&#103;roup';
$old[]='index'; $new[]='&#105;ndex';
$old[]='insert'; $new[]='&#105;nsert';
$old[]='key'; $new[]='&#107;ey';
$old[]='like'; $new[]='&#108;ike';
$old[]='limit'; $new[]='&#108;imit';
$old[]='order'; $new[]='&#111;rder';
$old[]='or'; $new[]='&#111;r';
$old[]='rename'; $new[]='&#114;ename';
$old[]='revoke'; $new[]='&#114;evoke';
$old[]='schema'; $new[]='&#115;chema';
$old[]='select'; $new[]='&#115;elect';
$old[]='set'; $new[]='&#115;et';
$old[]='show'; $new[]='&#115;how';
$old[]='sql'; $new[]='&#115;ql';
$old[]='table'; $new[]='&#116;able';
$old[]='then'; $new[]='&#116;hen';
$old[]='to'; $new[]='&#116;o';
$old[]='union'; $new[]='&#117;nion';
$old[]='update'; $new[]='&#117;pdate';
$old[]='usage'; $new[]='&#117;sage';
$old[]='values'; $new[]='&#118;alues';
$old[]='write'; $new[]='&#119;rite';
$old[]='xor'; $new[]='&#120;or';
$newstring = str_replace($old,$new,$string);