Как хранить данные сеанса - PullRequest
0 голосов
/ 05 августа 2011

У меня есть этот код, он успешно сохраняет '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 ]

5 голосов
/ 05 августа 2011

Вы не получаете этот столбец в своем запросе:

$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"
0 голосов
/ 05 августа 2011

Все в порядке. Если вы попытаетесь проверить, что является $_SESSION['author_is_admin']; Вам нужно

print $_SESSION['author_is_admin'];

или

var_dump($_SESSION['author_is_admin']);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...