如何基于PHP中的get url将用户重定向到不同的页面?

时间:2016-03-29 13:34:16

标签: php url get

我正在建立一个博客。 我想重定向尝试访问帖子页面的用户,而没有获取参数来浏览上一页(如果可能,不使用$_SERVER['HTTP_REFERER']

例如:www.myblog.com/post.php

仅允许通过此网址访问此页面

www.myblog.com/post.php?id=number

2 个答案:

答案 0 :(得分:2)

如果id未设置或为空,您可以使用此PHP代码,只需选择此处 我建议使用empty()函数:

if(!isset($_GET['id'])) {
header("location: url");
}

if($_GET['id'] == NULL) {
header("location: url");
}

if(empty($_GET['id'])) {
header("location: url");
}

答案 1 :(得分:1)

您可以使用会话变量。

首先,在访问post.php的脚本中,您需要启动会话,并且您需要知道id,例如:

session_start();
$_SESSION['post_id'] = $id; //$id is the post id

接下来,在post.php启动会话,你可以使用id重定向,做一个sql查询.......

相关问题