dongshou6788 2013-06-20 12:16
浏览 12
已采纳

内容执行后PHP重定向?

I'm trying to do a PHP redirect after my content has executed, however I cannot get the code to execute.

If I load this up, it redirects the page but doesn't insert anything to the database. If I comment out the header redirect line, it enters the data into my database.

How can I get the PHP to run and execute, and then redirect the page?

<?php require("gplus.php"); ?>
<?php require("database.php");?>    
<?php if(isset($personMarkup)): ?>
<?php
$checkuser = "SELECT gid FROM user WHERE gid = '$id'";
$updateuser = "UPDATE user SET name = '$name', pic = '$img' WHERE gid = '$id'";
$adduser = "INSERT INTO user (gid, name, pic) VALUES ('$id','$name','$img')";
$checkuserrlt = mysqli_query($con, $checkuser); 


if(mysqli_num_rows($checkuserrlt) > 0) {
   $result = mysqli_query($con, $updateuser) or die(mysqli_error());
} else {
     $result = mysqli_query($con, $adduser) or die(mysqli_error());
}
?>
<?php endif ?>

<?
mysqli_close($con);
?>
<?php
header( 'Location: http://google.com' );
exit();
?>
  • 写回答

1条回答 默认 最新

  • dongma0722 2013-06-20 12:20
    关注

    When you do:

    <?php endif ?>
    
    <?
    

    You are outputting content in your browser, in that case, a new line. When you do that, PHP will send the HTTP headers back to the client, and it becomes impossible to add new headers. That's why your header() function won't work.

    Open and close your PHP tags once. <?php at the beginning of your script, ?> at the end. Also you should see error messages, make sure they are enabled (in development only, you don't want to show error messages in production):

    error_reporting(-1);
    ini_set('display_errors', -1);
    

    If you had error reporting enabled, you would certainly see this error message:

    Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23

    You can read more about this error here: How to fix "Headers already sent" error in PHP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加