从$ _SESSION获取用户名 [英] Get username from $_SESSION

查看:72
本文介绍了从$ _SESSION获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取用户的用户名以回显,但只能看到用户ID,或者如果我尝试回显用户名,它会给我字符串长度而不是实际名称.

I am trying to get the username of my users to echo out but only seeing the userID or if I try to echo the username it gives me the string length rather than the actual name.

我正在将会话脚本调用到我的标头中,该标头包含在所有页面中:

I am calling in my session script into my header which is included on all pages:

    <?
session_start();
session_regenerate_id();
$_SESSION['username']=$email;
 include ('../db_con.php');
?>

在header.php文件中,我想回显用户名,但是当我尝试显示用户名而不是实际名称时,却没有如上所述:

within the header.php file I want to echo out the username but not as mentioned above when I try that it show the length and not the actual name:

            <?php echo $_SESSION['username']; 
            var_dump($_SESSION);
        ?>

会话从我的登录脚本开始:

the session begins from my login script here:

<?
session_start();
if(isset($_SESSION['user'])){
    header("Location:home.php");
    exit;
}


    $dbh=new PDO('mysql:dbname=dashboardr;host=localhost', '******', '******');/*Change The Credentials to connect to database.*/
    $email=$_POST['username'];
    $password=$_POST['pass'];

    if(isset($_POST) && $email!='' && $password!=''){
        $sql=$dbh->prepare("SELECT id,password,psalt FROM user_login WHERE username=?");
        $sql->execute(array($email));

    while($r=$sql->fetch()){
        $p=$r['password'];
        $p_salt=$r['psalt'];
        $id=$r['id'];
    }

        $site_salt="subinsblogsalt"; /*Common Salt used for password storing on site. You can't change it. If you want to change it, change it when you register a user.*/
        $salted_hash = hash('sha256',$password.$site_salt.$p_salt);

    if($p==$salted_hash){
        $_SESSION['user']=$id;
  $_SESSION['username']=$email;

        header("Location:home.php");

    }else{
        echo "<h2>Username/Password is Incorrect.</h2>";
        }
    }
?>

推荐答案

在以下位置看不到用户名的原因:

the reason you do not see the user name in:

$_SESSION['username']

是因为您从来没有把它放在那儿.

is because you never put it there.

您需要像这样将用户名分配给会话:

you need to assign the user name to the session like so:

$_SESSION['username']=$email;

然后您可以echo $_SESSION['username'];想要显示用户名的任何地方.

you can then echo $_SESSION['username']; where ever you want the username displayed.

确保在任何页面上使用您具有session_start();的会话之前,没有任何输出.

make sure on any page you use sessions you have session_start(); before any output.

这篇关于从$ _SESSION获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆