从数据库中的表中获取一个特定字段并在会话中使用它
问题描述:
I'm currently building a website that has a log in form. Depending on which role this user has a navigation bar has to be displayed accordingly.
For Example : Someone who hasn't yet logged in will have a "login" link. If someone has logged in and is not an admin a different nav bar is displayed.
All log in details are stored in a db and this has to be done using sessions since it will send the user to a different page.
Any ideas ?
Code till now :
$query = "SELECT count(*) FROM tbl_users
WHERE username = '$username' AND password = '$pass'";
$result = mysqli_query($connection, $query)
or die("Error in query: ". mysqli_error($connection));
$row = mysqli_fetch_row($result);
$counter = $row[0];
if($counter > 0)
{
session_start();
$_SESSION['user']= $username;
$_SESSION['role'] = $row['role'];
我正在构建一个具有登录表单的网站。 根据该用户具有导航栏的角色必须相应地显示。 p>
例如:尚未登录的用户将拥有“登录”链接。 如果有人登录且不是管理员,则会显示不同的导航栏。 p>
所有登录详细信息都存储在数据库中,这必须使用会话来完成,因为它会将用户发送到不同的页面。 p>
任何想法? p>
代码到现在为止: p>
$ query =“SELECT count(*)FROM tbl_users
WHERE username ='$ 用户名'AND密码='$ pass'“;
$ result = mysqli_query($ connection,$ query)
或die(”查询错误:“。mysqli_error($ connection));
$ row = mysqli_fetch_row($ result);
$ counter = $ row [0];
if($ counter> 0)
{
} session_start();
$ _SESSION ['user'] = $ 用户名;
$ _SESSION ['role'] = $ row ['role'];
code> pre>
div>
答
a somewhat vague question but, check your role.
if($_SESSION['role']=='admin')
echo 'I am admin';
elseif($_SESSION['role']=='customer')
echo 'I am customer';
else
echo 'No valid role / not logged in';