让我们说一个访问者输入:user_001,id就像脚本一样,在重定向用户之前查看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", "003.php");
$request = substr($page_name, strpos($page_name, "_") + 1);
header("Location:" . $request . ".php");
}else {
header("Location:" . $exitlink);
}
?>
好的,所以我列出了数组:
$valid_ids = array("001.php", "002.php", "003.php");
在决定转到有效的.php或$request
之前,如何让脚本检查数组中是否有$exitlink
?
答案 0 :(得分:2)
简单明了,使用in_array()
。另外,请确保您的有效身份证件不包含.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的替换功能将"user_"
替换为""
,如下所示。 ..
str_replace("user_", "", $request);
编辑2:
使用explode();
$stringExploded = explode("_", $request);
$requestId = $stringExploded[1];
这会将字符串更改为一个字符串数组,每个字符串都按&#34; _&#34;
分隔例如
与"I_am_a_goat"
["I", "am", "a", "goat"]
会分为explode("_", array);
答案 1 :(得分:0)
<?
$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