Home > Back-end >  Starting new session on every curl request
Starting new session on every curl request

Time:07-18

My question is about SESSIONS in PHP.

I am using the following code to start a new session when user opens my website:

<?php session_start(); ?>

What I want to know is, suppose a script sends "two" CURL request to my website, then will they get a new session each time?

Or will PHP recognize the "second" CURL request from same server, and open the previous session which PHP allotted to this request maker?

Suppose I want a new session started on every CURL request, then is my current PHP code session_start() okay?

Suppose I want an older session to be resumed, on every CURL request, then how should I do that while starting a session?

CodePudding user response:

PHP uses cookies to manage sessions. In order to use cookies with curl you need to enable a cookie jar

For example:

curl --cookie-jar cookies.txt  http://www.example.com

Note: cookies.txt must be placed somewhere your instance of curl can read and write to it. Using this format on each invocation of curl should allow you to use the same PHP session each time.

Without the cookie-jar argument, curl won't persist cookies between sessions. PHP won't see a cookie it recognises, so it will start a new session each time.

More details on the cURL page: https://curl.se/docs/manpage.html

  •  Tags:  
  • php
  • Related