我正在尝试创建一个网页,其中包含只能由注册用户访问的用户和信息。是否可以限制未注册用户可以看到的文件?如果是这样,如何?我已经在 index.php 中有一个带有连接的 MySQL 数据库。这是我到目前为止所拥有的:
<head></head>
<body>
<h3>Signup Here:</h3>
<form method="post" action="userindex.php">
Please enter user name: <input type="text" name="username" value="" /><br />
Please enter password: <input type="password" name="password" value="" />
<br />
<input type="submit" value="Submit"/>
</form>
</body>
<?php
include ("dbroutines.php");
if (isset($_POST['username'])) {
if ($_POST['username']>'' && $_POST['password']>'' ) {
$q="insert into users (Name, Password ) values ('".$_POST['username']."', '".$_POST['password']."')";
echo 'query='.$q;
$conn=db_connect();
$result=$conn->query($q);
echo '<br />xxx'.$conn->error."xxx";
unset($_POST['username']);
unset($_POST['password']);
} else {
echo 'Please enter both username AND password!';
}
}
$q="select Name, Password from users";
$conn=db_connect();
$result=$conn->query($q);
echo 'xxx'.$conn->error."xxx";
if ($result){
echo '<hr />';
for ($count=0; $row=$result->fetch_row(); ++$count ) {
echo $count." Name=".$row[0]." password=".$row[1].'<br />';
}
echo '<b style="color:red;">there are '.$count.' users in your database!'.'</b><hr />';
}
由此,您能否指定哪种类型的用户可以访问某些文件,例如 userindex.php?
最佳答案
我认为验证用户并不是万无一失的解决方案。您必须在 session 中保留一个 token 以记住该用户是注册用户。您必须创建一个名为 Security.php 的通用 php 页面,您将在其中放置以下代码,因为聪明的用户可以直接键入 URL 并访问您的 secret 页面。您需要在要保护的每个 php 页面的顶部包含此页面。
if (!isset($_SESSION['AuthId'])) {
header('Location:Login.php');
exit;
}
关于php - 如何在为注册用户提供完全访问权限的同时限制未注册用户的访问权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15651099/