如何在 php 中获取会话 ID 或用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8703507/
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
How Can I get a Session Id or username in php?
提问by Nick
I have a website where users can log in. I have tried:
我有一个用户可以登录的网站。我试过:
<?php echo $_SESSION ['userlogin']
and When users login I set their session as userlogin, but it won't show their username.
This is the tutorial I used
当用户登录时,我将他们的会话设置为 userlogin,但不会显示他们的用户名。
这是我使用的教程
回答by Jules
Did you start the sessionbefore using it?
您是否在使用之前启动了会话?
So to set it:
所以要设置它:
<?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".
}
?>
And to retrieve it on another page do:
并在另一个页面上检索它:
<?php
//Start your session
session_start();
//Read your session (if it is set)
if (isset($_SESSION['userlogin']))
echo $_SESSION['userlogin'];
?>
EDIT
编辑
Some more information about how to create a loginform.. You say you tried setting $_SESSION['user']
but that this didn't work.
有关如何创建登录表单的更多信息.. 您说您尝试过设置$_SESSION['user']
,但这不起作用。
So just make sure that you actually did a session_start();
before that. If you did, everything should work. Unless you assign an empty variable to the session. So doublecheck that the variable you assign actually contains a value. Like:
所以只要确保你在这session_start();
之前确实做了一个。如果你这样做了,一切都应该有效。除非您为会话分配一个空变量。因此,请仔细检查您分配的变量是否确实包含一个值。喜欢:
<?php
session_start();
echo "Assigning session value to: " . $user;
$_SESSION['user'] = $user;
?>
In the tutorial you linked me to they are doing:
在您将我链接到他们正在做的教程中:
$_SESSION[user]=$_GET[userlogin];
This means they are assigning a value they got from their loginform that they create here:
这意味着他们正在分配一个从他们在此处创建的登录表单中获得的值:
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>";
}
There you see <input type='text' name='userlogin' size'20'>
. But there is no <form></form>
tag around this form.. So this won't properly post. So what you should do is the following:
你看<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>
This form will post the form back to index.phpwith userloginand passwordas $_POST
variables.
这种形式将发布的形式回到index.php文件与用户登陆和密码作为$_POST
变量。
In your index.phpyou can then do:
在您的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";
}
?>
I can't make it much clearer without writing the entire code for you. So I hope this helps you.
如果不为您编写完整的代码,我无法让它更清楚。所以我希望这对你有帮助。
回答by Michael
Put session_start(); before the echo
把 session_start(); 在回声之前
回答by TJHeuvel
Without showing more code we cant comment much, but are you sure you are calling session_startin each page you use your session variable too? Also ensure you are using the same capitalisation (userlogin != UserLogin).
没有显示更多代码,我们无法评论太多,但是您确定在每个使用会话变量的页面中都调用session_start吗?还要确保您使用相同的大写字母(userlogin != UserLogin)。