为真实用户构建注册,登录和密码的最快方法是什么?

时间:2015-06-24 21:17:26

标签: javascript php mysql passwords

我想了解如何使用我选择的自定义登录凭据来密码保护我的网站。 使用自定义的html,css和javascript来创建界面让我得到这样的一点 - > http://codepen.io/lexeckhart/pen/RPLPwX

但是每个人都可以访问该页面上的所有内容。我冒着成为白痴的风险说我记得用php和mysql或sql来做下一部分。也许

要添加标题问题,我想知道我开始创建此数据库的位置?我可以用ftp做吗?

HTML

 <div id="successful_login" class="fix-middle">
  <div class="container text-center">
    <h1>Welcome back to the internet!</h1>
    <p>You've successfully managed to log into a nonexistant account in order to test a login dialog box.<br> If you like it, you are welcomed to use it wherever you want, no strings attached.<br><br><a href="#" class="link dialog-reset">Rerun the whole thing.</a></p>
  </div>
</div>
<div id="successful_registration" class="fix-middle">
  <div class="container text-center">
    <h1>Welcome to the internet!</h1>
    <p>You've successfully managed to register for a nonexistant account in order to test a registration dialog box.<br> If you like it, you are welcomed to use it wherever you want, no strings attached.<br><br><a href="#" class="link dialog-reset">Rerun the whole thing.</a></p>
  </div>
</div>

<div id="dialog" class="dialog dialog-effect-in">
  <div class="dialog-front">
    <div class="dialog-content">
      <form id="login_form" class="dialog-form" action="" method="POST">
        <fieldset>
          <legend>Log in</legend>
          <div class="form-group">
            <label for="user_username" class="control-label">Username:</label>
            <input type="text" id="user_username" class="form-control" name="user_username" autofocus/>
          </div>
          <div class="form-group">
            <label for="user_password" class="control-label">Password:</label>
            <input type="password" id="user_password" class="form-control" name="user_password"/>
          </div>
          <div class="text-center pad-top-20">
            <p>Have you forgotten your<br><a href="#" class="link"><strong>username</strong></a> or <a href="#" class="link"><strong>password</strong></a>?</p>
          </div>
          <div class="pad-top-20 pad-btm-20">
            <input type="submit" class="btn btn-default btn-block btn-lg" value="Continue">
          </div>
          <div class="text-center">
            <p>Do you wish to register<br> for <a href="#" class="link user-actions"><strong>a new account</strong></a>?</p>
          </div>
        </fieldset>
      </form>
    </div>
  </div>
  <div class="dialog-back">
    <div class="dialog-content">
      <form id="register_form" class="dialog-form" action="" method="POST">
        <fieldset>
          <legend>Register</legend>
          <div class="form-group">
            <label for="user_username" class="control-label">Username:</label>
            <input type="text" id="user_username" class="form-control" name="user_username"/> 


          </div>
              <div class="form-group">
                <label for="user_password" class="control-label">Password:</label>
                <input type="password" id="user_password" class="form-control" name="user_password"/>
              </div>
              <div class="form-group">
                <label for="user_cnf_password" class="control-label">Confirm password:</label>
                <input type="password" id="user_cnf_password" class="form-control" name="user_cnf_password"/>
              </div>
              <div class="form-group pad-top-20 form-group-checkbox">
                <div class="checkbox">
                  <label>
                    <input type="checkbox" id="user_terms" name="user_terms">
                    I have read and I agree with the Terms and Conditions
                  </label>
                </div>
              </div>
              <div class="pad-btm-20">
                <input type="submit" class="btn btn-default btn-block btn-lg" value="Continue"/>
              </div>
              <div class="text-center">
                <p>Return to <a href="#" class="link user-actions"><strong>log in page</strong></a>?</p>
              </div>
            </fieldset>
          </form>
        </div>
      </div>
    </div>

JAVASCRIPT     //&#34; getFormData()&#34;函数检索表单中每个输入字段的名称和值;

function getFormData(form) {
  var data = {};
  $(form).find('input, select').each(function() {
    if (this.tagName.toLowerCase() == 'input') {
      if (this.type.toLowerCase() == 'checkbox') {
        data[this.name] = this.checked;
      } else if (this.type.toLowerCase() != 'submit') {
        data[this.name] = this.value;
      }
    } else {
      data[this.name] = this.value;
    }
  });
  return data;
}

// The "addFormError()" function, when called, adds the "error" class to the form-group that wraps around the "formRow" attribute;

function addFormError(formRow, errorMsg) {
  var errorMSG = '<span class="error-msg">' + errorMsg + '</span>';
  $(formRow).parents('.form-group').addClass('has-error');
  $(formRow).parents('.form-group').append(errorMSG);
  $('#dialog').removeClass('dialog-effect-in');
  $('#dialog').addClass('shakeit');
  setTimeout(function() {
    $('#dialog').removeClass('shakeit');
  }, 300);
}

// FORM HANDLER:

// form_name - This attribute ties the form-handler function to the form you want to submit through ajax. Requires an ID (ex: #myfamousid)
// custom_validation - 

function form_handler(form_name, custom_validation, success_message, error_message, success_function, error_function) {
  $(form_name).find('input[type="submit"]').on('click', function(e) { // if submit button is clicked

    window.onbeforeunload = null; // cancels the alert message for unsaved changes (if such function exists)

    $(form_name).find('.form-group .error-msg').remove();
    var submitButton = this;
    submitButton.disabled = true; // Disables the submit buttton until the rows pass validation or we get a response from the server.

    var form = $(form_name)[0];
    // The custom validation function must return true or false.
    if (custom_validation != null) {
      if (!custom_validation(form, getFormData(form))) {
        submitButton.disabled = false;
        return false;
      }
    }
    e.preventDefault(); //STOP default action
  });
  $(document).click(function(e) { // Whenever the user clicks inside the form, the error messages will be removed.
    if ($(e.target).closest(form_name).length) {
      $(form_name).find('.form-group').removeClass('has-error');
      setTimeout(function() {
        $(form_name).find('.form-group .error-msg').remove();
      }, 300);
    } else {
      return
    }
  });
}

// LOGIN FORM: Validation function
function validate_login_form(form, data) {
  if (data.user_username == "") {
    // if username variable is empty
    addFormError(form["user_username"], 'The username is invalid');
    return false; // stop the script if validation is triggered
  }

  if (data.user_password == "") {
    // if password variable is empty
    addFormError(form["user_password"], 'The password is invalid');
    return false; // stop the script if validation is triggered
  }

  $('#dialog').removeClass('dialog-effect-in').removeClass('shakeit');
  $('#dialog').addClass('dialog-effect-out');

  $('#successful_login').addClass('active');
  //return true;
}

// REGISTRATION FORM: Validation function
function validate_registration_form(form, data) {
  if (data.user_username == "") {
    // if username variable is empty
    addFormError(form["user_username"], 'The username is invalid');
    return false; // stop the script if validation is triggered
  }

  if (data.user_password == "") {
    // if password variable is empty
    addFormError(form["user_password"], 'The password is invalid');
    return false; // stop the script if validation is triggered
  }

  if (data.user_cnf_password == "" || data.user_password != data.user_cnf_password) {
    // if password variable is empty
    addFormError(form["user_cnf_password"], "The passwords don't match");
    return false; // stop the script if validation is triggered
  }

  if (!data.user_terms) {
    // if password variable is empty
    addFormError(form["user_terms"], "You need to read and accept the Terms and Conditions before proceeding");
    return false; // stop the script if validation is triggered
  }

  $('#dialog').removeClass('dialog-effect-in').removeClass('shakeit');
  $('#dialog').addClass('dialog-effect-out');

  $('#successful_registration').addClass('active');
  //return true;
}

form_handler("#login_form", validate_login_form, null, null, null, null, null, null);
form_handler("#register_form", validate_registration_form, null, null, null, null, null, null);

var dialogBox = $('#dialog');

dialogBox.on('click', 'a.user-actions', function() {
  dialogBox.toggleClass('flip');
});

$('#successful_login,#successful_registration').on('click', 'a.dialog-reset', function() {
  $('#successful_login,#successful_registration').removeClass('active');
  dialogBox.removeClass('dialog-effect-out').addClass('dialog-effect-in');
  document.getElementById('login_form').reset();
  document.getElementById('register_form').reset();
});

1 个答案:

答案 0 :(得分:-1)

将MySQL中的凭据存储在数据库表中(散列),通过php访问数据库,使用php或javascript重定向用户。