duankanyi6539 2014-12-05 02:49
浏览 29

什么原因导致PHP在完成脚本之前重定向头?

On my website I use PHP files for all sorts of things, friending people, creating accounts, and more. I will have a PHP file like so:

<?php
    /*Does some sort of action to stored data on the server*/
    header('location: mysite.org');
?>

The issue is that the it goes to mysite.org before the rest of the PHP is actually updated. This causes problems because if a user, for example, posts a message, it won't appear right away, only after 1 or 2 refreshes it will. How could I fix this? I am accessing these PHP files with HTML forms sending a POST request.

  • 写回答

1条回答 默认 最新

  • douxie2029 2014-12-05 02:56
    关注

    If you want to trigger the headers after a certain 'action', include a conditional statement. For example, if you're sending POST data, you can do:

    if(isset($_POST)){
      //do stuff here
    header();
    }
    

    Since you are storing data on a database, you can call the header() after successful insertion into the database.

    if(!$mysql_query){//if it fails
    //do nothing
    }
    else{
    header();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真