AJAX - 登录的复杂用户激活

时间:2017-02-25 22:37:31

标签: ajax activation

现在这可能听起来很复杂,但我有一个名为activation.php的页面,在用户使用我的reg.php页面注册后,他们会收到一封带有激活链接的电子邮件 - activation.php然后k的get方法使activation.php?k=activation-code-here

我做了它,所以它返回激活的状态,例如 - 成功的错误。

在我的reg.php页面上,我想让它使用ajax打开一个说'Activation Success'的sweetAlert弹出窗口。它将获取弹出窗口,具体取决于用户'activ_status'在使用正确的激活密钥访问0页面时是否从数据库中的1更改为activation.php

这是我的代码:

Activation.php

    <?php

    include('inc/conf/db_.php');

    $result = '';

    if(empty($_GET['k']) || (!$_GET['k']))
    {
        header('Location: login.php');
        exit();
        $result = 'Hacker Tried Accessing Auth Page';
    }

    $key = mysqli_real_escape_string($con,$_GET["k"]);
?>

<!DOCTYPE html>
<html lang="en">
<head>

<?php

if (!empty($key))
{

    $query  =   "SELECT * FROM users WHERE activ_key = '$key'";
    $result =   mysqli_query($con,$query) or die('error');
         if (mysqli_num_rows($result))
         {
             $row       =   mysqli_fetch_array($result);
             if ($row['activ_status']!='1')
             {
                 $query     =   "update users set activ_status='1' where activ_key='$key'";
                 $result    =   mysqli_query($con,$query) or die('error');
                 $result    = 'Successfuly Activated, Return To Your Other Page.';
             }
             else
             {
                 $result = "Account Already Activated";
             }

         }
         else
         {
             $result = 'Invalid Access Token';
         }
}
else
{
    $result = 'Error';
}

?>

  <?php include('inc/login_header.php'); ?>

      <div class="accountbg"></div>
      <div class="wrapper-page">
         <div class="panel panel-color panel-primary panel-pages">
            <div class="panel-body">

            <br>
            <center><div id="result"></center>
            <br>

               <h3 class="text-center m-t-0 m-b-30"> <span class=""><?php echo site_settings('site_name'); ?></h3>
               <h4 class="text-muted text-center m-t-0"><b>Activation Status</b></h4>

               <br>

               <center><img src="assets/images/loading.gif"></center>

               <br>
               <center><div class="alert alert-info"><button type="button" class="close"></button><?php echo $result; ?></div></center>
               <?php return $result; ?>

</body>
</html>

我的reg.php页面

    <?php

    include('inc/conf/db_.php');

    if (session_status() == PHP_SESSION_ACTIVE) {
        session_start();
    }

    if(isset($_SESSION['user_i']) || isset($_SESSION['login'])) {
        header('Location: index.php');
        exit();
    }

    else if (session_status() == PHP_SESSION_NONE) {

?>

<!DOCTYPE html>
<html lang="en">
<head>

    <link href="assets/plugins/bootstrap-sweetalert/sweet-alert.css" rel="stylesheet" type="text/css">


    <?php include('inc/login_header.php'); ?>


      <div class="accountbg"></div>
      <div class="wrapper-page">
         <div class="panel panel-color panel-primary panel-pages">
            <div class="panel-body">

                <center><img src="assets/images/loading.gif" id="loading-image" style="display:none"/></center>

               <h3 class="text-center m-t-0 m-b-30"> <span class=""><?php echo site_settings('site_name'); ?></h3>
               <h4 class="text-muted text-center m-t-0"><b>Sign Up</b></h4>


               <form id="registered_form" class="form-horizontal m-t-20">


                  <div class="form-group has-feedback">
                     <div class="col-xs-12"> <input class="form-control" name="email" type="email" required="" placeholder="Email"></div>
                     <span class="fa fa-envelope form-control-feedback text-muted" style="margin-top: 3%;"></span>
                  </div>

                  <div class="form-group has-feedback">
                     <div class="col-xs-12"> <input class="form-control" name="username" type="text" required="" placeholder="Username"></div>
                     <span class="fa fa-user form-control-feedback text-muted" style="margin-top: 3%;"></span>
                  </div>

                  <div class="form-group has-feedback">
                     <div class="col-xs-12"> <input class="form-control" name="password" type="password" required="" placeholder="Password"></div>
                     <span class="fa fa-lock form-control-feedback text-muted" style="margin-top: 3%;"></span>
                  </div>

                          <div class="form-group">
                             <div class="g-recaptcha" data-sitekey="6LeGFxYUAAAAAOqjyovTS3H0D1HS4IBgoHMvG4y_"></div>
                          </div>

                  <div class="form-group">
                     <div class="col-xs-12">
                        <div class="checkbox checkbox-primary" style="text-align: center;"> <input type="checkbox" name="checkbox" value="agree"> <label for="checkbox-signup"> I accept <a href="#">Terms and Conditions</a> </label></div>
                     </div>
                  </div>

                  <div class="form-group text-center m-t-20">
                     <div class="col-xs-12"> <button class="btn btn-primary w-md waves-effect waves-light" type="submit" id="reg_button" style="width: 100%;" >Register</button></div>
                  </div>

                  <div class="form-group m-t-30 m-b-0">
                     <div class="col-sm-12 text-center"> <a href="login.php" class="text-muted">Already have an account?</a></div>
                  </div>


               </form>

            </div>
         </div>
      </div>

<script src="assets/js/jquery.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.validate.js"></script>
<script src="assets/plugins/bootstrap-sweetalert/sweet-alert.min.js"></script>

<script>

    $(document).ready(function()
    {

    jQuery.validator.addMethod("noSpace", function(value, element)
    { 
      return value.indexOf(" ") < 0 && value != ""; 
    }, "Spaces are not allowed");

            $("#registered_form").submit(function()
            {
                if ($("#registered_form").valid())
                {

                  $('#loading-image').show();

                    var data1 = $('#registered_form').serialize();

                    $.ajax({
                        type: "POST",
                        url: "inc/pgs/register.php",
                        data: data1,
                        success: function(msg)
                        {
                            $('#loading-image').hide();

                            console.log(msg);
                            if(msg == '')
                            {
                              swal({
                                title: "Registration Success!",
                                text: "Your registration was successful! Check your email and click on your activation link to be able to access your account. NOTE: Keep this page open",
                                type: "success",
                                timer: 15000,
                                showConfirmButton: false
                              });
                            }
                            else
                            {
                              swal("Registration Error!", msg, "error");
                            }
                        }
                    });

                    $.ajax({
                        type: "GET",
                        url: "activation.php",
                        data: data1,
                        success: function(msg)
                        {
                            if(msg == 'Successfuly Activated, Return To Your Other Page.')
                            {
                              swal({
                                title: "Activation Success",
                                text: "You can now login!",
                                type: "success",
                                timer: 15000,
                                showConfirmButton: false
                              });
                            }
                        }
                    });


                }
                return false;
            });
        });
    </script>


   </body>
</html>

<?php } ?>

我发现了一段可能对我有帮助的代码。它似乎没有加载模式:

                        $.ajax({
                              url: 'activation.php',
                              type: 'GET',
                              dataType: 'jsonp',
                              cache: 'false',
                              timeout: 32000,
                              success: function(data) {
                                if(data == 'Successfuly Activated, Return To Your Other Page.')
                                {
                                   swal("Activation Completed!", "You can now login using the link at the bottom of the page - Already have an account?", "success")
                                }
                              },
                              error: function(jqXHR, textStatus, errorThrown) {
                                  console.log("Error[refresh]: " + textStatus);
                                  console.log(jqXHR);
                                  ajaxCall();
                              },
                          });

1 个答案:

答案 0 :(得分:0)

请清楚地解释您所遇到的阻碍代码按预期运行的问题。

如果我的解释是正确的,您希望更新用户在另一个窗口中激活时保持打开的注册页面,然后使用AJAX在注册页面上反映更改。实际上,在这种情况下,您希望将数据从服务器发送到客户端(当数据库中的服务器发生更改时,您希望更改某些内容)。在这种情况下,一个简单的解决方案是使用setTimeout重复请求状态(例如每2秒)。存在其他解决方案但可能过度,例如长轮询或网络套接字。