gpt4 book ai didi

PHP session (用户/管理员)用户级别

转载 作者:行者123 更新时间:2023-11-29 19:20:31 25 4
gpt4 key购买 nike

我只是 php 的新手,需要几周时间自学并制作各种项目来学习如何做事......

我目前正在制作一个基本项目,其中包括...索引(登录页面)登记主页(用户级别)管理员(管理员级别)登出数据库连接

我的数据库中有一个名为 userlevel 的列,默认值为 0,我会将管理员帐户更改为 1....

目前,任何登录的用户都可以访问 home.php 页面,因为 session 变量是“user”,因此只要任何用户登录,他们就会转到 home.php,如果未登录,则返回到索引有效...就像我说的,我对 php 很陌生,今天才开始学习 session ,所以这一切都有点令人难以承受...基本上我只是将相同的页面从主页粘贴到管理员以开始编辑 session 详细信息,以便它只允许用户级别为1的用户访问管理员,否则返回索引....我的相关页面的代码如下......

index.php(登录页面)

<?php
ob_start();
session_start();

require_once 'dbconnect.php';



// it will never let you open index(login) page if session is set
if ( isset($_SESSION['user'])!="" ) {
header("Location: home.php");
exit;
}

$error = false;

if( isset($_POST['btn-login']) ) {

// prevent sql injections/ clear user invalid inputs
$email = trim($_POST['email']);
$email = strip_tags($email);
$email = htmlspecialchars($email);

$pass = trim($_POST['pass']);
$pass = strip_tags($pass);
$pass = htmlspecialchars($pass);
// prevent sql injections / clear user invalid inputs

if(empty($email)){
$error = true;
$emailError = "Please enter your email address.";
} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$error = true;
$emailError = "Please enter valid email address.";
}

if(empty($pass)){
$error = true;
$passError = "Please enter your password.";
}

// if there's no error, continue to login
if (!$error) {

$password = hash('sha256', $pass); // password hashing using SHA256

$res=mysql_query("SELECT id, fname, pass FROM project WHERE email='$email'");
$row=mysql_fetch_array($res);
$count = mysql_num_rows($res); // if uname/pass correct it returns must be 1 row

if( $count == 1 && $row['pass']==$password ) {
$_SESSION['user'] = $row['id'];
header("Location: home.php");
} else {
$errMSG = "Incorrect Credentials, Try again...";
}


}

}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign In</title>
</head>
<body>


<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">


<input type="email" name="email" placeholder="Enter Your Email" value="<?php echo $email; ?>" maxlength="40" />
<?php echo $emailError; ?><br>

<br>

<input type="password" name="pass" placeholder="Enter Your Password" maxlength="15" />
<?php echo $passError; ?><br>

<br>

<button type="submit" name="btn-login">Sign In</button><br>


<br>
<br>
<a href="register.php">Register</a> <a href="index.php">Sign in</a> <a href="admin.php">Admin</a>
<br>
<?php
if ( isset($errMSG) ) {

?>
<?php echo $errMSG; ?>

<?php
}
?>


</body>
</html>
<?php ob_end_flush(); ?>

home.php

<?php
ob_start();
session_start();
require_once 'dbconnect.php';


// if session is not set this will redirect to login page
if( !isset($_SESSION['user']) ) {
header("Location: index.php");
exit;
}
// select loggedin users detail
$res=mysql_query("SELECT * FROM project WHERE id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);





?>




<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['fname']; ?></title>
</head>
<body>

Hello <?php echo $userRow['fname']; ?> you are sucessfully logged in!

<br>Your last name is <?php echo $userRow['lname']; ?>
<br>Your email address is <?php echo $userRow['email']; ?>

<br><br><br><br><br><br><br><br><br><br>







<br><br><br><br><br><br><br><br><br><br>


<a href="logout.php?logout"></span>Sign Out</a></li>


<br>
<br>
<a href="register.php">Register</a> <a href="index.php">Sign in</a> <a href="admin.php">Admin</a>

</body>
</html>
<?php ob_end_flush(); ?>

admin.php

<?php
ob_start();
session_start();

require_once 'dbconnect.php';



// if session is not set this will redirect to login page
if( !isset($_SESSION['user']) ) {
header("Location: index.php");
exit;
}

// select loggedin users detail
$res=mysql_query("SELECT * FROM project WHERE id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);





?>




<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['fname']; ?></title>
</head>
<body>


Hello <?php echo $userRow['fname']; ?> you are sucessfully logged in!

<br>Your last name is <?php echo $userRow['lname']; ?>
<br>Your email address is <?php echo $userRow['email']; ?>

<br><br><br><br><br><br><br><br><br><br>


<h1><?php echo $userRow['userlevel']; ?></h1>





<br><br><br><br><br><br><br><br><br><br>


<a href="logout.php?logout"></span>Sign Out</a></li>

<br>
<br>
<a href="register.php">Register</a> <a href="index.php">Sign in</a> <a href="admin.php">Admin</a>

</body>
</html>
<?php ob_end_flush(); ?>

如果有人可以告诉我如何使 session 查看数据库中的用户级别行以提取存储的信息(用户或管理员为0或1)以及如何相应地更改index.php(登录)页面,以及如何编辑admin.php以仅允许具有用户级别1的登录用户查看该页面?

抱歉,如果这含糊不清或到处都是,我仍然每天都在寻找新东西

谢谢

最佳答案

如果您将 is_admin 字段添加到您的项目表(我假设是您的用户表),那么您可以使用如下所示的内容来测试用户是否是管理员:

$sql = "SELECT * FROM project WHERE id='" . mysql_real_escape_string($id) . "'";
$res = mysql_query($sql);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if (!$row || !$row['is_admin']) {
die();
}

(我不使用 PHP,也没有测试过这段代码,所以将其视为伪代码)

关于PHP session (用户/管理员)用户级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42476700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com