-4

I cant get header location to work on my server, but when i run the script at localhost, everything works fine. It seems the server does not run this line.

header('location:index.php');

Server running linux stack with php 7.2, localhost is running xampp (osx) with php 7.3.

I have several projects on the same server with header location, and those are working fine.

phingoc
  • 101
  • 1
  • 9
  • You can check your relative path of index.php – Ankur Tiwari Feb 28 '19 at 07:06
  • 3
    Are you outputting any content before this line? – Sven Feb 28 '19 at 07:09
  • Any errors in the Apache error log? – Ferrybig Feb 28 '19 at 07:12
  • Duplicate of https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Salman A Feb 28 '19 at 07:15
  • @rkeet _“Linux is case sensitive”_ - yes, when it comes to the file system; this here has absolutely nothing to do with that, it is an HTTP header. And HTTP header names are not case-sensitive, that is explicitly in the specification. https://stackoverflow.com/a/5259004/10955263 – 04FS Feb 28 '19 at 07:53
  • It would help you you actually commented/answered any of the questions/answers you've gotten – M. Eriksson Feb 28 '19 at 09:15

6 Answers6

1

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP

Linux server is case sensitive try with uppercase Location this. its better to stop script after the header location so no other script will run afterwards.

header('Location: index.php');  
exit;
  • 1
    It won't matter if you write `location` or `Location`. – M. Eriksson Feb 28 '19 at 07:21
  • +1 for "it's better to stop the script after the header location so no other script will run afterwards." - adding exit after the location header was set fixed the issue for me. Oddly enough in an environment with IIS it worked fine, but when I copied it over to apache it did not. – Eilert Hjelmeseth Jan 30 '20 at 01:59
0

Check the path of your index.php

Also you can try to add this before header.

ob_start(); then after that you can exit()

0

you can use

echo "<script> window.location.href = 'index.php'; </script>";
  • This is not a good idea. If you want to redirect a request, do it by passing a `location`-header from PHP instead, since the above requires the client to be able to parse HTML and JS (which isn't always the case). – M. Eriksson Feb 28 '19 at 09:10
0

alternative to header() you can use :

echo '<script> location.replace("http://yoursite.com/yourpage.php");</script>';
  • This is not a good idea. If you want to redirect a request, do it by passing a `location`-header from PHP instead, since the above requires the client to be able to parse HTML and JS for it to work (which isn't always the case). – M. Eriksson Feb 28 '19 at 09:12
  • assume that you are in a case where the header() function would stop functioning,and u need to implement redirection then what would you do??and javascript is the most widely used language and if programmed correctly it will execute surely – anurag pandey Feb 28 '19 at 09:18
  • _"assume that you are in a case where the header() function would stop functioning"_ - Can you give _one single example_ of when/how `header()` would stop working while the site still works? – M. Eriksson Feb 28 '19 at 09:21
  • _"and javascript is the most widely used language"_ - That might be, but that doesn't mean that all clients can parse HTML and JS. If you're building an API, most clients will probably not execute any code but just expect something like json back. – M. Eriksson Feb 28 '19 at 09:21
  • this guy maybe in a same situation due to some bug he may have created – anurag pandey Feb 28 '19 at 09:23
  • ...and that's why the OP is here, to ask how to solve it. So isn't it better to _fix_ the error than "cover it up" with less effective scripts? – M. Eriksson Feb 28 '19 at 09:25
  • i compltely agree with you... but i have answered it in worst case for him..... thats why i have written a word "alternative" clearly – anurag pandey Feb 28 '19 at 09:28
0

The solution to this question was to add ob_start() to the top of php document. Worked like a charm on Linux server and localhost (MacOs with XAMPP)

phingoc
  • 101
  • 1
  • 9
-1

header best works with absolute URL's, try using http://...../index.php rather than just the filename index.php and since it is index.php use the parent folder, and with the exit; command for example, if

     Location('http://www.slyde.com/index.php'); 
     exit;
     //should be 
     Location('http://www.slyde.com');
     exit;

and so with relative folders.

     Location('http://www.slyde.com/app/index.php');
     exit;
     //should be 
     Location('http://www.slyde.com/app');
     exit;

i hope this works..

Ande Caleb
  • 1,163
  • 1
  • 14
  • 35
  • `location()` isn't a core function. Also, when sending a location header (using `header('location: ...')`), the URL doesn't have to be absolute. – M. Eriksson Feb 28 '19 at 07:18
  • @Magnus Eriksson i've gotten very unusual errors sometimes, but doing this just normalizes it for every server i work with, and most at times it works, but thnks for the hint. duly noted. – Ande Caleb Feb 28 '19 at 07:30
  • Adding the hostname when redirecting within a site isn't necessary (and I wouldn't call it normalized). I would rather do `location: /uri`. However, that doesn't really matter since this answer still uses a non-core PHP function `Location()`. – M. Eriksson Feb 28 '19 at 09:08