TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-22-2008, 02:10 PM   #1 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default checking email address domains

currently i'm checking with this method:

$email = "godjesus@hotmail.com";
$domain = explode( "@", $email );
if(($domain[1] == "yahoo.com") || ($domain[1] == "hotmail.com")){
echo "its either yahoo or hotmail";
}

is there a better way of doing this or is this the most optimized way?
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 05-22-2008, 04:43 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I don't know what you're after exactly. However, I would put the list of acceptable domains in an array, that way you don't have to keep adding to your if statement. I would take the following approach, using objects so that you can extend if need be, and keep it nice and tidy.

php Code:
class Validate_Email
{
    private $m_szEmail;
    private $m_szDomain;
   
    public function __construct($szEmail)
    {
        $this->m_szEmail = $szEmail;
    }
   
    public function hasValidDomain()
    {
        $aAllowedDomains = array('yahoo', 'hotmail', 'msn', 'gmail');
        preg_match('~.+?@(?P<domain>.+?)\..+~', $this->m_szEmail, $aMatches);
       
        $this->m_szDomain = $aMatches['domain'];
   
        if(!in_array($this->m_szDomain, $aAllowedDomains))
        {
            return false;
        }
       
        return true;
    }
   
    public function getDomain()
    {
        return ucfirst($this->m_szDomain);
    }
}

Then you would simply use it like so. I've put this in a loop so we can check an array of them.

php Code:
$aEmails = array('messiah@hotmail.com', 'buddha@hushmail.com', 'horus@yahoo.co.uk');

foreach ($aEmails as $szEmail)
{
    $pEmail = new Validate_Email($szEmail);
   
    if(!$pEmail->hasValidDomain())
    {
        printf('%s is an unacceptable domain.<br />', $pEmail->getDomain());
        continue;
    }
       
    printf('%s is an acceptable domain.<br />', $pEmail->getDomain());
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
webosb (05-22-2008)
Old 05-27-2008, 07:33 PM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

if your on a linux platform you could use the checkdnsrr() function:

PHP Code:
$email 'user@domain.com';

$domain=explode('@'$email);

if(!
checkdnsrr($domain[1], 'MX')) //MX = mail exchange
{
    return 
false;

__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
webosb (11-21-2008)
Old 05-27-2008, 10:23 PM   #4 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

If you want to check domain availability (such as ping the host), try the fsockopen function.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:52 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design