繁体   English   中英

如何存储会话数据

[英]How to Store Session Data

我有这段代码,它可以成功存储'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;
}
  }

  ?>

您不会在查询中检索该列:

$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"

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

print $_SESSION['author_is_admin'];

要么

var_dump($_SESSION['author_is_admin']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM