title: PHP Email change script
body: <p>I am trying to do some little script for changing email in my free services. Users are registered with name, password, etc. What I want is to add web form for changing their email in database with PHP script which allows them to change their email with process of confirmation to old email. How should it work?</p>
  
  <p>User enters to some site (log in to it) click on change email and there will be fields with old email, newemail and again newemail2. There will be checks for old email is really the old email in databse, newemail is the same as newemail2. I have done some checks for validity of email etc here:</p>
  
  <pre><code>    if ($auth-&gt;isLogged())
  {
      if(!empty($email))
      {
          if ($this-&gt;checkEmail($mail))
          {           
              $this-&gt;db-&gt;selectDb("login");            
              if (!mysql_result(mysql_query("SELECT COUNT(id) FROM account WHERE email='".$mail."'"), 0) &gt; 0)
              {
  </code></pre>
  
  <p>I created script- which uses php changing email with database and site on one page with php self functions like this:</p>
  
  <pre><code>$login_db_new_connect = mysql_connect($logindb[db_host],$logindb[db_username],$logindb[db_password]);
  $selectdb = mysql_select_db($logindb[db_name],$login_db_new_connect); 
      if ($_GET[h] &amp;&amp; $_GET[h] != '' &amp;&amp; $_GET[h] != '0')
      {
          $emailz = $newemail1;
          $query = mysql_query("SELECT username FROM `account` WHERE reset_email='$_GET[h]'");
          $res = mysql_fetch_array($query);
          $check = mysql_query("SELECT email FROM `account` WHERE reset_email='$_GET[h]'");
          $ress = mysql_fetch_array($check);
          $check1 = mysql_query("SELECT reset_email2 FROM `account` WHERE reset_email='$_GET[h]'");
          $resss = mysql_fetch_array($check1);
          $emailz = $resss[reset_email2];
  
          if (mysql_num_rows($query) == 1)
          {
              echo "&lt;b&gt;E-mail was changed.&lt;/b&gt;";
              $to = $ress[email];
              $headers = "Headers here";  
              $message = "Email has been Changed (more text to come later)";
              mail($to, '', $message, $headers);
              mysql_query("UPDATE account SET email='$emailz' WHERE reset_email='$_GET[h]'") or die ('query change ...');
              mysql_query("UPDATE account SET reset_email='', reset_email2='' WHERE username='$res[username]'") or die ('query ...');
          }
          else
          {
              echo "&lt;b&gt;Error not changed.&lt;/b&gt;";     
          }
      }
      else
      {  
          $oldemail = addslashes($_POST["oldemail"]);
          $newemail1 = addslashes($_POST["newemail1"]);
          $newemail2 = addslashes($_POST["newemail2"]);
          if ($oldemail == "" || $newemail1 == "" || $newemail2 == "")
          {
              $reason =  "
              &lt;form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\"&gt;
              &lt;table width=\"310\"&gt;
              &lt;tr&gt;&lt;td&gt;Old e-mail:&lt;/td&gt;&lt;td&gt;&lt;input type=\"email\" name=\"oldemail\"&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;New e-mail:&lt;/td&gt;&lt;td&gt;&lt;input type=\"email\" name=\"newemail1\"&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;New e-mail again:&lt;/td&gt;&lt;td&gt;&lt;input type=\"email\" name=\"newemail2\"&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td align=\"center\" colspan=\"2\"&gt;&lt;br&gt;&lt;input type=\"submit\" value=\"Change e-mail\"&gt;
              &lt;/table&gt;
              &lt;/form&gt;";
          }
          else
          {   
              $login_db_new_connect = mysql_connect($logindb[db_host],$logindb[db_username],$logindb[db_password]);
              $selectdb = mysql_select_db($logindb[db_name],$login_db_new_connect);
              $queryy = mysql_query("SELECT username FROM `account` WHERE id='$id'");
              $resu = mysql_fetch_array($queryy);  
              $sql = "SELECT * FROM account WHERE email='$oldemail' AND username='$resu[username]'";
              $result = mysql_query($sql);
              $result2 = mysql_num_rows($result);
  
              if ($result2 !== 1)
              {
                  $reason = "Something is wrong";
              }
              else
              {
                  if ($newemail1 == $newemail2)
                  {
                      $emailz = $newemail1;       
                      $rand = random_string(40);
                      $sql1 = "UPDATE `account` SET reset_email='$rand' WHERE id='$id'";
                      mysql_query($sql1) or die ('query ...');
                      $sql2 = "UPDATE `account` SET reset_email2='$newemail1' WHERE id='$id'";
                      mysql_query($sql2) or die ('query ...');
                      $to = $_POST["oldemail"];
                      $headers = "";  
                      $message = "Activation link: $config[path_to_thisfile]?h=$rand\n\n\n";
                      mail($to, '', $message, $headers);
                      echo "&lt;b&gt;activation link has been sent&lt;/b&gt;";
                  }
                  else
                  {                                                                                  
                      echo "&lt;b&gt;wrong input data&lt;/b&gt;";
                  }
              }
          } 
      }
  ?&gt;
  </code></pre>
  
  <p>But i want there more checks like i mentioned above for checking email integrity and validate, check if user is online... And maybe my script is not fully ok :D</p>
  
  <p>So I am open to your ideas now :) Thank you.</p>