如何在 php 中获取会话 ID 并显示它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21302733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:01:24  来源:igfitidea点击:

How can i get session id in php and show it?

phpsession

提问by AliM

I need to use some unique id like session id. How can I get session id in php?

我需要使用一些唯一的 ID,如会话 ID。如何在php中获取会话ID?

回答by zerokavn

session_start();    
echo session_id();

回答by Alma Do

I would not recommend you to start session just to get some unique id. Instead, use such things as uniqid()because it's intended to return unique id.

我不建议你开始会话只是为了获得一些唯一的 id。相反,使用诸如uniqid()因为它旨在返回唯一 id 之类的东西。

However, if you already have session, then, of course, use session_id()to get your session id - but do not rely on that, because "unique id" isn't same as "session id" in common sense: for example, multiple tabs in most browsers will use same process, thus, use same session identifier in result - and, therefore, different connections will have same id. It's your decision about desired behavior, I've mentioned this just to show the difference between session id and unique id.

但是,如果您已经有会话,那么当然可以使用session_id()来获取会话 ID - 但不要依赖它,因为“唯一 ID”与常识中的“会话 ID”不同:例如,多个选项卡在大多数浏览器中将使用相同的进程,因此,在结果中使用相同的会话标识符 - 因此,不同的连接将具有相同的 ID。这是您对所需行为的决定,我提到这一点只是为了显示会话 ID 和唯一 ID 之间的区别。

回答by Utkarsh Singh

Before getting a session id you need to start a session and that is done by using: session_start() function.

在获取会话 id 之前,您需要启动一个会话,这是通过使用: session_start() 函数来完成的。

Now that you have started a session you can get a session id by using: session_id().

现在您已经开始了一个会话,您可以通过使用 session_id() 来获取会话 ID。

/* A small piece of code for setting, displaying and destroying session in PHP */

<?php
session_start();
$r=session_id();

/* SOME PIECE OF CODE TO AUTHENTICATE THE USER, MOSTLY SQL QUERY... */

/* now registering a session for an authenticated user */
$_SESSION['username']=$username;

/* now displaying the session id..... */
echo "the session id id: ".$r;
echo " and the session has been registered for: ".$_SESSION['username'];


/* now destroying the session id */

if(isset($_SESSION['username']))
{
    $_SESSION=array();
    unset($_SESSION);
    session_destroy();
    echo "session destroyed...";
}
?>

回答by Hassan

You have uniqid function for unique id

你有唯一 id 的 uniqid 函数

You can add prefix to make it more unique

您可以添加前缀以使其更独特

Source : http://pk1.php.net/uniqid

来源:http: //pk1.php.net/uniqid

回答by Surendra

if(isset($_POST['submit']))
   {  
 if(!empty($_POST['login_username']) && !empty($_POST['login_password']))
    {
     $uname = $_POST['login_username'];
     $pass = $_POST['login_password'];
     $res="SELECT count(*),uname,role FROM users WHERE uname='$uname' and  password='$pass' ";
$query=mysql_query($res)or die (mysql_error());  

list($result,$uname,$role) = mysql_fetch_row($query);   
$_SESSION['username'] = $uname;
$_SESSION['role'] = $role;
 if(isset($_SESSION['username']) && $_SESSION['role']=="admin")
 {
 if($result>0)
      {
       header ('Location:Dashboard.php');
      }
      else
      {
         header ('Location:loginform.php');
      }
 }

回答by praveen

In the PHP file first you need to register the session

首先在PHP文件中你需要注册会话

 <? session_start();
     $_SESSION['id'] = $userData['user_id'];?>

And in each page of your php application you can retrive the session id

并且在您的 php 应用程序的每个页面中,您都可以检索会话 ID

 <? session_start()
    id =  $_SESSION['id'];
   ?>