3

我知道我可以对 URL 参数使用 POST 方法来根据特定变量显示数据,我知道如何使用 GET 方法 - 但我被告知可以使用 POST 方法隐藏部分像这样的 URL。

/data.php?parameter=1234

就 URL 参数而言,这两种方法的实际区别是什么?

下面是一些根据特定链接的 id 从数据库中获取数据的代码

    <?php
//This includes the variables, adjusted within the 'config.php file' and the functions from the 'functions.php' - the config variables are adjusted prior to anything else.
require('configs/config.php');
require('configs/functions.php');

    //This is the actual interaction with the database, according to the id.
    $query = mysql_query("SELECT * FROM table WHERE id=" .$_GET['id'] . ";") or die("An error has occurred");

            //This re-directs to an error page the user preventing them from viewing the page if there are no rows with data equal to the query.
    if( mysql_num_rows($query) < 1 )
{
  header('Location: 404.php');
  exit;
}

    //Here each cell in the database is fetched and assigned a variable.
    while($row = mysql_fetch_array($query))
    {
        $id = $row['id'];
        $title = $row['title'];
        $month = $row['month'];
        $day = $row['day'];
        $photo = $row['photo'];
        $text = $row['text'];    
    }
?>

在一个单独的页面上,我根据 ID 生成指向 data.php 文件的链接,如下所示:

<a href="post.php?id=<?php echo $content['id']; ?>"><?php echo $content['title']; ?></a>

忘记了通过上面的代码可能会发生潜在的 SQL 注入,我将如何使用 POST 方法来隐藏 URL 参数,或者至少不像这样显示它们:

http://example.com/data.php?id=1
4

6 回答 6

2

为了使用 POST,您需要使用<form>标签,并且根据您提取这些 URL 的方式,使用 javascript 来提供帮助可能会更容易。这是一个基本示例:

<form method="post" action="data.php">
    <input type="hidden" name="parameter" value="1234" />
    <input type="submit" value="Go" />
</form>

Go 按钮将 POST 表单数据,现在在 data.php 中,您将能够从$_POST['parameter']. 请注意,使用 POST 时,您可能希望将 (HTTP 302) 重定向回页面,以便当用户点击后退按钮时,浏览器不会提示重新提交表单。

使用 javascript,您可以parameter在发布表单之前将输入设置为不同的值。

于 2011-10-26T17:22:59.910 回答
2

为您的表单使用方法“POST”。我有同样的问题,只是将 POST 添加到表单中删除了 URL 中的参数

<form id="abc" name="abc" action="someaction.php" method="post">
    <input type="text" id="username" name="username"/>
    <input type="password" id="password" name="password"/>
    <input type="submit" id="submit" name="submit" value="submit"/>
</form>
于 2013-06-21T12:13:57.143 回答
0

要发布值,浏览器必须使用带有 method="post" 的表单,或者使用 javascript 模拟表单。各种开发人员工具(fireug 等)可以将 GET 表单转换为 POST 表单,但通常需要一个表单。

理论上 GET 请求不应该有任何副作用,并且“应该”从请求到请求是一致的。也就是说,服务器应该返回相同的内容。在当今几乎所有事物都是动态的世界中,这可能没有什么实际的设计意义。

于 2011-10-26T17:23:14.593 回答
0

无论您使用 GET 还是 POST,参数都会出现在$_REQUEST. 关键区别在于使用 POST 允许变量不会出现在 URL 历史记录中。这会降低您不想在 URL 历史记录中显示的密码等数据的可见性。要使用 POST 而不是 GET,只需<form method="POST" ...>在文档中生成即可。

更好的是在 cookie 中存储敏感值(如用户 ID),这样它们就不会出现$_REQUEST。由于 cookie 的内容是在额外的 HTTP 请求标头中而不是在内容中提供的,因此它们通常不作为历史记录的一部分存储。

于 2011-10-26T17:23:16.373 回答
0

为了使用 POST 而不是 GET,您需要在 html 中使用 HTML 表单标签,如下所示:

<form method="POST" action="/data.php">
  <input type="hidden" name="parameter" value="1234" />
  <button type="submit">Submit</button>
</form>

提交后,您的 URL 将只是/data.php并且 parameter=1234 将在您的(隐藏的)发布缓冲区中。

有道理?

于 2011-10-26T17:23:25.867 回答
0

要进行 POST,您必须使用表单或一些 javascript/ajax 技巧。An<a>只会导致 GET 请求。

请注意,POST 请求仍然可以在 URL 中包含查询参数。拥有它们不是“正常的”,但它们是被允许的。主要区别在于对于 GET 请求(忽略 cookie),URL 是向服务器发送参数/数据的唯一方式。使用 POST,您可以同时使用 URL 和 POST 请求的正文,这是通常放置 POST 表单数据的位置。

于 2011-10-26T17:24:45.770 回答