function sending email twice
<p>I have the following function sending an email twice (and I believe running if($result) twice).</p>

<p>it is called on a separate page :</p>

<pre><code>&lt;?php $User = new User();
  $User-&gt;ValidReg();
  $valid = $User-&gt;ValidReg();
  if ($valid === false) {
</code></pre>

<p>Here is the function in its class:</p>

<pre><code>public function ValidReg() {
  if (  !empty($_POST['username'])
     &amp;&amp; !empty($_POST['password'])
     &amp;&amp; !empty($_POST['email']) 
     &amp;&amp; !empty($_POST['state'])) {

    //valid ?
    $valid = true;

    $username = mysql_real_escape_string($_POST['username']);
    $password = md5(mysql_real_escape_string($_POST['password']));
    $email = mysql_real_escape_string($_POST['email']);
    $firstname = mysql_real_escape_string($_POST['firstname']);
    $lastname = mysql_real_escape_string($_POST['lastname']);
    $state = mysql_real_escape_string($_POST['state']);

    $checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'");

    if(mysql_num_rows($checkusername) == 1) {
      echo "&lt;div id='shopperlogin1'&gt;&lt;p&gt;Sorry, that username is taken.&lt;br /&gt; Please go back and try again.&lt;/p&gt;&lt;/div&gt;";
    }
    else {
      //test
      $confirm_code=mysql_real_escape_string(md5(uniqid(rand())));

      $sql="INSERT INTO temp_users (
              confirm_code, Username, Password, 
              EmailAddress, FirstName, LastName, State) 
            VALUES (
              '$confirm_code', '$username', '$password', 
              '$email', '$firstname', '$lastname', '$state')";
      $result=mysql_query($sql) 
        or die ("Query failed: " . mysql_error() . " Actual query: " . $query);

      // if suceesfully inserted data into database, send confirmation link to email
      if ($result) {

        // send e-mail to ...
        $to=$email;

        // Your subject
        $subject="Your confirmation link here";

        // From
        $header="blahblah@blahbalh.com";

        // Your message
        $message="Your Comfirmation link \r\n";
        $message.="Click on this link to activate your account \r\n";
        $message.="http://www.employeediscounted.com/secret/login.php?passkey=$confirm_code";

        // send email
        $sentmail = mail($to,$subject,$message,$header);
      }

      // if not found
      else {
        echo "&lt;div id='emailmsg'&gt;Not found your email in our database.&lt;/div&gt;";
      }

      // if your email succesfully sent
      if($sentmail){
        echo "&lt;div id='emailmsg'&gt;Your Confirmation link Has Been Sent To Your Email Address.&lt;/div&gt;";
      }
      else {
        echo "&lt;div id='emailmsg'&gt;Cannot send Confirmation link to your e-mail address.&lt;/div&gt;";
      }
    }
  }
  else {
     $valid = false;
  }
  return $valid;
}
</code></pre>
