如何存储会话数据

时间:2011-08-05 14:39:01

标签: php session

我有这个代码,它成功存储'author_id',但我希望它也存储'author_is_admin'字段。我究竟做错了什么。我是PHP的新手,所以解释你编写的代码所做的可能会有所帮助。

<?php
session_start();
require_once('inc/config.php');

$_SESSION['author_is_admin'];

    if (isset($_SESSION['author_id'])) {    // Logged in.
/* Build redirect URL */
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if (!(substr($url, -1) == '/')) {
    $url .= '/';
}

/* Redirect to index. */
header("Location: $url");
exit();
}

$error = false;

if (isset($_POST['submitted'])) {   // Form submitted, attempt login.
if (!empty($_POST['email']) && !empty($_POST['password'])) {    // Email & password      entered.
    require_once('inc/db.php');
    $email = escape_data($_POST['email']);
    $password = escape_data($_POST['password']);
} else {    // Email or password empty.
    $email = $password = false;
    $error = true;
}
if ($email && $password) {  // Email and password entered.
    $query = "SELECT author_id FROM authors WHERE (author_email ='$email' AND author_password = MD5('$password'))";
    $result = mysql_query($query) or trigger_error('Error: ' . mysql_error());

    if (@mysql_num_rows($result) == 1) {    // Email and password correct.
        $row = mysql_fetch_array($result);
        mysql_free_result($result);
        mysql_close();

        /* Store session data */
        $_SESSION['author_id'] = $row['author_id'];
        $_SESSION['author_is_admin'] = $row['author_is_admin'];

        /* Build redirect URL */
        $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
        if (!(substr($url, -1) == '/')) {
            $url .= '/';
        }

        /* Redirect to index */
        header("Location: $url");
        exit();
    } else {    // User not authenticated.
        $error = true;
    }
} else {    // Email and/or password not entered.
    $error = true;
}
  }

  ?>

2 个答案:

答案 0 :(得分:5)

您没有在查询中检索该列:

$query = "SELECT author_id FROM authors WHERE (author_email ='$email' AND author_password....."

//Should be 
$query = "SELECT author_id, author_is_admin FROM authors WHERE (author_email ='$email' AND author_password"

答案 1 :(得分:0)

一切都好。如果您尝试检查$_SESSION['author_is_admin'];您需要什么

print $_SESSION['author_is_admin'];

var_dump($_SESSION['author_is_admin']);