【问题标题】:change the display according to connexion state根据连接状态改变显示
【发布时间】:2014-09-26 06:11:56
【问题描述】:

我正在处理 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>

当我想访问这个页面时,它会显示“未定义的变量:_SESSION”。我该怎么做才能解决这个问题?

【问题讨论】:

  • 您的类将 PDO 包装在一个非常薄、效率低的层中,除了一些更好的变量绑定之外,并没有使它更容易或更简单地使用。你应该从你的很多方法中返回$this——这样你就可以链接调用以获得更简洁、更清晰的代码

标签: php session login


【解决方案1】:

最后我遇到了问题,你的 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>

【讨论】:

    【解决方案2】:

    要获取数据,您需要使用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>
    

    希望对你有帮助。

    谢谢。

    【讨论】:

    • @Bojangles 它也对 OP 没有帮助,因为 query() 不会返回任何可以使用 fetchAll() 的东西。
    • 对不起,我会提高自己。谢谢你的建议。我会编辑我的回答者。谢谢@Bojangles..
    • 不,这仍然行不通。 阅读 OP 对query() 的定义。它不返回任何内容,因此您的代码将失败
    • 它显示另一个错误“致命错误:在第 4 行对 C:\wamp\www\CVtheque\View\annonces.php 中的非对象调用成员函数 fetchAll()”,第四行是查询的行
    • 请检查您的 sql 查询表名 'annonce' 使用以下代码 - $results = $c->query("select poste,client,type_contrat,desc_annonce from 'annonce ' ")->fetchAll();
    猜你喜欢
    • 2021-09-02
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多