e-mail validation

Σε αυτή την περιοχή μπορείτε να βρείτε ή να αναζητήσετε πληροφορίες σχετικές με την PHP

Συντονιστές: WebDev Moderators, Super-Moderators, PHP Moderators

Απάντηση
Άβαταρ μέλους
xmavidis
Honorary Member
Δημοσιεύσεις: 1217
Εγγραφή: 02 Δεκ 2003 19:59
Τοποθεσία: Ηράκλειο

e-mail validation

Δημοσίευση από xmavidis » 19 Αύγ 2006 17:26

Έχω μια κλάση (που τη βρήκα εδώ) αλλά δεν έχω καταλάβει τον τρόπο χρήσης της. Μπορεί κάποιος να βοηθήσει;

Κώδικας: Επιλογή όλων

<?php
class email_validation_class
&#123;
   var $email_regular_expression="^&#40;&#91;-!#\$%&'*+./0-9=?A-Z^_`a-z&#123;|&#125;~&#93;&#41;+@&#40;&#91;-!#\$%&'*+/0-9=?A-Z^_`a-z&#123;|&#125;~&#93;+\\.&#41;+&#91;a-zA-Z&#93;&#123;2,6&#125;\$";
   var $timeout=0;
   var $data_timeout=0;
   var $localhost="";
   var $localuser="";
   var $debug=0;
   var $html_debug=0;
   var $exclude_address="";
   var $getmxrr="GetMXRR";

   var $next_token="";
   var $preg;
   var $last_code="";

   Function Tokenize&#40;$string,$separator=""&#41;
   &#123;
      if&#40;!strcmp&#40;$separator,""&#41;&#41;
      &#123;
         $separator=$string;
         $string=$this->next_token;
      &#125;
      for&#40;$character=0;$character<strlen&#40;$separator&#41;;$character++&#41;
      &#123;
         if&#40;GetType&#40;$position=strpos&#40;$string,$separator&#91;$character&#93;&#41;&#41;=="integer"&#41;
            $found=&#40;IsSet&#40;$found&#41; ? min&#40;$found,$position&#41; &#58; $position&#41;;
      &#125;
      if&#40;IsSet&#40;$found&#41;&#41;
      &#123;
         $this->next_token=substr&#40;$string,$found+1&#41;;
         return&#40;substr&#40;$string,0,$found&#41;&#41;;
      &#125;
      else
      &#123;
         $this->next_token="";
         return&#40;$string&#41;;
      &#125;
   &#125;

   Function OutputDebug&#40;$message&#41;
   &#123;
      $message.="\n";
      if&#40;$this->html_debug&#41;
         $message=str_replace&#40;"\n","<br />\n",HtmlEntities&#40;$message&#41;&#41;;
      echo $message;
      flush&#40;&#41;;
   &#125;

   Function GetLine&#40;$connection&#41;
   &#123;
      for&#40;$line="";;&#41;
      &#123;
         if&#40;feof&#40;$connection&#41;&#41;
            return&#40;0&#41;;
         $line.=fgets&#40;$connection,100&#41;;
         $length=strlen&#40;$line&#41;;
         if&#40;$length>=2
         && substr&#40;$line,$length-2,2&#41;=="\r\n"&#41;
         &#123;
            $line=substr&#40;$line,0,$length-2&#41;;
            if&#40;$this->debug&#41;
               $this->OutputDebug&#40;"S $line"&#41;;
            return&#40;$line&#41;;
         &#125;
      &#125;
   &#125;

   Function PutLine&#40;$connection,$line&#41;
   &#123;
      if&#40;$this->debug&#41;
         $this->OutputDebug&#40;"C $line"&#41;;
      return&#40;fputs&#40;$connection,"$line\r\n"&#41;&#41;;
   &#125;

   Function ValidateEmailAddress&#40;$email&#41;
   &#123;
      if&#40;IsSet&#40;$this->preg&#41;&#41;
      &#123;
         if&#40;strlen&#40;$this->preg&#41;&#41;
            return&#40;preg_match&#40;$this->preg,$email&#41;&#41;;
      &#125;
      else
      &#123;
         $this->preg=&#40;function_exists&#40;"preg_match"&#41; ? "/".str_replace&#40;"/", "\\/", $this->email_regular_expression&#41;."/" &#58; ""&#41;;
         return&#40;$this->ValidateEmailAddress&#40;$email&#41;&#41;;
      &#125;
      return&#40;eregi&#40;$this->email_regular_expression,$email&#41;!=0&#41;;
   &#125;

   Function ValidateEmailHost&#40;$email,&$hosts&#41;
   &#123;
      if&#40;!$this->ValidateEmailAddress&#40;$email&#41;&#41;
         return&#40;0&#41;;
      $user=$this->Tokenize&#40;$email,"@"&#41;;
      $domain=$this->Tokenize&#40;""&#41;;
      $hosts=$weights=array&#40;&#41;;
      $getmxrr=$this->getmxrr;
      if&#40;function_exists&#40;$getmxrr&#41;
      && $getmxrr&#40;$domain,$hosts,$weights&#41;&#41;
      &#123;
         $mxhosts=array&#40;&#41;;
         for&#40;$host=0;$host<count&#40;$hosts&#41;;$host++&#41;
            $mxhosts&#91;$weights&#91;$host&#93;&#93;=$hosts&#91;$host&#93;;
         KSort&#40;$mxhosts&#41;;
         for&#40;Reset&#40;$mxhosts&#41;,$host=0;$host<count&#40;$mxhosts&#41;;Next&#40;$mxhosts&#41;,$host++&#41;
            $hosts&#91;$host&#93;=$mxhosts&#91;Key&#40;$mxhosts&#41;&#93;;
      &#125;
      else
      &#123;
         if&#40;strcmp&#40;$ip=@gethostbyname&#40;$domain&#41;,$domain&#41;
         && &#40;strlen&#40;$this->exclude_address&#41;==0
         || strcmp&#40;@gethostbyname&#40;$this->exclude_address&#41;,$ip&#41;&#41;&#41;
            $hosts&#91;&#93;=$domain;
      &#125;
      return&#40;count&#40;$hosts&#41;!=0&#41;;
   &#125;

   Function VerifyResultLines&#40;$connection,$code&#41;
   &#123;
      while&#40;&#40;$line=$this->GetLine&#40;$connection&#41;&#41;&#41;
      &#123;
         $this->last_code=$this->Tokenize&#40;$line," -"&#41;;
         if&#40;strcmp&#40;$this->last_code,$code&#41;&#41;
            return&#40;0&#41;;
         if&#40;!strcmp&#40;substr&#40;$line, strlen&#40;$this->last_code&#41;, 1&#41;," "&#41;&#41;
            return&#40;1&#41;;
      &#125;
      return&#40;-1&#41;;
   &#125;

   Function ValidateEmailBox&#40;$email&#41;
   &#123;
      if&#40;!$this->ValidateEmailHost&#40;$email,$hosts&#41;&#41;
         return&#40;0&#41;;
      if&#40;!strcmp&#40;$localhost=$this->localhost,""&#41;
      && !strcmp&#40;$localhost=getenv&#40;"SERVER_NAME"&#41;,""&#41;
      && !strcmp&#40;$localhost=getenv&#40;"HOST"&#41;,""&#41;&#41;
         $localhost="localhost";
      if&#40;!strcmp&#40;$localuser=$this->localuser,""&#41;
      && !strcmp&#40;$localuser=getenv&#40;"USERNAME"&#41;,""&#41;
      && !strcmp&#40;$localuser=getenv&#40;"USER"&#41;,""&#41;&#41;
         $localuser="root";
      for&#40;$host=0;$host<count&#40;$hosts&#41;;$host++&#41;
      &#123;
         $domain=$hosts&#91;$host&#93;;
         if&#40;ereg&#40;'^&#91;0-9&#93;&#123;1,3&#125;\.&#91;0-9&#93;&#123;1,3&#125;\.&#91;0-9&#93;&#123;1,3&#125;\.&#91;0-9&#93;&#123;1,3&#125;$',$domain&#41;&#41;
            $ip=$domain;
         else
         &#123;
            if&#40;$this->debug&#41;
               $this->OutputDebug&#40;"Resolving host name \"".$hosts&#91;$host&#93;."\"..."&#41;;
            if&#40;!strcmp&#40;$ip=@gethostbyname&#40;$domain&#41;,$domain&#41;&#41;
            &#123;
               if&#40;$this->debug&#41;
                  $this->OutputDebug&#40;"Could not resolve host name \"".$hosts&#91;$host&#93;."\"."&#41;;
               continue;
            &#125;
         &#125;
         if&#40;strlen&#40;$this->exclude_address&#41;
         && !strcmp&#40;@gethostbyname&#40;$this->exclude_address&#41;,$ip&#41;&#41;
         &#123;
            if&#40;$this->debug&#41;
               $this->OutputDebug&#40;"Host address of \"".$hosts&#91;$host&#93;."\" is the exclude address"&#41;;
            continue;
         &#125;
         if&#40;$this->debug&#41;
            $this->OutputDebug&#40;"Connecting to host address \"".$ip."\"..."&#41;;
         if&#40;&#40;$connection=&#40;$this->timeout ? @fsockopen&#40;$ip,25,$errno,$error,$this->timeout&#41; &#58; @fsockopen&#40;$ip,25&#41;&#41;&#41;&#41;
         &#123;
            $timeout=&#40;$this->data_timeout ? $this->data_timeout &#58; $this->timeout&#41;;
            if&#40;$timeout
            && function_exists&#40;"socket_set_timeout"&#41;&#41;
               socket_set_timeout&#40;$connection,$timeout,0&#41;;
            if&#40;$this->debug&#41;
               $this->OutputDebug&#40;"Connected."&#41;;
            if&#40;$this->VerifyResultLines&#40;$connection,"220"&#41;>0
            && $this->PutLine&#40;$connection,"HELO $localhost"&#41;
            && $this->VerifyResultLines&#40;$connection,"250"&#41;>0
            && $this->PutLine&#40;$connection,"MAIL FROM&#58; <$localuser@$localhost>"&#41;
            && $this->VerifyResultLines&#40;$connection,"250"&#41;>0
            && $this->PutLine&#40;$connection,"RCPT TO&#58; <$email>"&#41;
            && &#40;$result=$this->VerifyResultLines&#40;$connection,"250"&#41;&#41;>=0&#41;
            &#123;
               if&#40;$result&#41;
               &#123;
                  if&#40;$this->PutLine&#40;$connection,"DATA"&#41;&#41;
                     $result=&#40;$this->VerifyResultLines&#40;$connection,"354"&#41;!=0&#41;;
               &#125;
               else
               &#123;
                  if&#40;strlen&#40;$this->last_code&#41;
                  && !strcmp&#40;$this->last_code&#91;0&#93;,"4"&#41;&#41;
                     $result=-1;
               &#125;
               if&#40;$this->debug&#41;
                  $this->OutputDebug&#40;"This host states that the address is ".&#40;$result ? &#40;$result>0 ? "valid" &#58; "undetermined"&#41; &#58; "not valid"&#41;."."&#41;;
               fclose&#40;$connection&#41;;
               if&#40;$this->debug&#41;
                  $this->OutputDebug&#40;"Disconnected."&#41;;
               return&#40;$result&#41;;
            &#125;
            if&#40;$this->debug&#41;
               $this->OutputDebug&#40;"Unable to validate the address with this host."&#41;;
            fclose&#40;$connection&#41;;
            if&#40;$this->debug&#41;
               $this->OutputDebug&#40;"Disconnected."&#41;;
         &#125;
         else
         &#123;
            if&#40;$this->debug&#41;
               $this->OutputDebug&#40;"Failed."&#41;;
         &#125;
      &#125;
      return&#40;-1&#41;;
   &#125;
&#125;;

?>
Και ο κώδικας της φόρμας

Κώδικας: Επιλογή όλων

myemail   = "webmaster@site.com"; // type your e-mail address
$subject   = "Contact from site.com\n"; 



if &#40;isset&#40;$_POST&#91;'submit'&#93;&#41;&#41; &#123;

		
		if &#40;!isset&#40;$_POST&#91;'name'&#93;&#41; || !isset&#40;$_POST&#91;'email'&#93;&#41; || !isset&#40;$_POST&#91;'subject'&#93;&#41; || !isset&#40;$_POST&#91;'text'&#93;&#41;&#41;
		&#123;
				if &#40;!isset&#40;$_POST&#91;'name'&#93;&#41;&#41; &#123;
					echo "Name ?";
				&#125;
				
				if &#40;!isset&#40;$_POST&#91;'email'&#93;&#41;&#41; &#123;
					echo "E-Mail ?";
			    &#125;
					
					
				&#125;
				if &#40;!isset&#40;$_POST&#91;'subject'&#93;&#41;&#41; &#123;
					echo "Subject ?";
				&#125;
				if &#40;!isset&#40;$_POST&#91;'text'&#93;&#41;&#41; &#123;
					echo "Text ?.";
				&#125;

		else 
		&#123;

$name = htmlspecialchars&#40;$_POST&#91;'name'&#93;, ENT_QUOTES&#41;;
$email = $_POST&#91;'email'&#93;;
$website = htmlspecialchars&#40;$_POST&#91;'website'&#93;, ENT_QUOTES&#41;;
$text  = htmlspecialchars&#40;$_POST&#91;'text'&#93;. ENT_QUOTES&#41;;

if &#40;$website="http&#58;//" ||$website=""&#41; &#123;
	
	$website="No website";

&#125;

$target_email = $email; 

$headers .= "MIME-Version&#58; 1.0\n";
$headers .= "Content-type&#58; text/html; charset=iso-8859-7\n";
$headers .= "X-Priority&#58; 1\n";
$headers .= "X-MSMail-Priority&#58; High\n";
$headers .= "X-Mailer&#58; php\n";
$headers .= "From&#58; $target_email <$target_email>\n";

// create the message
$recipient = $myemail; 

// start the creation of message
$msg .= "<span style=\"font-family&#58; \"Trebuchet MS\", Arial, sans-serif; font-size&#58; 9pt; color&#58; #800000; \">";
$msg .= "<b>Name&#58;</b> ". $name."<br />\n";
$msg .= "<b>E-Mail&#58;</b> ". $email."<br/>\n";
$msg .= "<b>Website&#58;</b> ". $website."<br />\n";
$msg .= "<b>Text&#58;</b><br />". $text."\n";
$msg .= "</span>";

// send the message
	if &#40;mail &#40;$recipient, $subject, $msg, $headers&#41;&#41;
		&#123;
		echo "Your message has been sent!\n"; 
		&#125; else &#123;
		echo "Problem.\n";
		&#125;
&#125; 
&#125;
?>
I want to know God’s thoughts; the rest are details.
Albert Einstein

Άβαταρ μέλους
xmavidis
Honorary Member
Δημοσιεύσεις: 1217
Εγγραφή: 02 Δεκ 2003 19:59
Τοποθεσία: Ηράκλειο

e-mail validation

Δημοσίευση από xmavidis » 19 Αύγ 2006 17:50

Το πρόβλημα λύθηκε με τη βοήθεια του rapid-eraser. :P
I want to know God’s thoughts; the rest are details.
Albert Einstein

Άβαταρ μέλους
cherouvim
Script Master
Δημοσιεύσεις: 3137
Εγγραφή: 13 Ιούλ 2005 22:56
Τοποθεσία: Athens, Greece
Επικοινωνία:

e-mail validation

Δημοσίευση από cherouvim » 19 Αύγ 2006 18:41

Κώδικας: Επιλογή όλων

Function Tokenize&#40;...

Κώδικας: Επιλογή όλων

if&#40;IsSet&#40;$found&#41;&#41;... 
Από coding style καλά πάμε...

Άβαταρ μέλους
xmavidis
Honorary Member
Δημοσιεύσεις: 1217
Εγγραφή: 02 Δεκ 2003 19:59
Τοποθεσία: Ηράκλειο

e-mail validation

Δημοσίευση από xmavidis » 19 Αύγ 2006 22:13

Δεν έγραψα εγώ την κλάση...
I want to know God’s thoughts; the rest are details.
Albert Einstein

Άβαταρ μέλους
cherouvim
Script Master
Δημοσιεύσεις: 3137
Εγγραφή: 13 Ιούλ 2005 22:56
Τοποθεσία: Athens, Greece
Επικοινωνία:

e-mail validation

Δημοσίευση από cherouvim » 19 Αύγ 2006 22:14

Το ξέρω. Γενικά το λέω :)

Απάντηση

Επιστροφή στο “PHP Προγραμματισμός”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες