简体   繁体   English

如果用户手动更改网址,如何将用户重定向到主页?

[英]How can I redirect a user to homepage if they change the url manually?

Let's say the user is in the page --> mysite.com/product.php?id=5 and he manually removed the ?id=5 and r=the url became this way--> mysite.com/product.php 假设用户在页面 - > mysite.com/product.php?id=5并且他手动删除了?id = 5和r = url就变成了这种方式 - > mysite.com/product.php

How can I redirect him to the homepage? 我怎样才能将他重定向到主页?

Just check to see if the id is missing from the query string. 只需检查查询字符串中是否缺少id。 If so, do the redirect. 如果是,请执行重定向。

if (!isset($_GET['id']) || empty($_GET['id'])) {
    //redirect
}

If you still want the page to be accessible if Parameters are set use this method: 如果您仍希望在设置参数时可以访问该页面,请使用以下方法:

if(!isset($_GET)){
    header("Location: http://mysite.com/");
    exit;
}

Check to see if no variables have been passed to the page, and then redirect accordingly. 检查是否没有变量传递到页面,然后相应地重定向。
exit after the Header to be sure that no text is sent to the browser that will break the header. 在Header之后exit以确保没有文本发送到将断开标题的浏览器。

If this will become a dead page, use a 403: 如果这将成为死页,请使用403:

header ('HTTP/1.1 301 Moved Permanently');
header ("Location: http://mysite.com/");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM