A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::open($save_path, $name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 132

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 290

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::read($session_id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 164

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::write($session_id, $session_data) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 233

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::destroy($session_id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 313

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 354

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 296

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Session cannot be started after headers have already been sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

Logo
Code Ranks ×

PHP cURL Part 6(Cookies saving and sending)

10/04/2019  .   3 minutes, 34 seconds to read  .   Posted by Admin
#php #curl #phpcurl #learnphp #phpframework #phptutorial #phpcode

So far, we've learned the following features of PHP cURL in this series of articles.

  1. PHP cURL Basics
  2. Getting Contents of any URL
  3. Downloading File from URL
  4. Checking URL existence
  5. Form Submission
  6. Authentication

In this article, we'll learn how to perform save & send cookies using PHP cURL. Sometimes websites use cookies to save user-specific information and then use those saved cookies to load resources for users. So, in this case, we can't access website resources directly. We first need to get cookies from that site and then we'll send those cookies in our upcoming cURL requests. Following is a very basic example for cookies.

$cookie_jar = tempnam('/tmp','coo');

// getting cookies
$curl = curl_init('http://httpbin.org/cookies/set?site=coderanks');
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);

//sending cookies
$curl = curl_init('http://httpbin.org/cookies');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
echo '<pre>',$response;

Response:

{
  "cookies": {
    "site": "coderanks"
  }
}

Explanation:

In the above script, we've performed the following three steps.

  1. Creating a temporary file to save cookies
  2. Getting cookies and saving in the file(created in the first step)
  3. Using cookies(saved in the second step) for a new curl request. 

Let's discuss each step in details with code.

Creating a temporary file to save cookies

We need to save cookies somewhere for future use, so for this, we're going to create a temporary file. This step can be with the following piece of code.

$cookie_jar = tempnam('/tmp','coo');
tempnam is a built-in PHP function that is used to create a unique empty file. Its first parameter is file path(default is System's directory) and the second parameter is the prefix of the file. So it will create a file in C:\Windows\Temp with a unique name prefixed with coo and will return its full path. Here is an example file created by tempname.
C:\Windows\Temp\coo1863.tmp

Getting cookies and saving in the file

Temporary file created in the first step will be empty. Now, in this step, we'll get cookies from URL and will save them in that file. Following piece of code will help us to do that.

// getting cookies
$curl = curl_init('http://httpbin.org/cookies/set?site=coderanks');
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);

Following options are already discussed in previous parts. Here is a simple detail for both options.

  1. CURLOPT_RETURNTRANSFER: used to direct cURL to not display response(default) but return after execution.
  2. CURLOPT_SSL_VERIFYPEER: used to enable or disable SSL.

CURLOPT_COOKIEJAR

This option is used to set the cookies file, which will be used by the cURL to save automatically parsed cookies.

So after executing the above script, there will be some text in that file, similar to below one.

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

httpbin.org	FALSE	/	FALSE	0	site	coderanks

Using cookies for a curl request

Now we've cookies saved in a file, we can use that file to send cookies for any cURL request. here is the script for sending cookies.

//sending cookies
$curl = curl_init('http://httpbin.org/cookies');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
echo '<pre>',$response;

CURLOPT_COOKIEFILE is used to set the cookies file to attach cookies in the cURL request. Here is the response to this request.

{
  "cookies": {
    "site": "coderanks"
  }
}

Note: Normal sites don't return cookies when you send them. We've used Httpbin.org for the testing purpose of this article to show you that how actually cookies saved and passed. In the real-life example, you'll pass cookies and get the site resources after executing, not cookies.

That's all for this part of the article.

Thanks for reading.