会话始终返回null

时间:2016-09-04 22:09:01

标签: php session

我有我的index.php页面,这是用户可以注册和登录的地方。我想在用户登录后显示Welcome Admin。我已经意识到会话为空可能导致$_SESSION变为空?

<?php

session_start();

try {
    $db = new PDO("mysql:host=127.0.0.1; dbname=blog", "root", "");
} catch (PDOException $ex) {
    echo $ex->getMessage();
}

$v = $db->prepare("select * from uyeler where uye_sifre=? and uye_ad=?");
var_dump($v);
if ($_POST) {

    $isim = $_POST["isim"];
    $sifre = $_POST["sifre"];

    $v->execute(array($sifre, $isim));
    $x = $v->fetchAll(PDO::FETCH_ASSOC);
    $d = $v->rowCount();


    if ($d) {
        $_SESSION["ad"] = $x["uye_ad"];
        $_SESSION["sife"] = $x["uye_sifre"];
        $_SESSION["durum"] = $x["uye_durum"];
        echo "Giriş başarılı";
    } else {
        echo "Geçersiz Kullanıcı";
    }

} else {
    if ($_SESSION) {
        var_dump($_SESSION);
        echo "adam üye";
    } else {

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


    <table cellpadding="5" cellspacing="5">
        <tr>
            <td>Uye Ad</td>
            <td><input type="text" name="isim" id=""></td>
        </tr>

        <tr>
            <td>Uye Şifre</td>
            <td><input type="password" name="sifre" id=""></td>
        </tr>
        <tr>

            <td><input type="submit"  id=""></td>
        </tr>

    </table>
</form>';
    }
}
?>

这是var_dump输出

wamp64\www\indeks.php:12:
object(PDOStatement)[2]
  public 'queryString' => string 'select * from uyeler where uye_sifre=? and uye_ad=?' (length=51)
C:\wamp64\www\indeks.php:34:
array (size=3)
  'ad' => null
  'sife' => null
  'durum' => null

2 个答案:

答案 0 :(得分:0)

试试这个:

$v = $db->prepare("select * from uyeler where uye_sifre=:sifre and uye_ad=:isim");
$v->bindParam(':sifre', $sifre, PDO::PARAM_INT);
$v->bindParam(':isim', $isim, PDO::PARAM_INT);

$v->execute();

有关详细信息,请查找PHP Docs

答案 1 :(得分:0)

  1. 手动运行查询以查看查询返回的内容。
  2. fetchAll返回一个多维数组。所以修复这些行

    $_SESSION["ad"] = $x["uye_ad"];
    $_SESSION["sife"] = $x["uye_sifre"];
    $_SESSION["durum"] = $x["uye_durum"];
    

    到此:

    $_SESSION["ad"] = $x[0]["uye_ad"];
    $_SESSION["sife"] = $x[0]["uye_sifre"];
    $_SESSION["durum"] = $x[0]["uye_durum"];
    

    $x是一个多维的,看起来像这样:

    $x = array(
    array('uye_ad'=>'any value', 'uye_sifre'=>'any value2', 'uye_durum'=>'any value3',)
    );