获取用户信息表(电子邮件user_id)

时间:2014-02-15 11:14:40

标签: php mysql session username

我正在尝试在安全页面上收到用户电子邮件。

<?php echo htmlentities($_SESSION['username']); ?></br>
<?php echo htmlentities($_SESSION['email']); ?>

但我得到Undefined index: email error

我也试图让user_id做这样的事情

<a href="/<?php echo htmlentities($_SESSION['user_id']);?>/wordpress/"></a>

http://www.01/wordpress/index.php

<a href="/<?php echo htmlentities($_SESSION['username']);?>/wordpress/"></a>

工作正常只是看不出为什么它不会发布id和邮件?

帮助任何人?

<?php
//restrictedpage.php :Init session
session_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
    header('Location:../logout/');
}

?>

<h1>
    <p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p></br>
    <h5>
        <p><?php echo htmlentities($_SESSION['email']); ?></p>
    </h5></br>
    <h5>
        <p><?php echo htmlentities($_SESSION['id_user']); ?></p>
    </h5>

我在最后一个代码上发布了loggin handler sry

2 个答案:

答案 0 :(得分:0)

收到你必须开始会话的价值后:

<?php
    session_start();
    echo htmlentities($_SESSION['username']);
    echo htmlentities($_SESSION['email']);
?>

答案 1 :(得分:0)

根据您所拥有的内容,您只需在会话中设置用户名,如果您希望id从$ login数组中提取并在会话中设置

$_SESSION['user_id'] = $login['user_id]';

尝试

<?php

session_start();
include('config.inc');

$login = mysql_query("
    SELECT *
    FROM users
    WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and
          (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')
");
if (mysql_num_rows($login) == 1) {
    $_SESSION['username'] = $_POST['username'];
    // get associative array
    $row = mysql_fetch_assoc($login);
    $_SESSION['user_id'] = $row['user_id'];
    header('Location: secure.php');
}
?>

或者使用PDO

$DB = new PDO( "connection string goes here" );
$query_user = $DB->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$query_user>bindValue(':username',mysql_real_escape_string($_POST['username'],PDO::PARAM_STR);
$query_user>bindValue(':password',mysql_real_escape_string($_POST['password'],PDO::PARAM_STR); 
$query_user->execute();
// get result row (as an object)
$result_row = $query_user->fetchObject();
$_SESSION['user_id'] = $result_row->user_id;