我是php的新手。我正在尝试重定向到某个页面。起初,我正在尝试通过下面的硬编码,但仍然无法正常工作。
$location = 'https://mywebsite.com/abc/?a=xyz';
header('Location:'.$location);
上面只是重定向到这里:https://mywebsite.com/abc/
但我无法在网址末尾获取查询参数。请有人告诉我上面的解决方法是什么?
编辑:
在日志中,我收到如下警告:
PHP Warning: Cannot modify header information - headers already sent by (output started at /myfile1.php:34) in /myfile2.php on line 317,
这是myfile1.php代码:
class MyClass32
{
function myFun() {
?>
<head>
<body>
<div align=center>
<p><br>
<table width=778 cellpadding=0 cellspacing=0 border=0>
<tr>
<td bgcolor="#3399cc" width=6> </td>
<td colspan=3>
<table width="100%" cellPadding=0 cellSpacing=0 border=0>
<tr>
<?php // **THIS IS LINE 34**
//START TOP RIGHT MENU
$abc = new MyClass($this);
if ($this->isLog()) $xyz->goInto();
//END TOP RIGHT MENU
?>
答案 0 :(得分:1)
这里有一些事情发生。
最重要的是,您需要使用双引号"
而不是单引号设置标题,有关详细信息,请参阅PHP Header Location with parameter。
您还需要在标头之后设置exit;
或die语句,以确保在达到标头后PHP的页面处理停止。
您的错误输出告诉您页面在标题值之前向浏览器输出内容,这可以包括空格或PHP。
检查
之类的内容 <--- file start
<?php
$var...
header("stuff.php");
<?php
之前有一个空格,所以这会给你的错误信息。检查代码中的这些内容。
查看您的网页来源myfile1.php
,如果来源不为空,那么您已经发现了正在发生的事情,看起来您的类 - &gt; myfun()在调用时会输出其内容。
您可能需要完成大量的代码整理和重新安排,这超出了当前问题的范围。但重要的是,改变
function myFun() {
?>
output text
<?php
}
使用以下语法更多内容:
function myFun(){
$output = "
output text
";
}
这意味着您可以控制数据的显示时间和方式,以便稍后在PHP文件中更接近它:
if (true || false ){
header("Location:some.php");
exit;
}
else {
print $output;
}
这是输出缓冲的粗略形式:What is output buffering?
此外:
检查您的.htaccess
和其他重定向会导致/index.php?a=xyz将自身重定向到/index.php或以其他方式移除查询字符串,如Splash58所述。
此外:
避免为您的变量命名$this
,尤其是在类方法上下文中,What does the variable $this mean in PHP?
答案 1 :(得分:0)