此代码使用保护函数。进行权限访问
我查看了地址栏,发现它仍在保护页
这是"保护页":
foreach($access_level as $k => $v)
{
// print_r($v); // output 12
protect($v);// call function in loop to get the values of array
}
}
global $v ;
function protect($v){
if($_SESSION['sessionloginid']==true )
{
if( $v ==1)
{header(" location: http://localhost/database/agtdatabase/agt_site/display/display.php");}
}
}
@上面的标记B是对的。
此外-只有在运行时没有输出到浏览器的情况下才能设置标头-如果您print_r($v)
,则标头已发送出去。确保您对函数的调用是最上面的行,就在session_start()
之后。
<?php
session_start();
protect();
/// Other code ///
function protect() {
if($_SESSION['sessionloginid']!==true) { header("Location: http://someplace/index.php"); }
}
使用header("HTTP/1.1 403 Unauthorized" );
可能是一个好主意,而不是重定向,如果你不希望用户看到消息,除非他们在不该看到的地方乱翻。
您还可以使用header("Location: http://someplace/",TRUE,403);
同时发送403代码和重定向(因此,您可能针对此网站使用的任何API都会识别它们是否未能正确登录)。
您将$v
作为参数传递给函数,但函数定义没有参数:
function protect(){
^---no args
PHP正好有两个变量作用域:本地和全局。您在函数内部全局化的$v可能不会看到您在上面的foreach循环中定义的$v。例如
$v = 1; // global scope
function foo() {
$v = 2; // local scope
bar();
}
function bar();
global $v;
echo $v; // outputs 1
}
你应该有
function protect($v) {
if ($v == .....) { ... }
}
相反。