PHP’nin ne kadar esnek ve kullanımı kolay bir dil olduğunu her fırsatta söylerim. Bu kolaylıklardan birisi de e-posta adreslerinin kontrolünde geçerli. Bu kontrolü yapabileceğiniz iki örnek vermek istiyorum. Birisi standart bir Regex uygulaması, diğeri ise hem yazım kuralını kontrol eden hem de eposta adresinde belirtilen adresin erişebilirliğini test eden FILTER_VALIDATE_EMAIL kullanan bir kontrol. Artık hangisinin kullanacağınız size kalmış ;)
PHP Regex ile E-posta Kontrolü
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php function epostaKontrol($eposta) { if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$eposta)) { list($username,$domain)= explode('@',$eposta); if(!checkdnsrr($domain,'MX')) { return false;} return true; } return false; } $eposta ="xxx@xxx.com"; //Buraya eposta adresini giriniz if (epostaKontrol($eposta)) { echo "E-posta adresi geçerli :)"; } else { echo "Hatalı E-posta adresi :("; } ?> |
PHP FILTER_VALIDATE_EMAIL ile E-posta Kontrolü
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php function epostaKontrol($eposta) { //Eposta adresinin yazım kontrolü if(!filter_var($eposta, FILTER_VALIDATE_EMAIL)) { return false; } //Host (alanadı) kontrolü list($user, $host) = explode("@", $eposta); if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A")) { return false; } return true; } $eposta ="xxx@xxx.com"; //Buraya eposta adresini giriniz if (epostaKontrol($eposta)) { echo "E-posta adresi geçerli :)"; } else { echo "Hatalı E-posta adresi :("; } ?> |