views:

174

answers:

5

hello

i have this code :

<?php
 session_start();
echo "".$_SESSION['eventnum']."";
$urlRefresh = "testremot.php";
header("Refresh: 5; URL=\"" . $urlRefresh . "\"");
  ?>

but the header doesnt work and this warning appear when i try to run this code :

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\remot\testremot.php:3) in C:\xampp\htdocs\remot\testremot.php on line 5

can you please help me ?

+1  A: 

You can not echo anything before issuing a header. If you MUST, then the only solution is to do output buffering.

Palantir
thank you .. it works
basma
+1  A: 

Ok, lets see what you have in line 3:

echo "".$_SESSION['eventnum']."";

Obviously you are generating output (echo) in this line and this line comes before you call header.
Move it below header:

session_start();
$urlRefresh = "testremot.php";
header("Refresh: 5; URL=\"" . $urlRefresh . "\"");
echo "".$_SESSION['eventnum']."";

But note (from Wikipedia):

The W3C's Web Content Accessibility Guidelines (7.4) discourage the creation of auto-refreshing pages, since most web browsers do not allow the user to disable or control the refresh rate.

Felix Kling
thank you I do what you say and it works
basma
A: 
<?php
  session_start();
  $urlRefresh = "testremot.php";
  header("Refresh: 5; URL=\"" . $urlRefresh . "\"");
  echo "".$_SESSION['eventnum']."";
?>

headers must be set before you display anything at all, there are a lot of questions like this around...

acmatos
thank you it worksI know that I have not print anything befor header but i thought this strict only for HTML codes, not php echo :)
basma
A: 

Do not use Refresh header. And do not show anything for 5 seconds. That's usability fault. Make a page vieweventnum.php and use a Location header to bring user there

Col. Shrapnel
thank you : ) ..
basma
A: 

basma, remove any whitespace before <?php or any string before php tag. this code should work properly if there are no space or other character before opening <?php

apis17
thank you, you are right but this was not the problem
basma
the code run successfully in my server if there are nothing before php. maybe there were something she missing to paste here at line 3. i mean actual code.
apis17