标头位置不起作用,仍在保护页面中 [英] header location doesn't work and still in protect page
问题描述
此代码使用保护功能.进行权限访问
This code use protect function . to do permission access
我查看地址栏后发现它仍在保护页面中
这是保护页面":
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正确.
@Mark B above has it right.
还-仅在运行时没有 输出到浏览器的情况下才可以设置标题-如果您print_r($v)
,则已经发送了标题.确保对函数的调用位于session_start()
之后.
Also - Headers are only able to be set if there is no output to the browser when they are run - If you print_r($v)
, headers are already sent out. Make sure your call to your function is the top possible line, right after 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" );
可能是一个好主意,而不是重定向,如果您不希望用户看到消息,除非他们在不应该去的地方闲逛.
Use of header("HTTP/1.1 403 Unauthorized" );
may be a good idea instead of redirecting, if you don't expect a user to see the message unless they are poking around where they shouldn't.
您也许还可以使用header("Location: http://someplace/",TRUE,403);
同时发送403代码和重定向(因此,您可能在此站点上使用的所有API都将识别出它们是否无法正确登录).
You may also be able to use header("Location: http://someplace/",TRUE,403);
to send a 403 code and a redirect at the same time (so any APIs you may use against this site will recognize if they failed to log in correctly).
这篇关于标头位置不起作用,仍在保护页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!