使用 php cUrl 发送会话变量 [英] send session var with php cUrl

查看:54
本文介绍了使用 php cUrl 发送会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Am trying to send data between scripts within my application.

问题是会话 ID 没有响应.

Problem is session id is not responding.

脚本 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 是...

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 />";

      ?>

然而,myScript.php 无法正常访问会话数据.

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?

谢谢

推荐答案

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

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

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

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.

在脚本 1 中使用它:

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

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

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.

您可以从 curl 请求手动跟踪 php 会话 cookie,也可以使用 CURLOPT_COOKIE.

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

这篇关于使用 php cUrl 发送会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆