user707165
user707165

Reputation:

PHP header not redirecting

I've been trying to forward a url after checking two initial conditions. 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.

This is my CSV file contents

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

Where am I going wrong ?

Upvotes: 1

Views: 427

Answers (3)

Lavy John
Lavy John

Reputation: 59

You can try with output buffering on

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

Upvotes: 1

sep
sep

Reputation: 186

You should also call exit() or die() after the header function call.

see redirect examples here https://www.php.net/manual/en/function.header.php

Upvotes: 2

Andrew Curioso
Andrew Curioso

Reputation: 2191

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.

Upvotes: 4

Related Questions