【发布时间】:2017-02-06 12:34:56
【问题描述】:
如果直接访问(没有变量)如何将页面重定向到主页? 假设我们有一个链接“example.com/site?website=$_GET[variable]”,如果像这样访问它,那就没问题了!但是如果有人尝试使用此链接“example.com/site”直接访问它,那么它应该重定向到主页。 怎么做?
【问题讨论】:
-
到目前为止你的代码在哪里?
如果直接访问(没有变量)如何将页面重定向到主页? 假设我们有一个链接“example.com/site?website=$_GET[variable]”,如果像这样访问它,那就没问题了!但是如果有人尝试使用此链接“example.com/site”直接访问它,那么它应该重定向到主页。 怎么做?
【问题讨论】:
if(isset($_GET['variable']) && !empty(trim($_GET['variable'])))
{
//your rest of code
} else {
header("Location:redirectpage.php");
}
【讨论】:
您只需要在脚本顶部检查是否设置了全局 $_GET 变量(如果设置了任何查询字符串变量,它将是),如果没有设置则重定向。
if(!isset($_GET)):
header("Location: /");
exit();
endif;
您可以在代码顶部添加它,如果设置了查询字符串,那么您的代码将照常进行。
【讨论】:
这应该是评论,但我在这里的声誉很低。
我有点困惑。但是你为什么不只检查链接中变量的存在 喜欢:
<?php
//在每个页面的下面添加代码或将其放在文件中并包含在任何地方 $access = false;
if(isset($_GET['variable_name'])){
//obviously any one can pass any funny variable, so you will need to match it with your database to verify or the variable might be a unique word of some sorts.
$access = true;
}else{
$access = false;
}
if($access == false){
die('No access to view this page');
}
【讨论】: