在php中激活用户表单电子邮件

时间:2014-04-20 13:05:18

标签: php email login registration

我正在尝试为员工制作注册页面。 一旦员工注册激活链接发送到管理员电子邮件,一旦管理员点击该链接,员工应该被激活。并且消息应该发送给员工电子邮件,他现在可以登录到他的帐户...到目前为止,我编写了一个代码,用于在数据库中存储员工详细信息,并在管理员电子邮件中发送消息继承我的代码。

<?php  

#database coding
if(!empty($_POST['txtfstname']) && !empty($_POST['txtlstname']) &&     !empty($_POST['txtemail']) && !empty($_POST['txtempno']))  
        {
    $con=mysqli_connect("servername","username",'password',"database");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }


    // escape variables for security
    $firstname = mysqli_real_escape_string($con, $_POST['txtfstname']);
    $lastname = mysqli_real_escape_string($con, $_POST['txtlstname']);
        $empno = mysqli_real_escape_string($con, $_POST['txtempno']);
            $pass = substr(hash('sha256', mt_rand()), 0, 50);
    $email = mysqli_real_escape_string($con, $_POST['txtemail']);
            $email_code = md5($_POST['txtfstname'] + microtime());
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
                                {  

                                }
            else
                {
             $sql="INSERT INTO empreg (first_name, last_name, email, emp_no, password, email_code)
             VALUES ('$firstname', '$lastname','$email','$empno','$pass','$email_code')";

             if (!mysqli_query($con,$sql))
               {
              die('Error: ' . mysqli_error($con));
               }
                echo "1 record added";
       }
mysqli_close($con);



#email Coding
# It's best to just make sure each element isn't empty, using the empty() function.  
# Note that if a field is not set, it is NULL, which is consdered empty, so there  
# is no need to use the isset() function as well. 


            $firstname = trim(stripslashes(htmlspecialchars($_POST['txtfstname']))); 
            $lastname = trim(stripslashes(htmlspecialchars($_POST['txtlstname']))); 
            $email = trim(stripslashes(htmlspecialchars($_POST['txtemail'])));
            $ip = $_SERVER['REMOTE_ADDR']; 
                            if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))  
                    {   
                    # Return Error - Invalid Email 
                    $msg = 'The email you have entered is invalid, please try again.';  
                    } 
                else 
                                {  
                # Sending the Email Message 
                # I moved this into the else clause. By putting it outside it was getting 
                # sent even if the email was invalid, which isn't exactly a good idea :)
                $to = 'adminemail@something.com'; // Send email to receipient
                $subject = 'Employee Registration'; // Give the email a subject
 $message = 'From: ' . $firstname . "\r\r\n" . $lastname . "\r\r\r\n" . 'IP Address: ' . $ip; // Our message above including who sent it

$message_body .= "Please click on link to activate Employee \n\n email=$email   $email_code ";


                # Here I am capturing the return value of the mail() function so that I
                # can check if it was actually successful. Just because the function is
                # executed does not mean the email was sent. The return value tells us
                # whether it was or not.
                $success = mail($to,$subject,$message_body); // Send our email
                if($success) 
                    {
                        # The email was sent successfully!
                        $msg = 'Thank you for your message.';
                    }
                else 
                    {
                        # Email failed to send.
                        $msg = 'An error occured. The mail could not be sent.';
                    }
            }
        }

    else if (!isset($_POST['submit']))
        {
            # If the form hasn't been submitted yet, then do nothing. Do not prompt to enter a name just yet.
        }
    # One of the fields was empty. This finds out which one and creates a message
    # to indicate which it was.
    else
        {
            $msg = "*";
            if(empty($_POST['txtfstname'])) 
            {
                $msg .= "Please enter your first name";
            }
            elseif(empty($_POST['txtlstname']))
            {
                $msg .= "Please enter your last name";
            }
            elseif(empty($_POST['txtemail'])) 
            {
                $msg .= "Please enter your email";
            } 
            else 
            {
                $msg .= "Please enter your employee number";
            }
        }



?>

<form id="contact" class="form" action="" method="post" name="contact"><strong>Employee  Registration</strong>
<em>Please enter the following information and submit. Once the administrator approves your registration, you will receive a confirmation email with login details.</em>
<p><?php echo $msg ?></p>
<table>
<tbody>
<tr>
<td>First Name:</td>
<td><input id="name" class="required" name="txtfstname" type="text" value="<?php echo    $_POST['txtfstname']; ?>" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input id="name" class="required" name="txtlstname" type="text" value="<?php echo  $_POST['txtlstname']; ?>" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input id="name" class="required" name="txtemail" type="text" value="<?php echo $_POST['txtemail']; ?>" /></td>
</tr>
<tr>
<td>Employee No:</td>
<td><input id="name" class="required" name="txtempno" type="text" value="<?php echo    $_POST['txtempno']; ?>" /></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="Send" /></td>
</tr>
</tbody>
</table>
</form>  

这是我的数据库列。

ID, 名字, 姓, 电子邮件, EMP_NO, 密码, 状态, email_code

现在我想要一些继承人,我无法弄明白         1.当管理员点击电子邮件中的激活链接时,如何将状态表格0更新为1         2.如何向员工发送激活他的电子邮件,现在可以在管理员激活员工时登录他的帐户。         3.当员工注册邮件发送给管理员进入SPAM文件夹时。但我希望它在收件箱内。怎么办呢。 任何帮助都可以提前感谢。

2 个答案:

答案 0 :(得分:0)

好的,所以,基本上点击链接,这样重定向:

$user变量必须具有激活其帐户的用户的用户名。

<a href="activate.php?user=<?php echo $user; ?>">Activate</a>

所以,在activate.php中:

<?php
if(isset($_GET['user']) && strlen($_GET['user']) > 0) {
// Update the users activated status from 0 to 1 by running a query.
// Mail to the employee of his account activation
} else {
// No user selected.
}

要解决垃圾邮件文件夹中收到邮件的问题,请将电子邮件保存到您的联系人/取消阻止该电子邮件,并在收件箱中收到。

答案 1 :(得分:-1)

我在PHP方面不是很有经验,但在使用mail()函数之前我已经发过电子邮件。 要了解有关这些的更多信息,请查看:

W3Schools

Php Documentation

我希望这可以帮助你:)