PHP error- Cannot modify header information

Asked By 0 points N/A Posted on -
qa-featured

Hi!
I'm just learning PHP, I made this code that will redirect automatically to index.php every time the session variable is not set or it is being destroyed by logout. I am using header function for this. It worked perfectly on my laptop, but when I had uploaded it on a free web hosting site, every time I run the code, it  will display an error on the very top of the page saying:  "Warning: Cannot modify header information – headers already sent by (output started at /home/database/admin.php:2) in /home/database/home.php  on line 2  "

I just don't get what it really means, I've been trying to transfer anywhere my code but still, it displays the error above.
What are the possible solutions for this?

Is this related to my php.ini configurations?

I appreciate all your help..

This is the code:
<?php
   if (!isset($_SESSION['username']) AND !isset($_SESSION['password'])) {
  header("Location: index.php");
   exit();
   }
 ?>
Thank you
James Field

SHARE
Answered By 0 points N/A #93874

PHP error- Cannot modify header information

qa-featured

I experienced that kind of problem when I was in college. I work with WAMP server and everything is alright. But the time that I need to upload my website to hosting site. I got also the same error.

I search the internet and Google it by there are some tries that didn’t work. I found solution from my self. I tried to put all my session variable and session codes at the top of my page. And it works well. No error again.

At this time I just still don’t know what the problem is regarding to that. What I can only share you is my solution that solved my problem regarding that.

Just put you session variable or sessions at the top of all your code.

Answered By 0 points N/A #197268

PHP error- Cannot modify header information

qa-featured

Hi,

Sometimes it happens that if you are having any echo statement in your file “Cannot modify header information – headers already sent” error displays rather than giving header("Location: index.php");.

You just cut and  paste below code in your file.

<?php

   if (!isset($_SESSION['username']) AND !isset($_SESSION['password']))

 {

echo ‘<script type=”text/javascript”>

Location.href=”index.php”;

</script>’;

   }

 ?>

Related Questions