[英]Adding user_id into Session mysql
I am trying to failing to figure out how to add my user_id from my database to a session like my username is. 我试图无法弄清楚如何将我的user_id从数据库添加到类似用户名的会话中。 I would like it so when I log in I can called the session to call the user id posts based on the username used to log in. 我想这样,当我登录时,我可以根据用于登录的用户名调用会话以调用用户ID帖子。
How do I do this? 我该怎么做呢?
my login php: 我的登录php:
<?php
session_start();
$connect = mysql_connect( "localhost", "user", "pass") or die('Database could not connect');
$select = mysql_select_db( "database", $connect) or die('Database could not select');
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
chckusername($username, $password);
}
function chckusername($username, $password){
$check = "SELECT * FROM users WHERE username='$username'";
$check_q = mysql_query($check) or die("<div class='loginmsg'>Error on checking Username<div>");
if (mysql_num_rows($check_q) == 1) {
chcklogin($username, $password);
}
else{
echo "<div id='loginmsg'>Wrong Username</div>";
}
}
function chcklogin($username, $password){
$login = "SELECT * FROM users WHERE username='$username' and password='$password' ";
$login_q = mysql_query($login) or die('Error on checking Username and Password');
print_r($lohin_q);
if (mysql_num_rows($login_q) == 1){
header('Location: http://ct6014-williams.studentsites.glos.ac.uk/pages-multi-page/home.php');
$_SESSION['username'] = $username;
$_SESSION['user_id'] = $userid; //this is where I want to store the user id
}
else {
echo "<div id='loginmsg'>Wrong Password </div>";
}
}
?>
on home.php: 在home.php上:
<?php echo "Your username is " . $_SESSION["username"] . "."; ?>
<?php echo "Your id is " . $_SESSION["user_id"] . "."; ?>
database names: 数据库名称:
user_id
, username
, user_first_name
, user_last_name
, user_email
, password
, user_reg_date
user_id
, username
, user_first_name
, user_last_name
, user_email
, password
, user_reg_date
EDIT: 编辑:
Ahh, I forgot to say, I have used include( 'login.php' ); 啊,我忘了说,我用过include('login.php'); which created the session when I logged in as a user. 当我以用户身份登录时创建了会话。 The first username session echos but I cannot seem to figure out hoe to set up the user_id session 第一个用户名会话回显,但我似乎无法找出设置user_id会话的方法
In chcklogin()
function, use mysql_fetch_array()
function to fetch the row from the result set and assign the required user data to $_SESSION
. 在chcklogin()
函数中,使用mysql_fetch_array()
函数从结果集中获取行,并将所需的用户数据分配给$_SESSION
。 So change your if (mysql_num_rows($login_q) == 1){ ...
block in the following way, 因此,以以下方式更改if (mysql_num_rows($login_q) == 1){ ...
if (mysql_num_rows($login_q) == 1){
$row = mysql_fetch_array($login_q);
$_SESSION['username'] = $row['username'];
$_SESSION['user_id'] = $row['user_id'];
header('Location: http://ct6014-williams.studentsites.glos.ac.uk/pages-multi-page/home.php');
exit();
} else {
echo "<div id='loginmsg'>Wrong Password </div>";
}
Sidenote: Don't use mysql_*
functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. 旁注:不要使用mysql_*
函数,从PHP 5.5开始不推荐使用它们,并在PHP 7.0中将其完全删除。 Use mysqli
or pdo
instead. 改用mysqli
或pdo
。 And this is why you shouldn't use mysql_*
functions . 这就是为什么您不应该使用mysql_*
函数的原因 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.