gpt4 book ai didi

php - 关闭PHP代码的正确形式,自由结果并关闭连接

转载 作者:行者123 更新时间:2023-11-29 22:32:07 25 4
gpt4 key购买 nike

我刚刚学习,想请教一下PHP代码中关闭IF的正确方法:

这是页面末尾的示例一

<?php

session_start();
$identification = $_SESSION['session_name'];
include 'inc/database.php';
$query = "SELECT * FROM `members` WHERE `username` = '$identification' LIMIT 1";
$result = mysqli_query($database, $query);
$user_info = mysqli_fetch_array($result, MYSQLI_ASSOC);
$user_level = $user_info["privs"];
if ($user_level=='ADMIN'){}
else {
header('Location: security.php?error=missing correct privileges. user is not admin');
exit();
?>
<html>
<head>
<title>Zona de Pruebas</title>
</head>
<body>
Username: <?php echo $user_info['username']?>, can be here because is admin</div>
</body>
</html>
<?php
}
mysqli_free_result($result);
mysqli_close($database);
?>

这是 HTML 正文之前的示例二

<?php

session_start();
$identification = $_SESSION['session_name'];
include 'inc/database.php';
$query = "SELECT * FROM `members` WHERE `username` = '$identification' LIMIT 1";
$result = mysqli_query($database, $query);
$user_info = mysqli_fetch_array($result, MYSQLI_ASSOC);
$user_level = $user_info["privs"];
if ($user_level=='ADMIN'){}
else {
header('Location: security.php?error=missing correct privileges. user is not admin');
exit();
}
mysqli_free_result($result);
mysqli_close($database);
?>
<html>
<head>
<title>Zona de Pruebas</title>
</head>
<body>
Username: <?php echo $user_info['username']?>, can be here because is admin</div>
</body>
</html>

正确的做法是什么?在顶部还是在最后?使用免费结果并关闭数据库连接是个好主意吗?这是正确的方法吗?我在尝试学习。

最佳答案

你是这个意思吗?

<?php

session_start();
$identification = $_SESSION['session_name'];
include 'inc/database.php';
$query = "SELECT * FROM `members` WHERE `username` = '" . $identification . "' LIMIT 1";
$result = mysqli_query($database, $query);
$user_info = mysqli_fetch_array($result, MYSQLI_ASSOC);

//Close your connection here, you dont need it anymore.

mysqli_free_result($result);
mysqli_close($database);

$user_level = $user_info["privs"];

//If $user_level is not equal to 'ADMIN'
if ($user_level != 'ADMIN'){

header('Location: security.php?error=missing correct privileges. user is not admin');
exit();
?>
<html>
<head>
<title>Zona de Pruebas</title>
</head>
<body>
Username: <?php echo $user_info['username']?>, can be here because is admin</div>
</body>
</html>
//End the If
<?php } ?>

我建议将您的 html 代码放入 php 文件中并像这样调用它:

include('view/myView.php');

所有变量都将传递到这个单独的文件。

所以它可能看起来像这样:

<?php

session_start();
$identification = $_SESSION['session_name'];
include 'inc/database.php';
$query = "SELECT * FROM `members` WHERE `username` = '" . $identification ."' LIMIT 1";
$result = mysqli_query($database, $query);
$user_info = mysqli_fetch_array($result, MYSQLI_ASSOC);

//Close your connection here, you dont need it anymore.

mysqli_free_result($result);
mysqli_close($database);

$user_level = $user_info["privs"];

//If $user_level is not equal to 'ADMIN'
if ($user_level != 'ADMIN'){

header('Location: security.php?error=missing correct privileges. user is not admin');
exit();
include('view/myView.php');
//End the If
} ?>

myView.php

    <html>
<head>
<title>Zona de Pruebas</title>
</head>
<body>
Username: <?php echo $user_info['username']?>, can be here because is admin</div>
</body>
</html>

看起来干净多了。或者 HTML 位于 security.php 内?如果是这样,那么您不需要包含。

关于php - 关闭PHP代码的正确形式,自由结果并关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29712312/

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