How do I automatically login to another database from a current website in cakephp

In cakephp I can login to a website no problem.
What I need to do is login and then click a button that will send me another cakephp website with its own login.
This works fine (on my pc) but I I need to automatically login to the website without having to login again

 echo $this->Html->link( 'link!', '');
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
You use Curl and cookies.  Store the cookie on your server and reuse it to log into the other site as needed.

So in your controller, is there a specific action that initiates this?
Loganathan NatarajanLAMP DeveloperCommented:
RobOwner (Aidellio)Commented:

How does your solution cover this part?:
What I need to do is login and then click a button that will send me another cakephp website with its own login.

The server needs to make the connection to the other site, not the client
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

jagguyAuthor Commented:
Thanks for the replies
Without an example in cakephp I cant spend hours fiddling and hoping something will work.
This either can be done or it cant. If it can can someone point me to an example please.
RobOwner (Aidellio)Commented:
Of course it can be done but how I've done it may not work for your implementation.

I have a class I've called proxy that handles all the CURL calls. It maintains the cookies etc.

I'll post the code of that class soon when I'm at my laptop
RobOwner (Aidellio)Commented:

class SiteProxy {

    function __construct($session) {
        $this->base_url = ""; #trailing slash
        $this->Session = $session;

        $this->curl = curl_init();
        $session->check('non.existant'); #init the session so it has an ID
        $cookie_file_path = realpath("../cookies/") . "/site_" . $session->id();
        curl_setopt_array($this->curl, array(
            CURLOPT_COOKIEFILE => $cookie_file_path,
            CURLOPT_COOKIEJAR => $cookie_file_path,
            CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7',

    function __destruct() {

      2)Use curl_setopt($ch, CURLOPT_HEADER, 1)
      3)Grab from the header cookies like this:
      preg_match_all('|Set-Cookie: (.*);|U', $content, $results);
      $cookies = implode(';', $results[1]);
      4)Set them using curl_setopt($ch, CURLOPT_COOKIE,  $cookies);

    function fetch_url($url, $method = "GET", $data = '', $follow = true) {
        //echo 'Requesting '.$url.' with '.$method.' and parameters '.$data;
        $ch = $this->curl;
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else {
            curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // true: return contents as a string, false: output to browser
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow);

        $thePage = curl_exec($ch);
        return $thePage;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Loganathan NatarajanLAMP DeveloperCommented:
The server needs to make the connection to the other site, not the client

In the first website, i login and store the details using cookie and retrieve in second website on the first page.
RobOwner (Aidellio)Commented:
Can you show a code example?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.