1

This might sound stupid, but I'm trying to make a client using PHP (with curl) and Ajax.

I'm facing a problem.

I've an index page with buttons sending requests to a server using Curl. I would like to be able to send multiple request from this index page by clicking different buttons.

The ajax part is working so I can call my php pages from my buttons. My issue is, I can't find a way to "reuse" my curl connection for subsequent requests. Every informations I send are forgot between each requests. I stored my connection like this :

//Initialisation
$ch = curl_init();

//Je stocke la connexion dans un pointeur pour stockage dans variable session.
$_SESSION['connexion'] = $ch;

this is done in the first requested ajax. Then in the second, when I call it, it becomes Null. Have you any idea of I could achieve this?

Brian
  • 3,850
  • 3
  • 21
  • 37
Alann S.
  • 125
  • 12

1 Answers1

1

You cannot "reuse" your cURL connection this way.

But the reason you're losing this information is that your cURL call is probably not sending session cookies. You need to save this information in the local session:

  • WEBPAGE (with buttons), has session W1 with id SESS-001-SITE1 saved as a cookie

  • the button calls the PHP script, which with session_start() can access W1 session

  • the PHP script calls another PHP script P2 (where, it's not so important)

  • P2 sets a cookie of its own, with id SESS-002-SITE2, mapping a session called W2

  • P2 also sends some interesting output

  • P1 receives this output and sends it, I suppose, to WEBPAGE via AJAX

    • P1 stores the cookie for W2, which it received from P2, into its own session W1
  • WEBPAGE makes another call

  • P1 receives the call, retrieves cookie for W2 from session, loads it into cURL

  • P1 connects to P2 via cURL and sends other information along with the cookie

  • P2 can now recover session W2 and respond appropriately

You can try and adapt another SO answer using file-based cookie jars .

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • Thanks for your help. My english isn't perfect, but I assume your answers acts in this order : Webpage is linked to P1 which is linked to P2. But in fact my Webpage is linked to P1 (logger), and also to P2 (sending data once I'm connected thx to P1) So if I understand well, I can make multiple call to curl_init (TO P1, P2, incoming P3 and more...), if I can save my datas in a cookie, it'll act like I've no interruption in my connexion ? Thx for your answers. – Alann S. Oct 27 '14 at 11:07
  • Maybe it would be best if you could supply a small pseudo-code, to clarify the architecture. – LSerni Oct 27 '14 at 12:49