简体   繁体   English

PHP标头未重定向

[英]PHP header not redirecting

I've been trying to forward a url after checking two initial conditions. 检查两个初始条件后,我一直在尝试转发url。 It's a simple bit of code. 这是一段简单的代码。 And what I am trying to achieve is check two initial conditions that will be loaded from a CSV file then if the conditions meet I want to forward the user to a different page. 我想要实现的是检查将从CSV文件加载的两个初始条件,然后如果条件满足,我想将用户转发到其他页面。

This is my CSV file contents 这是我的CSV文件内容

katz,26.06.2011,http://www.google.com


<?php
error_reporting(E_ALL|E_STRICT);
ini_set("display_errors", "On");
$name_value=$_GET['query'];
$fh = fopen('db.csv', 'r');
$now = date("d.m.Y"); 
$data=fgetcsv($fh); 
$name=$data[0];
$date=$data[1];
$url=$data[2];
if($name_value == $name AND $date>=$now)
{
   header("Location: $url");
}
else
  {
   echo("not successful<br>");
  }
echo "name1 is $name_value<br>";
    echo "name2 is $name<br>";
    echo "date is $date<br>";
    echo "now is $now<br>";
    exit;
?>

I am getting this warning 我收到此警告

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/x/client_authorized.php:5) in /Applications/XAMPP/xamppfiles/htdocs/x/client_authorized.php on line 17 警告:无法修改标头信息-第17行的/Applications/XAMPP/xamppfiles/htdocs/x/client_authorized.php中已发送的标头(输出始于/Applications/XAMPP/xamppfiles/htdocs/x/client_authorized.php:5)。

Where am I going wrong ? 我要去哪里错了?

You cannot send headers after the output is already started (the headers have to be the first things out the door). 输出开始后,您将无法发送标头(标头必须是头等大事)。

Make sure you don't have anything (including whitespace -- like new lines, tabs, and space) before your opening <?php . 在打开<?php之前,请确保没有任何内容(包括空格-如换行,制表符和空格)。

You should also call exit() or die() after the header function call. 您还应该在头函数调用之后调用exit()或die()。

see redirect examples here http://us2.php.net/manual/en/function.header.php 在此处查看重定向示例http://us2.php.net/manual/zh/function.header.php

You can try with output buffering on 您可以尝试使用输出缓冲

$ <?php
$ ob_start();
$ --------
$ --------
$ ob_end_flush();
$ ?>

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

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