-3

I have a big issue and I can't solve it. I made a simple login script and it worked on my laptop's LAMP server. I was very happy and i uploaded it to my host, but there i couldn't log in. I solved that header() is not working after $_SESSION. My current test code without any login and everything:

 <?php
 session_start();
 $_SESSION['test'] = 10;
 header("Location: anything.php");
 ?>

And it doesn't redirect. Where is the problem?

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
MLL
  • 31
  • 9

2 Answers2

1

The problem is probably that the headers have already been send. That is, session_start sends out several HTTP headers. From http://php.net/manual/en/function.session-start.php:

This function sends out several HTTP headers depending on the configuration. See session_cache_limiter() to customize these headers.

Try turning all warnings and errors on (this should give you a warning) with error_reporting(-1);. Doing this during development is good practice anyway.

If it's not that, then you must be sending some output before the header. Although, looking at the example code you gave, that's probably not the case.

See this very nicely answered question for more information about the problem.

Community
  • 1
  • 1
Aleph
  • 1,209
  • 10
  • 19
-1

That's the strange. There must be a error before header function. Error before any header call will sent the header to the browser and your php header function will not work. You can try the javascript to redirect.

<?php
your_code.....
echo "<script type='text/javascript'>document.location='http://youdomain/anything.php';</script>";
?>
Ujjwal Manandhar
  • 2,194
  • 16
  • 20