-4

The code below shows me "111" and "222". But doesn't redirect me anywhere. What can I do now?

<?php
echo "111";
header("Location: http://www.google.com");
echo "222";
return Redirect::to('http://www.google.com'); 
echo "333";
?>
Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
divHelper11
  • 2,090
  • 3
  • 22
  • 37

3 Answers3

1

Two things ...

You need to put exit after your header("Location: ... ") Example:

header("Location: http://www.google.com");
exit;

But more importantly ... your header won't redirect if you've already written to the output buffer with echo.

bashaus
  • 1,614
  • 1
  • 17
  • 33
  • Thank you but still nothing happens at all – divHelper11 Nov 02 '15 at 14:03
  • 1
    Be aware that you cannot have ANY output before you use header(). So no echo, var_dump, no or whatever above your php tag, no includes of headers, nothing. – Ivar Nov 02 '15 at 14:04
0

You can't have any output before the header tag is set, so remove the echo statement and exit the script after the header:

header("Location: http://www.google.com");
exit;
Ashley
  • 1,459
  • 2
  • 12
  • 25
-1

It's not working because you've already sent data to the client (the echo). Either send your headers before you echo anything, or buffer it like this:

ob_start();
echo "111"; //Note: You're missing a semicolon here
header("Location: http://www.google.com");
echo "222";
$obj = Redirect::to('http://www.google.com'); 
echo "333";
ob_flush();
return $obj;

To Continue Reading... Hope this helps!

Jujunol
  • 457
  • 5
  • 18