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

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

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了