2015-10-14 31 views
1

幾個星期前,我將一個開源文件admnistrator集成到我的網站,其中包括一個登錄/成員系統。 (文件admnistrator可以找到here)。如何將現有的登錄腳本集成到每個頁面上的新登錄表單中?

由於我正在學習網絡開發,我認爲這將是一個好主意,將此登錄系統集成到我的整個網站。 因此,我添加了必要的PHP代碼以保持連接在我的標題(包含在每個頁面中),並且註銷腳本也不是問題。

但我的問題是與登錄表單。這裏是一個我做(這是在我的header.php文件):

<form method="post" action=""> 

     <div id="username"> 
      <input type="text" name="txt_username" id="txt_username" placeholder="username" required="" value="" /> 
     <span class="username_icon"><i class="fa fa-user"></i></span> 
     </div> 
     <div id="password"> 
     <input type="password" name="txt_password" id="txt_password" placeholder="password" required="" /> 
     <span class="password_icon"><i class="fa fa-lock"></i></span> 
     </div> 
     <div id="stay_connected"> 
     <input type="checkbox" name="chk_connected" id="chk_connected"> 
     <label for="chk_connected"><span>Stay Connected</span></label> 
     </div> 
     <div id="submit_button"> 
     <button type="submit" name="sub_login" id="sub_login"><i id="submit"class="fa fa-long-arrow-right"></i></button> 
     </div> 
     <div class="feedback">login successful <br />redirecting...</div> 

     </form> 

,這裏是該文件admnistrator使用登錄用戶他(我打算踢了一次,原來全的login.php我的是準備好):

<?php 

require_once('config/config.inc.php'); 
require_once('config/settings.inc.php'); 
require_once('classes/User.php'); 
require_once('lib/functions.php'); 

session_start(); 

/***********************************************************************************************************/ 
/********************************* INITIALISATION OF VARIABLES ********************************************/ 
/***********************************************************************************************************/ 
$error = ""; 

/***********************************************************************************************************/ 
/********************************* DATA TREATMENT **************************************************/ 
/***********************************************************************************************************/ 
if(isset($_COOKIE['identifiant']) && !empty($_COOKIE['identifiant']) && isset($_COOKIE['mdp']) && !empty($_COOKIE['mdp'])) { 


    $monUtilisateur = User::check($_COOKIE['identifiant'], $_COOKIE['mdp']); 

    if($monUtilisateur !== false) { 
     $_SESSION['auth'] = $monUtilisateur; 
     redirect('index.php'); 
    } 
    else { 
     // on supprime les cookies 
     setcookie('identifiant'); 
     setcookie('mdp'); 

     $error = "The username or password is incorrect."; 
    } 

} 
else { 

    if(isset($_POST['txt_identifiant']) || isset($_POST['txt_mdp'])) { 

     /////////// PRELIMINARY CONTROLS //////////////////////////////////////////////////////////////////////// 
     $nbErreurs = isset($GLOBALS['login_nbErreurs']) ? (int)$GLOBALS['login_nbErreurs'] : 0; 
     $nbChampsrestantsaremplir = isset($GLOBALS['login_nbChampsrestantsaremplir']) ? (int)$GLOBALS['login_nbChampsrestantsaremplir'] : 0; 
     $t_erreurs = array(); 

     /***********************************************************************************************************/ 
     /********** USERNAME ************************************************************************************/ 
     /***********************************************************************************************************/ 
     if (isset($_POST['txt_identifiant'])) { 
      $_POST['txt_identifiant'] = trim($_POST['txt_identifiant']); 
      if ($_POST['txt_identifiant'] == '') { 
       $nbChampsrestantsaremplir++; 
       $t_erreurs['txt_identifiant'] = 'Veuillez saisir votre identifiant'; 
      } 
     } 

     /***********************************************************************************************************/ 
     /*********** PASSWORD *******************************************************************************************/ 
     /***********************************************************************************************************/ 
     if (isset($_POST['txt_mdp'])) { 
      $_POST['txt_mdp'] = trim($_POST['txt_mdp']); 
      if ($_POST['txt_mdp'] == '') { 
       $nbChampsrestantsaremplir++; 
       $t_erreurs['txt_mdp'] = 'Please enter your password'; 
      } 
     } 

     ////////// END OF PRELIMINARY CONTROLS ////////////////////////////////////////////////////////////////////// 
     $GLOBALS['login_nbErreurs'] = $nbErreurs; 
     $GLOBALS['login_nbChampsrestantsaremplir'] = $nbChampsrestantsaremplir ; 


     if ($nbErreurs == 0 && $nbChampsrestantsaremplir == 0) { 

      $monUtilisateur = User::check($_POST['txt_identifiant'], $_POST['txt_mdp']); 

      if($monUtilisateur !== false) { 
       // si l'authentification est bonne et que la case est cochee, on cree le cookie 
       if (isset($_POST['chk_cookie']) && $_POST['chk_cookie']=="oui") {   
        // on cree les cookies valide 1 JOUR    
        setcookie('identifiant', $_POST['txt_identifiant'], time() + 1*24*3600, null, null, false, true); 
        setcookie('mdp', $_POST['txt_mdp'], time() + 1*24*3600, null, null, false, true); 
       } 
       // sinon on supprime 
       else { 
        // on supprime les cookies 
        setcookie('identifiant'); 
        setcookie('mdp'); 
       } 
       //je cree une session 
       $_SESSION['auth'] = $monUtilisateur; 

       //je redirige 
       redirect('index.php'); 
      } 
      else 
       $error = "The username or password is incorrect."; 

     } 

    } 

} 
?> 
<!doctype html> 
<html lang="fr"> 
<head> 
    <meta charset="utf-8"> 
    <title>Authentification</title> 
    <meta name="robots" content="noindex,nofollow" /> 

    <!-- CSS --> 
    <link rel="stylesheet" href="themes/original/css/login.css" /> 

    <!-- JQUERY --> 
    <script src="js/jquery-1.11.0.min.js"></script> 

    <!-- SCRIPTS DIVERS --> 
    <script src="js/jquery.placeholder.min.js"></script> 
    <script> 
    $(document).ready(function(){ 
     $('input[placeholder]').placeholder(); 
    }); 
    </script> 

    <script> 
    $(document).ready(function(){ 
     $('#txt_identifiant').focus();         
    }); 
    </script> 
</head> 

<body> 

    <div id="content"> 

     <div id="logo"> 
      <img alt="logo" src="themes/original/images/logo.png" /> 
     </div> 

     <? if(isset($error)) { ?><p class="error"><?php echo $error ?></p><? } ?> 

     <form method="post" action="login.php"> 

      <div id="identifiant"> 
       <input type="text" name="txt_identifiant" id="txt_identifiant" placeholder="Identifiant" required="" value="" /> 
      </div> 
      <div id="mdp"> 
       <input type="password" name="txt_mdp" id="txt_mdp" placeholder="Mot de passe" required="" /> 
      </div> 
      <div id="maintenir"> 
       <input type="checkbox" name="chk_maintenir" id="chk_maintenir"> 
       <label for="chk_maintenir"><span>Stay connected</span></label> 
      </div> 
      <div id="soumettre"> 
       <input type="submit" name="sub_login" id="sub_login" value="Connexion" style="width:100px;" /> 
      </div> 

     </form> 

</div><!-- fin de content --> 

</body> 
</html> 

我天真地試圖執行存在於原來的login.php的PHP代碼(同時改變路徑),但當然它沒有這樣做的伎倆。即使是這樣,我不確定這是否是最好的主意。 我覺得所有在我的頭文件中的登錄php代碼會有點「沉重」,或者可能是糟糕的結構化。

例如,註銷系統非常簡單,一個重定向按鈕導致logout.php頁面,然後立即銷燬會話並將用戶重定向到我想要的地方。 這或多或少是我嘗試使用登錄系統的原因:提交表單 - >簡單地調用login.php文件來檢查一切是否正常並開始會話 - >立即重定向到頁面。

但到目前爲止,使用文件admnistrator的登錄頁面的原始代碼和我可憐的知識,我一直無法做到這一點。

希望我很清楚,我很新,但我願意按照每一條指示,盡我所能嘗試你的建議!

非常感謝您的幫助,

-Apatik

解決由於Julios,檢查的意見。謝謝 !

+0

只需更改您的表單字段的名稱以匹配您同事表單上的字段。例如改變:'to''。改變所有的輸入元素,並使其工作。 –

+0

雖然你可能想要谷歌** xss攻擊**之前拿在手中的責任提交表單與用戶數據到您的服務器... –

+0

感謝您的回答,是的,我完全忘記了這一點,我會改變他的部分,而不是我的,所以它是在「英語」和功能被稱爲的地方。但我怎麼能讓我的提交按鈕重定向到這個login.php文件,然後「檢查」字段?我必須刪除他的login.php的登錄表單,因爲我已經在我的標題上找到了一個。謝謝您的幫助 ! – apatik

回答

0

讓你形成元素的名稱和形式的行動匹配的名字和原來的登錄表單的行動。

相關問題