0

我有以下代码:

<?php
    define('ENVIRONMENT', 'tests');
    $_POST['id']='AccountPagesView.a_book/45';
    $_POST['old_value']='1';
    $_POST['value']='2';
    header("Location: http://localhost/index.php/welcome/update_record");
?>

我需要在这个脚本中设置 $_POST 数组并通过 url 加载脚本。但是来自 url 的脚本告诉我 $_POST 数组为空。为什么?如何设置 $_POST 数组并通过 url 将其发送到脚本?先感谢您。

更新:我有一些必须测试的代码,并且 url 上有一些脚本"http://localhost/index.php/welcome/update_record",它使用 $_POST 数组中的值;所以,我不能改变这个脚本,我想测试它。我该怎么做?

更新2:

<?php
    //include ('\application\controllers\welcome.php');
    define('ENVIRONMENT', 'tests');
    $_POST_DATA=array();
    $_POST_DATA['id']='AccountPagesView.a_book/45';
    $_POST_DATA['old_value']='1';
    $_POST_DATA['value']='2';
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/index.php/welcome/update_record');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST_DATA);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_exec($ch);
?>
4

5 回答 5

4

你不能。重定向将始终导致通过 GET 加载目标页面。

但是,您可以使用会话来存储这些值。在两个页面上调用session_start();并使用超全局数组$_SESSION而不是$_POST.

于 2012-07-01T14:53:25.507 回答
1

我相信这是您需要在不使用 JS 的情况下将 POST 值从一个 PHP 脚本发送到另一个脚本,如果您绝对不想使用$_SESSION,尽管这是您应该使用的。

$ch = curl_init();
$data = array('id' => 'AccountPagesView.a_book/45', 'old_value' => '1', 'value' => '2',);

curl_setopt($ch, CURLOPT_URL, 'http://path-to/other.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
于 2012-07-01T16:19:46.017 回答
0

这是应该使用 $_SESSION 的主要原因之一。请参阅此其他问题以获取解释:PHP - Pass POST variables with header()?

<?php
session_start();
define('ENVIRONMENT', 'tests');
$_SESSION['id']='AccountPagesView.a_book/45';
$_SESSION['old_value']='1';
$_SESSION['value']='2';
header("Location: http://localhost/index.php/welcome/update_record");

然后在 index.php/welcome/update_record

<?php
session_start();
define('ENVIRONMENT', 'tests');
$id = $_SESSION['id'];
$old_value =  $_SESSION['old_value'];
$value = $_SESSION['value'];

//do something
于 2012-07-01T15:23:39.390 回答
0

两个答案。如果以下方法不起作用:

<?php
    define('ENVIRONMENT', 'tests');
    $_POST['id']='AccountPagesView.a_book/45';
    $_POST['old_value']='1';
    $_POST['value']='2';
    require("/index.php/welcome/update_record");
?>

(我对页面 URL 有点吃惊。)

然后:

当您坚持 POST 时(您有权要求),您可以执行以下操作:

<html>
  <head>
  </head>
  <body>
    <form action="/index.php/welcome/update_record" method="post">
      <input type="hidden" name="id" value="AccountPagesView.a_book/45">
      <input type="hidden" name="old_value" value="1">
      <input type="hidden" name="value" value="2">
      <input type="hidden" name="ENVIRONMENT" value="tests">
    </form>
    <script type="text/javascript">
       document.forms[0].submit();
    </script>
  </body>
</html>

环境的定义需要以某种方式解决。

如果目标脚本使用 $_REQUEST,即 $_POST + $_GET (io $_POST),那么您执行 HTTP GET URL:...-?id=...&old_value=1&value=2这将是最简单的解决方案。

于 2012-07-01T16:19:59.323 回答
0

$_POST只有通过方法发送请求时才存在POST,这意味着发送了表单。

你可以$_SESSION改用。

于 2012-07-01T14:53:36.277 回答