没有正确重定向头

时间:2013-10-23 05:11:17

标签: php mysql

删除有效,页面重定向但删除后的网址为http://localhost:/manage_items.php?yesdelete=23为什么会这样?它应该是manage_items.php

while ($row = $get_products->fetch()) {
  $item_id =  $row['item_id'];
  $user_id =  $row['user_id'];
  $item_name = $row['item_name'];
  $date = $row['add_date'];
  $image = $row['photopath'];
  $products .= "<br/><img src = $image><img> Item ID: $item_id UserID: $user_id NAME: 
                  $item_name Added on: $date &nbsp
                  <a href='item_edit.php?pid=$item_id'>Edit</a>&nbsp 
                  <a href='manage_items.php?deleted=$item_id'>Delete</a>";
}

//delete Item

if(isset($_GET['deleted'])) {
   echo 'delete this product?'.$_GET['deleted'].'<a 
         href="manage_items.php?yesdelete='.$_GET['deleted'].'">Yes<a/> 
         / <a href ="manage_items.php">No</a>';
   exit();
}

if(isset($_GET['yesdelete'])) {
  $deleteid = $_GET['yesdelete'];
  $sql = $db->exec("DELETE FROM item WHERE `item_id` = '$deleteid' LIMIT 1");
  $image_delete = 'file_to/$deleteid';

  if(file_exists($image_delete)) {
    unlink($image_delete);
  }
  header("Location: manage_items.php");
  exit();
}

3 个答案:

答案 0 :(得分:0)

在任何类型的输出进入浏览器才能正常工作之前必须调用

标题,即使我没有看到整个代码我看到你输出的空白字符串:

  ?>
  <?php

删除所有这些地方以及可以完成输出的其他地方 - 然后重定向将起作用

另外,我真的建议你使用正确的重定向:

function redirect($url)
{
    if (!headers_sent())
    {
        if (strtoupper($_SERVER['SERVER_PROTOCOL']) == 'HTTP/1.1')
            @header('HTTP/1.1 303 See Other', true, 303);
        else
            @header('HTTP/1.0 302 Found', true, 302);
        @header('Location: ' . $url);
        @header('Content-type: text/html');
    }

    echo '<html><head><title>Redirect</title><meta http-equiv="Refresh" content="0;URL='.htmlspecialchars($url).'"></head><body>'.
        '<a href="'.htmlspecialchars($url).'">Click here</a>'.
        '</body></html>';
    @ob_flush();
    exit();
}

答案 1 :(得分:0)

看起来已经发送了一个标题的情况。

您必须执行以下操作。

  1. 删除代码块。

    ?> <?php

  2. 在第一个<?

  3. 之前删除所有空格和换行符
  4. 删除最后一个?>或至少确保之后没有空格或换行符。
  5. 您可以使用输出缓冲ob_startob_end_flush
  6. 编辑:您还在重定向之前发送html。在这里,我重新安排了您的代码。我已将删除块移到了开头。

    <?php
    //delete Item
    
    if(isset($_GET['yesdelete']))
    {
        $deleteid = $_GET['yesdelete'];
    
        $sql = $db->exec("DELETE FROM item WHERE `item_id` = '$deleteid' LIMIT 1");
    
        $image_delete = 'file_to/$deleteid';
    
        if(file_exists($image_delete))
        {
            unlink($image_delete);
    
        }
        header("Location: manage_items.php");
        exit();
    }
    while ($row = $get_products->fetch())
    {
        $item_id =  $row['item_id'];
        $user_id =  $row['user_id'];
        $item_name = $row['item_name'];
        $date = $row['add_date'];
        $image = $row['photopath'];
        $products .= "<br/><img src = $image><img> Item ID: $item_id UserID: $user_id NAME: 
                      $item_name Added on: $date &nbsp
                      <a href='item_edit.php?pid=$item_id'>Edit</a>&nbsp 
                      <a href='manage_items.php?deleted=$item_id'>Delete</a>";
    }
    if(isset($_GET['deleted']))
    {
        echo 'delete this product?'.$_GET['deleted'].'<a 
               href="manage_items.php?yesdelete='.$_GET['deleted'].'">Yes<a/> 
               / <a href ="manage_items.php">No</a>';
        exit();
    }
    ?>
    

答案 2 :(得分:0)

这就是问题所在。它位于我页面顶部的会话块中

session_start();

if(!isset($_SESSION['id']))

{
header("Location: yackimo_register_form.php");

exit();
}
$userID = $_SESSION['id'];
echo "";<<<------------

坏习惯