[英]Restrict Access Page for Particular user in PHP
我的数据库中有3位用户
id username password
1 bhaku 123
2 navin 124
3 avinash 123
我想限制页面访问。 登录后,用户“ bhaku”将可以访问该页面。 &其他用户无法通过登录或直接访问。
我用下面的代码;
session_start();
if($_SESSION["full_name"]!="bhaskar")
{
header('Location: index.php');
}
但会引发“间接循环”错误。
任何用户都可以访问该页面。
那么解决方案是什么?
create one more row in ur table with name access as shown below
id username password access
1 bhaku 123 0
2 navin 124 1
3 avinash 123 1
now allow user to view the page depending on the access value
example
session_start();
if($_SESSION["access"]!=0)
{
header('Location: index.php');
}
Hope it helps.
而不是受用户名限制; 我认为您必须已经为其分配ID来进行限制; 你要做的是
也创建ID会话;
session_start(); if($_SESSION["user_id"]!==1 OR isset($_SESSION["user_id"])) { header('Location: index.php'); exit; }
在数据库中创建名为Page access或您喜欢的东西的行。 然后插入一个标志值,例如0表示未阻止,1表示已阻止。
username access
jijo 0
user1 1
在页面顶部,查询数据库并检查标志是0还是1,如果要在多于1页的页面中使用它,则将该值存储在会话中。 如果标志为1,则表示用户已被阻止,然后将用户重定向到其他任何页面。
尝试打印$ ['full_name']的名称,并确保获得预期的结果。
使用支票
<?php
session_start();
if(isset($_SESSION['full_name'] && $_SESSION['full_name'] == "bhaskaran")
{
// redirect where you want to move this user
header('Location: index.php');
exit;
}
?>
还有一点是,使用id而不是name进行重定向会更容易,因为name可以为大写,并且可能会遇到潜在的安全问题。
如果您有一组想要提供额外访问权限的相似人员,则如上所述,创建一个单独的字段来存储其访问权限。 将其设置为1或所需的任何值,并将其包含在if()子句中以检查是否也设置了该值。
希望对您有帮助。 请让我知道是否需要任何进一步的信息。
您的方法应该起作用。 尝试换向。
session_start();
if(isset($_SESSION["full_name"]) && $_SESSION["full_name"] =="bhaskar"){
//your content
}
else
header('Location: index.php');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.