Merhaba,
Bildğiniz üzere TC kimlik no sorgulama web servisi açıldıktan bir süre sonra paralı olarak hizmet vermeye başladı. Eğer kullanıcılardan alacağımız TC kimlik no bizim için önemli ise bunun kontrolünü gerçekleştirmemiz gerekmektedir. Bu object ile girilen TC kimlik numarasının algoritmasını kontrol ederek, en azından geçerli bir TC kimlik no mu? değil mi? bunu bilebiliriz.
Eğer web servisi hizmeti alıyorsanız bile geçersiz sorgu istekleri ile ekstra ücret ödemek istemiyorsanız, sorguyu web servisine göndermeden önce bu object ile algoritmasını kontrol etmenizi tavsiye ederim.
TC Kimlik No Controller Object v0.1;
<?php
/**
* Tc Kimlik No Algoritma Kontrolü
* @author Hakan DAMAR
* @link http://www.hakandamar.com
*/
namespace Securty{
class Controller{
private static $tcNo;
protected $errCount;
public function __construct(){
// maybe use later...
}
protected function _tcNoCheck(){
//Default Error Counter
$this->errCount = 0;
// For Controller
$isDouble = is_double(self::$tcNo);
$size = strlen(self::$tcNo);
$type = gettype(self::$tcNo);
// First Controller
if(self::$tcNo == "" || self::$tcNo == null || self::$tcNo == 0){
$this->errCount = 5;
}else if($size != 11 && $type != "integer"){
// Data Type Size Controller
$this->errCount = 1;
}else if($type == "integer"){
// Type convert controller
$this->errCount = 3;
}else if($isDouble != true) {
// Data Type Controller
$this->errCount = 2;
}else{
// TC NO's
$tc1;$tc2;$tc3;$tc4;$tc5;$tc6;
$tc7;$tc8;$tc9;$tc10;$tc11;
$tc1 = substr(self::$tcNo,0,1);
$tc2 = substr(self::$tcNo,1,1);
$tc3 = substr(self::$tcNo,2,1);
$tc4 = substr(self::$tcNo,3,1);
$tc5 = substr(self::$tcNo,4,1);
$tc6 = substr(self::$tcNo,5,1);
$tc7 = substr(self::$tcNo,6,1);
$tc8 = substr(self::$tcNo,7,1);
$tc9 = substr(self::$tcNo,8,1);
$tc10 = substr(self::$tcNo,9,1);
$tc11 = substr(self::$tcNo,10,1);
//First Algo. Checks
$algoCheck1 = (($tc1 + $tc3 + $tc5 + $tc7 + $tc9) * 7);
$algoCheck2 = abs(((($tc2 + $tc4 + $tc6 + $tc8) - $algoCheck1) % 10));
$algoCheck3 = (($tc1 + $tc2 + $tc3 + $tc4 + $tc5 + $tc6 + $tc7 + $tc8 + $tc9 + $tc10) % 10);
if($algoCheck2 != $tc10){
$this->errCount = 4;
}
if($algoCheck3 != $tc11){
$this->errCount = 4;
}
}
// Error Controller
$errControl = self::errorHandler($this->errCount);
if($errControl != 200){ // 200 HTTP OK!
print($errControl);
exit;
}else{
$result = "<font color='green' size='+2'><b>GEÇERLİ T.C. Kimlik No.</b></font>";
//$result = true;
return $result;
}
}
protected function errorHandler(){
$errID = $this->errCount;
switch ($errID) {
case 0:
$err = 200;
break;
case 1:
$err = "<font color='red' size='+3'><b>T.C. kimlik no 11 haneli olmak zorundadır!</b></font>";
break;
case 2:
$err = "<font color='red' size='+3'><b>T.C. kimlik no sadece sayısal girilmelidir!</b></font>";
break;
case 3:
$err = "<font color='red' size='+3'><b>T.C. kimlik no 0(sıfır) ile başlayamaz ve 11 hane olmak zorundadır!</b></font>";
break;
case 4:
$err = "<font color='red' size='+3'><b>Geçersiz T.C. kimlik no!!!</b></font>";
break;
case 5:
$err = "<font color='red' size='+3'><b>Bu alan boş bırakılamaz!</b></font>";
break;
default:
throw new Exception("<font color='red' size='+3'><b>Beklenmedik bir hata meydana geldi!</b></font>");
break;
}
return $err;
}
public function _get(){
return self::_tcNoCheck();
}
public function _set($tcNo){
self::$tcNo = $tcNo;
}
}
}
?>
Kullanımı;
namespace {
$tcController = new \Securty\Controller();
$tcController->_set(92108806946);
print($tcController->_get());
}
Bir kontrol için bu kadar çok kod olması gözünüzü korkutmasın.
1 kontrolü benim pc’im de 0.2ms’de gerçekleştiriyor.
Söz konusu bir server olunca bu süre dahada kısalacaktır.
İyi Çalışmalar.