2014-02-25 195 views
0

我最近開始處理表單驗證和登錄/註冊。但是,我遇到了一個問題,當我單擊具有現有用戶名和密碼的登錄按鈕時,我想要轉到同一頁面(登錄頁面)或起始頁面,並顯示消息:「您已成功登錄」。我不知道該怎麼做。我有除其他外,兩個PHP文件,的login.php登錄表單和註冊PHP

if (loggedin()) { 
     $firstname = getuserfield('firstname'); 
     $surname = getuserfield('surname'); 
     echo 'You\'re logged in, '.$firstname.' '.$surname.'. <a href="logout.php">Log  out </a><br>'; 
    } else { 
    include 'loginform.inc.php'; 

    } 

loginform.inc.php

if (isset($_POST['username'])&&isset($_POST['password'])) { 
    $username = $_POST['username']; 
    $password = $_POST['password']; 

    $password_hash = md5($password); 
    if (!empty($username)&&!empty($password)) { 
     $query = "SELECT `id` FROM `users` WHERE `username`='".mysql_real_escape_string($username)."' AND `password`='".mysql_real_escape_string($password_hash)."'"; 
     if ($query_run = mysql_query($query)) { 
      $query_num_rows = mysql_num_rows($query_run); 
      if ($query_num_rows==0) { 
      ?> <div id = "a5"> <?php echo 'No such combination of username and password' ?> </div>; <?php 
      } else if ($query_num_rows==1){ 
      $user_id = mysql_result($query_run, 0, 'id'); 
      $_SESSION['user_id']=$user_id; 

      header('Location: login.php'); 
      } 
      } 
    } else { 
?> <div id="a"> <?php echo 'Please enter username and password!' ?> </div> 
+0

爲什麼在驗證用戶憑據後,您不重定向到成功頁面?並在成功頁面上,只需檢查會話是否設置! – NoobEditor

回答

-1

方法:1

可以包括loginform.inc.php成功登錄後login.php這樣

if (loggedin()) { 
     $firstname = getuserfield('firstname'); 
     $surname = getuserfield('surname'); 
     echo 'You\'re logged in, '.$firstname.' '.$surname.'. <a href="logout.php">Log  out </a><br>'; 
     include 'loginform.inc.php'; // include here after successfully login 
    } else { 
    include 'loginform.inc.php'; 

    } 

方法:2

你可以在會話變量和轉讓頭存儲您的成功消息的loginform.inc.php。然後您可以在loginform.inc.php頁面上打印您的會話。

的login.php:

session_start(); 
if (loggedin()) { 
      $firstname = getuserfield('firstname'); 
      $surname = getuserfield('surname'); 
      $_SESSION['msg_text'] = 'You\'re logged in, '.$firstname.' '.$surname.'. <a href="logout.php">Log  out </a><br>'; // set a session variable 

       header("location:loginform.inc.php"); // transfer header on the loginform.inc.php page 
       exit; 

     } else { 
     include 'loginform.inc.php'; 

     } 
+0

試過了,沒有工作。 – user2517370

0

我會用一些更高級的登錄。我使用的登錄表單完美,易於理解和使用。訪問這個login form我使用的登錄名。如果您願意,也可以輕鬆轉換爲面向對象。