0

I'm trying to set up a cookie in Joomla from an external website using cURL. The process flow is as below:

  1. User does an action in external.com
  2. external.com cURL a URL in joomla.com
  3. joomla.com sets the cookie
  4. When user visits joomla.com, the cookie will be in their browser and it will be used to show/hide pages.

Unfortunately, I can't seem to be able to set a persistent cookie. Here's what I did:

  1. [joomla.com] Created a custom component, with a custom controller and a custom task. Below the code on the controller:

    public function createCookie() {
        $app = JFactory::getApplication();
        $app->input->cookie->set(
            "TEST",
            "TestContent",
            time() + "3600",
            $app->get('cookie_path', '/'),
            $app->get('cookie_domain'),
            $app->isSSLConnection()
        );
    }
    
  2. [external.com] Created a cURL call to hit the task created at point 1.

     $cURLConnection = curl_init();
     curl_setopt($cURLConnection, CURLOPT_URL, 'https://joomla.com/index.php?option=com_test&task=testclass.createCookie');
     curl_setopt($cURLConnection, CURLOPT_HEADER, false);
     curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($cURLConnection, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($cURLConnection, CURLOPT_SSL_VERIFYPEER, false);
    
     $curlExec = curl_exec($cURLConnection);
     curl_close($cURLConnection);
    

If I visit the joomla.com website after the cURL call, the cookie is not there, it's like it has never been created. If I visit the page directly, the cookie gets created.

My question is: How can I create a cookie in Joomla from an external cURL call?

1

You should think of cURL as its own separate browser. It has no ties to IE, Firefox, Chrome, or whatever browser you use. Just like Firefox has its own cookies and IE has its own cookies, cURL can also be enabled with cookies, but it needs a file to store the cookie data in. This is specified with the CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR options. The COOKIEJAR option is for storing the cookies that cURL receives, and COOKIEFILE is for sending the cookie information on subsequent requests, so you can log in and then do additional, authenticated requests. Just set both options to "cookie.txt" or something like that.

curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

But again, this does not set the cookies in your IE/FF/Chrome browser. It simply allows cURL to send and receive cookies like a normal browser does.

6
  • Hi @James Shaver, thank you very much for your reply. So is there any other way of setting up a cookie in Joomla from an external application without using cURL? I thought cURL was the best option to do this.
    – Kabal
    Nov 17 '20 at 11:31
  • @Kabal, cookies are bound to the domain in the browser (XSS), so there's not much you can do with PHP to get around that.... If you're trying to create a "Single Sign-On" system, then you probably should look at a solution like OpenID. Nov 17 '20 at 14:25
  • Thanks, @James Shaver, I really appreciate your help. I'm not trying to create an SSO system, I only need those two web applications to communicate between each other, and a cookie to be set on Joomla remotely. I guess the result will be the same if I use an Ajax call, right?
    – Kabal
    Nov 18 '20 at 9:17
  • Unless both domains are on the same subdomain (i.e. site1.domain.com and site2.domain.com), then YMMV with setting HttpCookie.Domain. I would expect multiple cross-browser issues to arise from that though.
    – Grant G
    Nov 18 '20 at 9:23
  • @GrantG Right, but OP specifically said "User does an action in external.com" Nov 18 '20 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.