【问题标题】:not redirecting properly with header没有使用标题正确重定向
【发布时间】:2013-11-01 05:33:28
【问题描述】:

删除成功,页面重定向,但删除后的url是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();
}

【问题讨论】:

  • 1. 您的代码易受 sql 注入攻击 2. 您的代码易受 XSS 攻击
  • 尝试不同的浏览器,看看是否相同。您的代码中没有任何内容表明查询字符串应该在重定向中保持不变。您还可以尝试在浏览器的 Net 开发者控制台标签 中监控网络请求/响应
  • @dhpratik 是的,我可以看到,但有什么问题?
  • @dhpratik 是的,它就在代码中
  • @mavrosxristoforos 对,代码有效。没有headers already sent 消息。确保没有空格

标签: php mysql


【解决方案1】:

必须在任何类型的输出进入浏览器之前调用header才能正常工作,即使我没有看到整个代码我看到你正在输出空白字符串:

  ?>
  <?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();
}

【讨论】:

  • OP 已经确认发生了重定向。这不是标头已发送问题
【解决方案2】:

看起来像是一个标头已经发送的案例。

您必须执行以下操作。

  1. 删除代码块。

    ?> <?php

  2. 删除第一个&lt;?之前的所有空格和换行符

  3. 删除最后一个 ?&gt; 或至少确保其后没有空格或换行符。
  4. 你可以使用输出缓冲ob_start, ob_end_flush

编辑:您还在重定向之前发送 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();
}
?>

【讨论】:

  • 我不是反对者,但你的回答没有任何用处。
  • @mavrosxristoforos 这篇文章中的第一项实际修复了错误
  • @IlyaBursov 你测试过吗?
  • @IlyaBursov 重定向有效,它没有发送标头。花一些时间阅读 cmets。我知道,因为我在第一次编辑这个问题之前就在这里(两个php代码块合并为一个)
  • @roger我的意思是你最初提出的这个问题的两个引用。
【解决方案3】:

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

session_start();

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

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

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

坏习惯

【讨论】:

  • 很高兴你找到了它。只需确保您下次确实添加了所有可疑代码即可。
猜你喜欢
  • 1970-01-01
  • 2020-02-10
  • 2018-04-14
  • 2021-12-06
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
相关资源
最近更新 更多