2016-05-05 24 views
0

我有一個基本的登錄/註冊表單。當你選擇註冊時,它會轉到我的註冊表單上。我的問題是與登錄。我想檢查我的數據庫中的登錄信息,然後顯示不同的標題部分。我是一名學生,對於如何做到這一點的所有不同信息感到困惑。從JavaScript運行php提交從HTML表格

目前,我在點擊登錄時調用了一個javascript函數,但後來我想調用我的php文件access.php,返回我的html頁面index.php,我想檢查用戶是否登錄,如果是的話顯示logged_in.html.php。

login_register_form.html.php

<div id="login_form"> 
    <form action="access.php" method="POST" name="login_form"> 
    <label id="label_email">Email<span class ="red">*</span></label> 
    <label id="label_password">Password<span class ="red">*</span></label> 
    </br> 
     <input type="text" name="email" id="email"/> 
     <input type="password" name="password" id="password"/> 
    <div id="form_buttons"> 
     <button onclick="loginUser()" Type="button" id="login" name="action" value="login">Login</button>  
     <button onclick="newUser()" Type="button" id="register" name="register">Register</button> 
    </div> 
    </form> 

的javascript

function loginUser() { 
    $.ajax({ 
     url: 'access.php', 
     dataType: 'php' 
    }) 
    window.location.assign("index.php"); 
} 

access.php

function userIsLoggedIn() 
{ 
    if (isset($_POST['action']) and $_POST['action'] == 'login') 
    { 
     if (!isset($_POST['email']) or $_POST['email'] == '' or 
     !isset($_POST['password']) or $_POST['password'] == '') 
     { 
      $GLOBALS['loginError'] = 'Please fill in both fields'; 
      return FALSE; 
     } 
     if (databaseContainsUser($_POST['email'], $password)) 
     { 
      session_start(); 
      $_SESSION['loggedIn'] = TRUE; 
      $_SESSION['email'] = $_POST['email']; 
      $_SESSION['password'] = $password; 
      return TRUE; 
     } 
     else 
     { 
      session_start(); 
      unset($_SESSION['loggedIn']); 
      unset($_SESSION['email']); 
      unset($_SESSION['password']); 
      $GLOBALS['loginError'] = 
      'The specified email address or password was incorrect.'; 
      return FALSE; 
     } 
    } 
} 
function databaseContainsUser($email, $password) 
{ 
    require_once('mysqli_connect.php'); 
    $username = $password = ""; 

    if (isset($_POST["submit"])){ 
     $username = test_input($_POST["username"]); 
     $password = test_input($_POST["password"]); 
    } 

    $query = "SELECT * FROM users 
     WHERE username='".$_POST['username']."' 
     AND password = '".($_POST['password'])."'"; 

    $result = mysqli_query($DBConnect, $query) or die(); 
    if (mysqli_num_rows($result) > 0) { 
     while($row = mysqli_fetch_array($result)) { 
      echo "<img src={$row["avatar"]} alt='avatar image' />"; 
     } 
     return TRUE; 
    } 
    else { 
     echo "Invalid login"; 
     return FALSE; 
    } 
} 

和的index.php

<?php 
session_start(); 

if (!isset($_SESSION['loggedIn'])){ 
    include 'access.php'; 
} 
?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
    <script type ="text/javascript" src="functions.js"></script> 
</head> 

<body> 
    <div id="body"> 
    <div id="container"> 
     <?php if (!isset($_SESSION['loggedIn'])){ 
       include 'login_register_form.html.php'; 
      } 
      else { 
       include 'logged_in.html.php'; 
      } 
     ?> 

任何幫助都將不勝感激

+0

**切勿將明文密碼!**請使用PHP的[內置函數(http://jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼的安全性。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。確保你[不要逃避密碼](http://stackoverflow.com/q/36628418/1011527)或在哈希之前使用其他任何清理機制。這樣做*更改密碼並導致不必要的附加編碼。 –

+0

要通過AJAX獲得回報,您必須在PHP中迴應一些內容。 –

+0

[Little Bobby](http://bobby-tables.com/)說[你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent -sql -injection-in-php)瞭解[MySQLi]的[prepared](http://en.wikipedia.org/wiki/Prepared_statement)語句(http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

回答

0

我可以向你解釋一個簡單的過程,你需要遵循才能讓它工作。

你真的不需要javascript和ajax來讓它工作。除非你不需要在後臺發生的事情,如果一切順利的話,請去儀表板頁面。

您可以在登錄頁面輸入用戶的電子郵件和密碼。在提交表單時,您可以訪問access.php頁面。哪些連接到數據庫並檢查憑證是否正確以及是否存在用戶。如果一切順利。然後簡單地將用戶重定向到帶有會話的儀表盤頁面,如果事情沒有按計劃順利發送,則將用戶發送到主頁並顯示錯誤。

您csn按照這兩個教程中的任何一個來開始。

http://www.codingcage.com/2015/01/user-registration-and-login-script-using-php-mysql.html?m=1。或

https://htmlcssphptutorial.wordpress.com/2015/07/07/simple-user-registration-login-script-in-php-and-mysql/

+0

謝謝Murlidhar。我可以讓它工作,如果我可以發送到新頁面,但我需要它返回到index.php只有一個不同的頭,這是我有問題的地方。我試圖使用ajax的原因是因爲當我做了研究時,其他類似查詢的大部分答案都表示它是需要的。 –