关于SESSION,我的If条件是否正确

时间:2017-04-17 20:19:13

标签: php session-variables static-classes

当我打开WAMP时,会打开index.php文件,该文件会将我重定向到$arr = include 'config.php'; print_r($arr); ,这是第一页。我在其上创建了一个layout.php条件,以检查是否有人登录,以便显示用户的名称而不是连接/铭文文本。除此之外,没有别的变化。

layout.php中

if

的index.php

<div class="header">
 <div class="header-left">
 <?php
  if (!isset($_SESSION['login'])) {?>
  <ul> 
    <li><a href="?controller=user&action=inscription">Inscription</a></li>
    <li class="login">
      <div id="loginContainer"><a id="loginButton"><span>Se Connecter</span></a>
        <div id="loginBox" style="display: none;">                
          <form id="loginForm"  method="POST" action="?controller=user&action=authentification">
            <fieldset id="body">
              <fieldset>
                <label>Login</label>
                <input type="text" name="login" id="email" >
              </fieldset>
              <fieldset>
                <label for="password">Mot de passe</label>
                <input type="password" name="password" id="password">
              </fieldset>
              <input type="submit" id="login" value="Connexion" name="xxl1">
              <label for="checkbox"><input type="checkbox" id="checkbox"> <i>Rester Connecté</i></label>
            </fieldset>
            <span><a href="#">Mot de passe oublié?</a></span>
          </form>
        </div>
      </div>
    </li>
  </ul>
 <?php } else { $name = $_SESSION['login'] ?>
  <h1  style="
  font-size: 19px;
  font-family: inherit;
  margin-right: -11px;
  margin-top: -22px;
  "> Bonjour <?php echo $name?> </h1>
  <a href="?controller=login&action=profil" style="
  font-size: 12px;
  font-family: inherit;
  margin-right: -11px;
  margin-top: 1px;
  "> Editer mon profil </a>
  <?php } ?>
</div>

这是我的带有Connexion功能的模型

Users.php

<?php
require_once('connection.php');

if (isset($_GET['controller']) && isset($_GET['action'])) {
    $controller = $_GET['controller'];
    $action     = $_GET['action'];
} else {
    $controller = 'accueil';
    $action     = 'home';
}

require_once('views/layout.php');
?>

User_controller.php

    public static function Connex()
    {
      if(isset($_POST['xxl1']))
      {
        $db = Db::getInstance();
        $login=$_POST['login'];
        $password=$_POST['password'];
        $requser = $db->prepare("SELECT * FROM users WHERE login = ? AND motdepasse = ?");
        $requser->execute(array($login,$password));
        $userexist = $requser->rowCount();
        if($userexist == 1)
        {
          session_start();
         $userinfo = $requser->fetch();
         $_SESSION['login'] = $userinfo['login'];
         $_SESSION['id_user'] = $userinfo['id_user'];
         $_SESSION['motdepasse'] = $userinfo['motdepasse'];
         if($userinfo['role']=="admin")
         {
           echo "TEST 1 "; 
         }
         else if($userinfo['role']=="client")
         {
        echo "TEST 2"; 
         }

       }
     }
   }

0 个答案:

没有答案