-2

假设我有一个index.php

<?php
    $currentUser = "Paedow";
?>

我有一个user.php

<?php
    printf($_POST["currentUser"]);
?>

如何在调用时将变量提交$currentUseruser.php ?

我试过这段代码,但这只会提交数据而不调用/查看页面:

function PostToHost($host, $path, $referer, $data_to_send) {
  $fp = fsockopen($host, 80);
  fputs($fp, "POST $path HTTP/1.1\r\n");
  fputs($fp, "Host: $host\r\n");
  fputs($fp, "Referer: $referer\r\n");
  fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  fputs($fp, "Content-length: ". strlen($data_to_send) ."\r\n");
  fputs($fp, "Connection: close\r\n\r\n");
  fputs($fp, $data_to_send);
  while(!feof($fp)) {
      $res .= fgets($fp, 128);
  }
  fclose($fp);

  return $res;
}
4

3 回答 3

3

选项1

使用将发布您的数据、返回输出的客户端 javascript,以便您可以随心所欲地使用它。参见jQuery.post

选项 2

使用服务器端(就像您已经使用的东西一样),而不是使用 cURL 来发布您的值。见卷曲

于 2012-11-22T17:41:50.687 回答
0

的内容$data_to_send需要如下所示:

currentUser=Paedow

这就是表单 urlencoded 数据的样子。

However, can I suggest a different tack: it may be easier for you to use cURL to do a HTTP POST instead like so:

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt($curl, CURLOPT_URL, $endpoint); 
$response = curl_exec($curl);
curl_close($curl);
print_r($response);

(Keep in mind, I haven't tested that and just typed it in off the top of my head.)

于 2012-11-22T17:44:29.023 回答
0

It seems it isn´t possible as I like; so I´m using Cookies.
Another option would be to use a Session.

于 2012-12-02T16:08:51.810 回答