Skip to content
Advertisement

PHP: CURL fails to save cookie

I am using PHP from one host to connect to the API of another on the same subnet, which requires cookies to remember the login. It gives me a WARNING: failed to save cookies in /var/includes/cookie. I set up a short test script for myself as follows:

JavaScript

The verbose log file gives me this output:

JavaScript

The contents of $output itself is correct; it gives me the OK signal from the API that the login was successful. However subsequent requests give me signals that I am not logged in, certainly due to the cookie not being saved and thus not read.

I have tried:

  • Using a path inside the served web directory for the cookie.
  • touch /var/includes/cookie and set owner and group to apache with read/write permissions.
  • Set 777 permissions on the file.
  • sudo -u apache bash to get a terminal and attempt to modify the file – this works, so surely file permissions are okay?

Any pointers where I should look? Could SELinux affect this? My issue looks similar to this one.

CentOS 8, PHP 7.2.11, libcurl 7.61.1

Advertisement

Answer

So turns out it was SELinux having a go. Feels a bit silly to mention the solution in the problem, but I know I’m not the first one to trip over SELinux.

First off, to test if this is really the issue, you can temporarily toggle it off (if it is safe for you to do so).

JavaScript

If that works, I would of course suggest to put it back on and instead set a rule for the file(s) that need to be modified.

JavaScript

Since I have the file at /var/includes/cookie I would run the following command to add a persistent rule:

JavaScript
  • -a adds a record
  • -t specifies a new type, in this case as one that is writable by httpd

Once that context rule is written, it still has to be applied:

JavaScript
  • -v to get some output as to what changed

See this guide for more in-depth descriptions.

Also worth noting: I had to install the policycoreutils-python-utils package to get the semanage command on my minimal CentOS system.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement