根据连接状态更改显示

时间:2014-07-08 10:03:37

标签: php session login

我正在进行PHP身份验证,我想确保用户是否已连接 如果用户没有连接到登录时出现的连接注册,否则名称将显示在首页几乎退出链接 这是我的代码

    <div id="pl_header" class="container_24 NormalTopPadding">
            <ul class="acc_nav">
                        <div class="jaccess">
                            <?php if($_SESSION['niv'] != 0){?>
                                <li><a href="login.php"><span>Login</span></a></li>                     
                                <li><a href="" data-reveal-id="myModal_197"><span>Sign up</span></a></li>
                            <?php }  else {?>
                                <li><span class="user">Welcome <strong><? echo ($_GET['login']);?></strong></span></li>
                                <li id="un-login"><a href="#"><span>Logout</span></a></li>
                            <?php }?>    
                        </div>
            </ul>
    </div>

当我想访问此pags时,它会显示“Undefined variable:_SESSION”。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:0)

最后我遇到了问题,下面的代码对你的pdo连接没有必要改变默认的pdo类

public function query($query) {
    $this->stmt = $this->dbh->prepare($query);
}

public function bind($param, $value, $type = null) {
    if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue($param, $value, $type);
}

public function execute() {
    return $this->stmt->execute();
}

public function resultset() {
    $this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

public function single() {
    $this->execute();
    return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

public function rowCount() {
    return $this->stmt->rowCount();
}

public function lastInsertId() {
    return $this->dbh->lastInsertId();
}

public function beginTransaction() {
    return $this->dbh->beginTransaction();
}

public function endTransaction() {
    return $this->dbh->commit();
}

public function cancelTransaction() {
    return $this->dbh->rollBack();
}

public function debugDumpParams() {
    return $this->stmt->debugDumpParams();
 }

使用以下代码,它将正常工作..

<?php

class Connexion {

private $host = "localhost";
private $user = "root";
private $pass = "";
private $dbname = "annonce";
public $dbh;
private $stmt;

public function __construct() {
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        // Set options
        $options = array(
            \PDO::ATTR_PERSISTENT => true,
            \PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        // Create a new PDO instanace
        try {
            $this->dbh = new \PDO($dsn, $this->user, $this->pass, $options);

        }catch (\PDOException $e) {
            echo "Connexion Error: " . $e->getMessage();
            exit;
        }
}

public function Connecton(){
            return $this->dbh;  
}

}
?>

<?php

$c = new Connexion();
$c = $c->Connecton();
    $results = $c->query("select poste,client,type_contrat,desc_annonce from annonce ")->fetchAll();
    ?>
     <div class="Joblist">
      <?php foreach($results as $r) { ?>
            <p class="Title">
              <span class="Ref">xxxx</span>
              <a href="details.php"><?php echo($r['poste']); ?></a>
            </p>
            <p class="Date"><span class="Fright"><?php echo($r['type_contrat']); ?></span>
             <span><a href="sector.php" class="Sectorbtn"><?php echo($r['client']); ?></a>  </span></p>
            <p><?php echo($r['desc_annonce']); ?></p>
            <p class="More"><a href="details.php">Voir plus</a></p>
      <?php }?>
     </div>

答案 1 :(得分:-1)

使用fetchAll()向用户query()获取所需数据 我使用以下代码编辑了您的代码。

$c = new Connexion();
    $results = $c->query("select poste,client,type_contrat,desc_annonce from annonce ")->fetchAll();
     <div class="Joblist">
      <?php foreach($results as $r) { ?>
            <p class="Title">
              <span class="Ref">xxxx</span>
              <a href="details.php"><?php echo($r['poste']); ?></a>
            </p>
            <p class="Date"><span class="Fright"><?php echo($r['type_contrat']); ?></span>
             <span><a href="sector.php" class="Sectorbtn"><?php echo($r['client']); ?></a>  </span></p>
            <p><?php echo($r['desc_annonce']); ?></p>
            <p class="More"><a href="details.php">Voir plus</a></p>
      <?php }?>
     </div>

我希望这会对你有所帮助。

感谢。