2014-02-25 35 views
0

我試圖做一個登錄系統使用Jquery來檢查點擊,然後發送到PHP文件。這個文件取得結果,檢查mysql數據庫。如果用戶名和密碼不正確,它會回顯。如果登錄是正確的,那麼它會在會話中放入用戶標識並嘗試調用標頭來更新開始文件上的會話,但這不起作用。Jquery登錄頭更新會話不工作

我必須手動爲要更新的會話重新加載頁面。這有可能改變嗎?

這是在index.php

<?php 
session_start(); 

print_r($_SESSION); 
?> 
<html> 
<head> 
    <title>Site</title> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > 
    <script type="text/javascript" src="jquery.min.js"></script> 
    <script type="text/javascript" src="script.js?v=1"></script> 
</head> 
<body> 
    <div id='user'> 
    <?php 
     if (isset($_SESSION['user_id'])) echo "Logged in"; else echo "Not logged in"; 
    ?> 
    </div> 
    <center> 
    <div id='login'> 
     <div id='login-warning'></div> 
     <input type='text' id='username' placeholder='Your name'> 
     <input type='text' id='password' placeholder='Your password'> 
     <input type='button' id='login-submit' value= 'Send'> 
    </div> 
    </center> 
</body> 
</html> 

這個JavaScript文件:

$(document).ready(function() { 

    $('input#login-submit').click(function() { 
     var username = $('input#username').val(); 
     var password = $('input#password').val(); 
     $.post('login.php', {u: username, p: password}, function(data) { 
       $('#login-warning').text(data); 
     }); 
    }); 
}); 

這是login.php中的文件:

<?php 

if (empty($_POST) === false) { 

    $U = ereg_replace("[^A-Za-z0-9._]", "", $_POST['u']); 
    $P = ereg_replace("[^A-Za-z0-9._]", "", $_POST['p']); 

    if(empty($U) === true AND empty($P) === true) { 
     $error = " You need to enter a username and password"; 
    } else if (empty($U) === false AND empty($P) === true) { 
     $error = " You need to enter a password"; 
    } else if (empty($U) === true AND empty($P) === false) { 
     $error = " You need to enter a username"; 
    } else if (empty($U) === false AND empty($P) === false) { 


     require_once 'db_connect.php'; 
     $query = "SELECT * FROM `users` WHERE user='$U'" or die("Error.." . mysqli_error($db)); 
     $result = mysqli_query($db, $query); 

     while($row = mysqli_fetch_array($result)) { 
      $id = $row['id']; 
      $user = $row['user']; 
      $pass = $row['password']; 
          $active = $row['active']; 
     } 

     if (!$id) { 
      $error = "User not found"; 
     } else { 
      if ($P == $pass AND $P != "") { 
       if ($active == 1) { 
        session_start(); 
        $_SESSION['user_name'] = $user; 
        $_SESSION['user_id'] = $id; 
        session_write_close(); 
        // 
        header("Location: index.php"); 
        // 
        ob_end_flush(); 
        exit; 
       } else { 
        $error = "You have not activated your account"; 
       } 
      } else { 
       $error = "Incorrect password"; 
      } 
     } 
    } 

} 

echo $error; 

?> 

回答

2

index.php不會更新與新的註冊會話,你不能在ajax調用中使用header("location:index.php"),你必須通過重新加載頁面JavaScript的。

的login.php:

if($active == 1) { 
    session_start(); 
    $_SESSION['user_name'] = $user; 
    $_SESSION['user_id'] = $id; 
    $error = false; 
} 

的index.php:

$.post('login.php', 
     {u: username, p: password}, 
     function(data) { 
      if(!data) { 
      location.reload(false) 
      } else { 
      $('#login-warning').text(data); 
      } 
     } 
)