我正在尝试使用header函数通过URL传递变量来重定向页面。但是当页面被重定向时,它传递的是实际的变量名,而不是与变量相关联的值。我是PHP的新手,对语法不是很了解,因此非常感谢任何进一步解释正确方法的帮助。
header('location: index.php?id=".$_POST[ac_id]."&err=".$login."');
我正在尝试使用header函数通过URL传递变量来重定向页面。但是当页面被重定向时,它传递的是实际的变量名,而不是与变量相关联的值。我是PHP的新手,对语法不是很了解,因此非常感谢任何进一步解释正确方法的帮助。
header('location: index.php?id=".$_POST[ac_id]."&err=".$login."');
header("Location: index.php?id=".$_POST['ac_id']."&err=".$login);
你在字符串中混合使用了单引号'
和双引号"
,因此无法正确地插入变量。在我上面的示例中,您严格使用双引号"
打开字符串,并将变量与字符串连接。
您的引号中包含引号。请尝试以下方法:
header('location: index.php?id=' . urlencode($_POST['ac_id']) . '&err=' . urlencode($login));
urlencode()函数可以处理url中的任何保留字符。
如果您认为在URL中有一个或两个以上的变量,我建议使用http_build_query()
.
header('Location: index.php?' . http_build_query(array(
'id' => $_POST['ac_id'],
'err' => $login
)));
此外,在location头中技术上不能使用相对路径。虽然它在大多数浏览器中可以工作,但根据RFCs规范,它是无效的。您应该包含完整的URL。
echo "<script>
window.location.href='index.php?id=".$_POST[ac_id]."&err=".$login';
</script>";
header('location: index.php?id='.$_POST['ac_id'].'&err='.$login);
试试这个:
header("location: index.php?id=$_POST[ac_id]&err=$login");
PHP变量在双引号字符串中会被扩展。