最佳答案 您想要的-6ren">
gpt4 book ai didi

php - 如何在php中仅当用户是管理员时才允许删除

转载 作者:行者123 更新时间:2023-11-29 12:22:08 26 4
gpt4 key购买 nike

如果用户名是admin,如何允许删除。

我想只允许删除管理员来删除记录请帮我解决这个问题谢谢......

session .php

<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "");
// Selecting Database
$db = mysql_select_db("dsrs", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from admin where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}
?>

删除.PHP

<?php
/*
DELETE.PHP
Deletes a specific entry from the 'drivers' table
*/

include('session.php');

// connect to the database
include('connect-db.php');

// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
$id = $_GET['id'];

// delete the entry
$result = mysql_query("DELETE FROM admission WHERE id=$id")
or die(mysql_error());
echo "<center>Delete.!</center>";
}
?>

最佳答案

您想要的是只允许admin用户删除指定的ID。我不知道如何将用户设置为“管理员”,但是这个伪代码将为您提供一般要点。

if (isset($_GET['id']) && is_numeric($_GET['id'])) {
// get id value
$id = $_GET['id'];

// delete the entry if admin
if($_SESSION['username'] == 'admin') {
$result = mysql_query("DELETE FROM admission WHERE id=$id") or die(mysql_error());
echo "<center>Delete.!</center>";
}
}

我不知道您在 session 中将用户名存储为什么,因此您需要更改它。

<小时/>

我建议您停止使用 mysql_* 库,因为它已被弃用。您应该查看 PDO和/或 MySQLi Prepared Statements .

关于php - 如何在php中仅当用户是管理员时才允许删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28846179/

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