php 发送不带表单的 POST 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3780912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:05:00  来源:igfitidea点击:

Sending POST data without form

phppostdata-transfer

提问by Samuel

Can i send for example a string or another piece of information to another .php file without it being exposed [thus not by GET but by POST conform to what i know] without using a form?

例如,我可以将一个字符串或另一条信息发送到另一个 .php 文件而不暴露它[因此不是通过 GET 而是通过 POST 符合我所知道的] 而不使用表单吗?

采纳答案by Stephen Holiday

If you don't want your data to be seen by the user, use a PHP session.

如果您不希望用户看到您的数据,请使用 PHP 会话。

Data in a post request is still accessible (and manipulable) by the user.

用户仍然可以访问(和操作)发布请求中的数据。

Checkout this tutorialon PHP Sessions.

在 PHP Sessions 上查看本教程

回答by Darin Dimitrov

You could use AJAX to send a POST request if you don't want forms.

如果不需要表单,可以使用 AJAX 发送 POST 请求。

Using jquery $.postmethod it is pretty simple:

使用 jquery $.post方法非常简单:

$.post('/foo.php', { key1: 'value1', key2: 'value2' }, function(result) {
    alert('successfully posted key1=value1&key2=value2 to foo.php');
});

回答by Gregor

Send your data with SESSION rather than post.

使用 SESSION 发送您的数据,而不是发布。

session_start();
$_SESSION['foo'] = "bar";

On the page where you recieve the request, if you absolutely need POST data (some weird logic), you can do this somwhere at the beginning:

在您收到请求的页面上,如果您绝对需要 POST 数据(一些奇怪的逻辑),您可以在开始的某个地方执行此操作:

$_POST['foo'] = $_SESSION['foo'];

The post data will be valid just the same as if it was sent with POST.

发布数据将与使用 POST 发送的数据一样有效。

Then destroy the session (or just unset the fields if you need the session for other purposes).

然后销毁会话(如果您需要会话用于其他目的,则只需取消设置字段)。

It is important to destroy a session or unset the fields, because unlike POST, SESSION will remain valid until you explicitely destroy it or until the end of browser session. If you don't do it, you can observe some strange results. For example: you use sesson for filtering some data. The user switches the filter on and gets filtered data. After a while, he returns to the page and expects the filter to be reset, but it's not: he still sees filtered data.

销毁会话或取消设置字段很重要,因为与 POST 不同,SESSION 将保持有效,直到您明确销毁它或直到浏览器会话结束。如果你不这样做,你会观察到一些奇怪的结果。例如:您使用 sesson 过滤一些数据。用户打开过滤器并获取过滤后的数据。过了一会儿,他返回到页面并期望过滤器被重置,但事实并非如此:他仍然看到过滤后的数据。

回答by wpcoder

Simply use: file_get_contents()

只需使用: file_get_contents()

// building array of variables
$content = http_build_query(array(
            'username' => 'value',
            'password' => 'value'
            ));
// creating the context change POST to GET if that is relevant 
$context = stream_context_create(array(
            'http' => array(
                'method' => 'POST',
                'content' => $content, )));

$result = file_get_contents('http://www.example.com/page.php', null, $context);
//dumping the reuslt
var_dump($result);

Reference: my answer to a similar question:

参考:我对类似问题的回答:

回答by Andreas Linden

have a look at the php documentation for theese functions you can send post reqeust using them.

查看这些函数的 php 文档,您可以使用它们发送 post reqeust。

fsockopen()
fputs()

or simply use a class like Zend_Http_Clientwhich is also based on socket-conenctions.

或者简单地使用Zend_Http_Client这样的类,它也基于套接字连接。

also found a neat exampleusing google...

还使用谷歌找到了一个简洁的例子......

回答by user11713698

function redir(data) {
  document.getElementById('redirect').innerHTML = '<form style="display:none;" position="absolute" method="post" action="location.php"><input id="redirbtn" type="submit" name="value" value=' + data + '></form>';
  document.getElementById('redirbtn').click();
}
<button onclick="redir('dataToBeSent');">Next Page</button>
<div id="redirect"></div>

You can use this method which creates a new hidden form whose "data" is sent by "post" to "location.php" when a button[Next Page] is clicked.

您可以使用此方法创建一个新的隐藏表单,当单击按钮 [Next Page] 时,该表单的“数据”通过“post”发送到“location.php”。