Php 在重定向之前检查数组中是否存在
假设访问者键入:user_001,我希望脚本在重定向用户之前查看数组中是否有001.php,或者转到退出链接 我猜它将涉及in_array()函数Php 在重定向之前检查数组中是否存在,php,arrays,Php,Arrays,假设访问者键入:user_001,我希望脚本在重定向用户之前查看数组中是否有001.php,或者转到退出链接 我猜它将涉及in_array()函数 <?php $exitlink = "error.php"; $page_name=$_POST['usernumber']; $pos=strpos($page_name,"_"); if($pos==true){ $valid_ids = array("001.php", "002.php",
<?php
$exitlink = "error.php";
$page_name=$_POST['usernumber'];
$pos=strpos($page_name,"_");
if($pos==true){
$valid_ids = array("001.php", "002.php", "003.php");
$request = substr($page_name, strpos($page_name, "_") + 1);
header("Location:" . $request . ".php");
}else {
header("Location:" . $exitlink);
}
?>
在脚本决定转到valid.php或
$exitlink
之前,如何让脚本检查数组中是否有$request
?很好且简单,使用in_array()
。还要确保您的有效id不包含.php
,否则您可能会得到{id}.php.php
if(in_array($request, $valid_ids)){
header("Location: " . $request); //Add ".php" only if the request doesn't have it in it. Or else you'll get Location: 001.php.php
}else{
header("Location: " . $exitlink);
}
编辑:对于您的第二个添加项,如果它总是user{some number}
,您可以使用php的replace函数将“user\
替换为”
,如下所示
str_replace("user_", "", $request);
编辑2:
使用explode()代码>
这会将字符串更改为字符串数组,每个字符串由“\u1”分割
比如说
“I_am_a_goat”
与explode(“_”,数组)一起使用时,将拆分为[“I”、“am”、“a”、“goat”]
代码>很好而且很简单,在数组()中使用
。还要确保您的有效id不包含.php
,否则您可能会得到{id}.php.php
if(in_array($request, $valid_ids)){
header("Location: " . $request); //Add ".php" only if the request doesn't have it in it. Or else you'll get Location: 001.php.php
}else{
header("Location: " . $exitlink);
}
编辑:对于您的第二个添加项,如果它总是user{some number}
,您可以使用php的replace函数将“user\
替换为”
,如下所示
str_replace("user_", "", $request);
编辑2:
使用explode()代码>
这会将字符串更改为字符串数组,每个字符串由“\u1”分割
比如说
“I_am_a_goat”
与explode(“_”,数组)一起使用时,将拆分为[“I”、“am”、“a”、“goat”]
代码>好了,伙计们,感谢你们的共同帮助,我破解了它
<?
$exitlink = "error.php";
$page_name=$_POST['usernumber'];
$pos=strpos($page_name,"_");
$request = substr($page_name, strpos($page_name, "_") + 1);
$valid_ids = array("123", "456", "789");
if(in_array($request, $valid_ids)){
header("Location:" . $request . ".php");
}
else{
header("Location: " . $exitlink);
}
?>
XD好了,伙计们,多亏了你们的共同帮助,我成功了
<?
$exitlink = "error.php";
$page_name=$_POST['usernumber'];
$pos=strpos($page_name,"_");
$request = substr($page_name, strpos($page_name, "_") + 1);
$valid_ids = array("123", "456", "789");
if(in_array($request, $valid_ids)){
header("Location:" . $request . ".php");
}
else{
header("Location: " . $exitlink);
}
?>
XDusein_-array()
usein_-array()
ahh是的,你是对的,我的错误(.php)我该如何保存:$request=substr($page-u-name,strpos($page-u-name,“)+1);并检查数组?| |!在数组中-可能是这样?@jinshun_li编辑了原始答案(:它可能会更改为超级用户、新手用户等等,所以对我不起作用:(@jinshun_li再次编辑以满足您的需要(:啊,是的,您是对的,我的错误(.php)我将如何保存:$request=substr($page_name,strop($page_name,“)+1);同时检查数组?| |!在|数组中-可能是这个?@jinshun|li编辑了原始答案(:它可能会更改为、超级用户、新手用户等等,所以对我不起作用:(@jinshun|li再次编辑以满足您的需要(: