关闭 PHP 代码、自由结果和关闭连接的正确形式
Correct form of closing PHP code, free result and close connection
刚刚学习所以想请教一下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代码中关闭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 ?如果是这样,那么您不需要包含。