如何在php中获取会话ID或用户名?

Nic*_*ick 3 php session session-variables

我有一个用户可以登录的网站.我试过:

<?php echo $_SESSION ['userlogin']
Run Code Online (Sandbox Code Playgroud)

当用户登录时,我将其会话设置为userlogin,但不会显示其用户名.

这是我用过的教程

Jul*_*les 12

你在使用它之前开始了会话吗?

所以设置它:

<?php
   //Fetch the username from the database
   //The $login and $password I use here are examples. You should substitute this
   //query with one that matches your needs and variables.
   //On top of that I ASSUMED you are storing your passwords MD5 encrypted. If not,
   //simply remove the md5() function from below.
   $query = "SELECT name FROM users WHERE login='" . mysql_real_escape_string($login) . "' AND password='" . md5($password)  . "'";
   $result = mysql_query($query);

   //Check if any row was returned. If so, fetch the name from that row
   if (mysql_num_rows($result) == 1) {
      $row = mysql_fetch_assoc_assoc($result);
      $name = $row['name'];

      //Start your session
      session_start();
      //Store the name in the session
      $_SESSION['userlogin'] = $name;
   }
   else {
      echo "The combination of the login and password do not match".
   }
?>
Run Code Online (Sandbox Code Playgroud)

并在另一页上检索它:

<?php
   //Start your session
   session_start();
   //Read your session (if it is set)
   if (isset($_SESSION['userlogin']))
      echo $_SESSION['userlogin'];
?>
Run Code Online (Sandbox Code Playgroud)

编辑

有关如何创建登录表单的更多信息..您说您尝试过设置$_SESSION['user'],但这不起作用.

所以只要确保你在此session_start();之前确实做了一件事.如果你这样做,一切都应该有效.除非您为会话分配一个空变量.因此,双重检查您分配的变量实际上包含一个值.喜欢:

<?php
   session_start();
   echo "Assigning session value to: " . $user;
   $_SESSION['user'] = $user;
?>
Run Code Online (Sandbox Code Playgroud)

在教程中,您将我链接到他们正在做的事情:

$_SESSION[user]=$_GET[userlogin];
Run Code Online (Sandbox Code Playgroud)

这意味着他们将从他们在此创建的登录表单中分配一个值:

function loginform() {
   print "please enter your login information to proceed with our site";
   print ("<table border='2'><tr><td>username</td><td><input type='text' name='userlogin' size'20'></td></tr><tr><td>password</td><td><input type='password' name='password' size'20'></td></tr></table>");
   print "<input type='submit' >";    
   print "<h3><a href='registerform.php'>register now!</a></h3>";    
} 
Run Code Online (Sandbox Code Playgroud)

你看到了<input type='text' name='userlogin' size'20'>.但是<form></form>这个表格周围没有标签..所以这不会发布.所以你应该做的是以下几点:

<form method="POST" action="index.php">
   <label for="userlogin">Username:</label> <input type="text" id="userlogin" name="userlogin" size="20" />
   <label for="password">Password:</label> <input type="password" id="password" name="password" size="20" />
   <input type="submit" value="login" />   
</form>
Run Code Online (Sandbox Code Playgroud)

此表单将表单发布回index.php,其中userloginpassword作为$_POST变量.

index.php中,您可以执行以下操作:

<?php
   //Get variables:
   $login = mysql_real_escape_string($_POST['userlogin']);
   $pass = mysql_real_escape_string($_POST['password']);

   //Check your table:
   $query = "SELECT userlogin FROM users WHERE userlogin = '" . $login . "' AND password='" . $pass . "'";
   $result = mysql_query($query);

   //Check if this user exists:
   if (mysql_num_rows($result) == 1) {
      echo "User exists!";

      //Store the login in the session:
      session_start();
      $_SESSION['userlogin'] = $login;
   }
   else {
      echo "Unknown user";
   }
?>
Run Code Online (Sandbox Code Playgroud)

如果不为你编写完整的代码,我无法更清楚.所以我希望这会对你有所帮助.

  • 'nick'就是一个例子,因为我不知道你的代码.你只需用他们的用户名(我的示例代码中的`$ username`)设置它.我的答案中的一点是,你应该在使用`session_start()`之前开始**会话,然后才能使用它(读或写). (2认同)