2015-10-14 30 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

让你形成元素的名称和形式的行动匹配的名字和原来的登录表单的行动。

相关问题