使用 php cUrl 发送会话变量

发布于 2024-12-06 01:02:41 字数 847 浏览 0 评论 0原文

我正在尝试在我的应用程序内的脚本之间发送数据。

问题是会话 ID 没有响应。

脚本 1 是...

 <?php 
      session_start();

      $_SESSION['id'] = 1;

      $data = "data to be sent to script";

      $ch = curl_init("http:.../myscript.php");

      $nvp = "&data=$data";

      curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

      echo curl_exec($ch);

      ?>

myScript.php 是...

      <?php

      session_start();          

      $id = $_SESSION['id'];

      $data = $_POST['data'];

      $result = function idData($id, $data); // returns matching results.


      echo "Session Id = $id <br />";


      echo "Id result = $result <br />";

      ?>

但是 myScript.php 无法正常访问会话数据。

有解决办法吗?可能的原因是什么?

谢谢

Am trying to send data between scripts within my application.

Problem is session id is not responding.

Script 1 is...

 <?php 
      session_start();

      $_SESSION['id'] = 1;

      $data = "data to be sent to script";

      $ch = curl_init("http:.../myscript.php");

      $nvp = "&data=$data";

      curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

      echo curl_exec($ch);

      ?>

myScript.php is...

      <?php

      session_start();          

      $id = $_SESSION['id'];

      $data = $_POST['data'];

      $result = function idData($id, $data); // returns matching results.


      echo "Session Id = $id <br />";


      echo "Id result = $result <br />";

      ?>

However myScript.php is not able to access the session data as per normal.

Is there a work around for this? What could the possible cause be?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧幽蓝 2024-12-13 01:02:42

我相信您正在寻找 CURLOPT_COOKIE

curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());

I believe you're looking for CURLOPT_COOKIE

curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
巡山小妖精 2024-12-13 01:02:42

在脚本 1 中,如果您自己跟踪响应中的会话 ID,则可以使用 CURLOPT_COOKIE

我认为如果脚本 1 将向 myscript.php 发出多个请求来创建会话,则您不需要或不希望在脚本 1 中使用 session_start 。

在脚本 1 中使用它:

curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); // set cookie file to given file
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); // set same file as cookie jar

然后像平常一样提出您的请求。请求完成时,myscript.php 设置的任何 cookie 都将保存到 cookie jar 文件中,在发送请求之前将检查 cookie 文件中是否有要发送的 cookie。

您可以手动跟踪来自curl 请求的php 会话cookie,并使用CURLOPT_COOKIE

In script 1 you could use CURLOPT_COOKIE if you keep track of the session ID from the response yourself.

I don't think you need or want session_start in script 1 if it is going to be making multiple requests to myscript.php that creates a session.

Use this in script 1:

curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); // set cookie file to given file
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); // set same file as cookie jar

And then make your request as normal. Any cookies set by myscript.php will be saved to the cookie jar file when the request completes, the cookiefile will be checked for any cookies to be sent prior to sending the request.

You could manually track the php session cookie from the curl request and use CURLOPT_COOKIE as well.

寒尘 2024-12-13 01:02:42

您错过了一个使用 post 的选项参数。请添加这个,它应该可以工作:
curl_setopt($ch, CURLOPT_POST, true);

You miss one option parameter for using post. Please add this one, it should work:
curl_setopt($ch, CURLOPT_POST, true);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文