如何使用标题找到错误GET值的默认位置 [英] How to locate to a default location on incorrect GET value using headers

查看:117
本文介绍了如何使用标题找到错误GET值的默认位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在接收GET值时将我的php头部定位到特定位置,如果值不正确,它将定位到像home.php这样的默认位置。

I am trying to locate my php header to a specific location on receiving a GET value and if the value is incorrect, it will locate to a default location like home.php

这是我迄今为止所做的工作

here is what I've done so far

<?php
// Check which page is queried

if(isset($_GET['page'])){
    $got = $_GET['page'];
}
else {
    $got = 'home';
}

// Now goto that location

if(header('Location: '.$got.'.php')){
    header('Location: '.$got.'.php');
}
else {
    header('Location: home.php');
}
?>


推荐答案

您可能正在寻找 file_exists ()函数。

You are probably looking for the file_exists() function.

你会这样做;

$got = 'home';
if(isset($_GET['page']))
{
  if(file_exists($_GET['page'].".php"))
  {
    $got = $_GET['page'];
  }

}

header('Location: '.$got.'.php');
exit;

编辑
您可以通过将网页加入白名单你可以让用户看到:

Edit You would be better of by whitelisting the pages you would allow the users to see:

$got = 'home';
$whitelist = array('contact', 'about', 'blog', ...); // could come from your database
if(isset($_GET['page']))
{
  if(file_exists($_GET['page'].".php") && in_array($_GET['page'], $whitelist))
  {
    $got = $_GET['page'];
  }

}

header('Location: '.$got.'.php');
exit;

这确保只有您允许的视图可以被访问。

This ensures that only the views you allow can be accessed.

这篇关于如何使用标题找到错误GET值的默认位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆