开发者

PHP header refresh warning

I have this code :

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

but the header doesn't 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?


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.


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


<?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...


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜