php - PHP中的标头重定向
问题描述
我正在(试图)编写一些代码来根据他们的设备在我的网站内重定向用户。
如果用户在移动设备上,我想在当前路径的开头添加一个目录“/m/”,并将它们重定向到新位置的同一个文件(如果可能,使用相同的 url 参数,但不是必需的)
即www.mysite.com/shop/items1/shop.php?para1=somestuff ==> www.mysite.com/m/shop/items1/shop.php?para1=somestuff
我得到的错误是:
无法访问此站点
网页位于
http://dev4/shop/items1/shop.php?uncd=c74f474e2c396da2993cfb344007ad6a&iid=014ec54f3746fd834da71ad21e76a0fa&crtfn=D2
可能暂时关闭或可能....... ERR_UNSAFE_REDIRECT
我的代码是
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include ($path . '/includes/functions.php');
$pathloc = ''; // Variable used for new folder location ie /m/ for mobile /t/ for tablet
$device= device(); // function to get the type of device
if ($device=="IsMob")
{$pathloc = '/m';
header("Location: " . $path . $pathloc . dirname($_SERVER['SCRIPT_NAME']) . "/" . basename($_SERVER["REQUEST_URI"], ".php"));
}
?>
我正在使用主机头“dev4”。
我怀疑目录和文件位置有误。我尝试将字符串输出到屏幕以检查正在构建的 url,一切看起来都很好,所以我有点难过。
任何帮助都将不胜感激。
TIA
解决方案
As is always the way, as soon as I post the question up I've been struggling with for 2 days, I work out the answer myself.
Reference for anyone it may help.
header("Location: http://Dev4" . $pathloc.dirname($_SERVER['SCRIPT_NAME']). "/". basename($_SERVER["REQUEST_URI"], ".php"));
推荐阅读
- shell - GitHub 操作的输出为空
- c# - 将 struct* 从 c# 传递给 c++ dll
- php - PHP SQLSRV 驱动程序将 NULL 作为空字符串获取
- aws-iot - 对于从 AWS IoT greengrass lambda 到 AWS IoT Core 规则引擎的通信,是否应将 lambda 配置为设备(事物)?
- javascript - node.js / javascript 应用程序是否支持 AES 密文窃取?
- discord - Discord py - 在“on_raw_reaction_add”函数中创建某个类别的频道?
- vb.net - 如何解析 XML 而不会对文件中的注释抛出异常
- javascript - 为什么我只在 Javascript 计算器中的减号运算符中看到 NaN?
- css - 如何降低标签的高度 - 棱角材料
- c++ - 如何旋转 AVFrame?