0

First of all let me illustrate what I mean.

For instance if I open google.com on my browser and I login, and then I open youtube.com or gmail.com or any other Google site, I automatically get logged in based on my google.com login details.

Now I need to achieve the same thing. I have 2 sites running the same database, infact one is a sub-domain to the other. Let say mainsite.com and sub.mainsite.com now I have a hyper-link that directs a user to sub.mainsite.com from mainsite.com and I don't want the user to login again on sub.mainsite.com I just want to get the user login id from mainsite.com and automatically login the user on sub.mainsite.com. Though for now am running this on my local WAMP server. I haven't tried it online.

Now I create a page on mainsite.com called master.php which gets the logged in user and echo the id. And if I type the address to the page on my browser, it works fine. But now I want to retrieve the id from master.php on the index page of sub.mainsite.com but instead I get an error of undefined index 'user'.

I know session details are protected from other sites but I guess there should be a way out of this.

master.php

<?php
    $home = $_SERVER['DOCUMENT_ROOT];
    ini_set('session.save_path', realpath($home));
    session_start();
    $id = $_SESSION['user'];
    echo $id;
?>

sub.mainsite.com/index.php

<?php
    function getCURL($url){
        $ch = curl_init();
        $timeout = 60;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    $data = getCURL('http://mainsite.com/master.php');
    echo $data;
?>
  • 1
    https://stackoverflow.com/questions/644920/allow-php-sessions-to-carry-over-to-subdomains – Shomz Sep 03 '17 at 10:37
  • this is how SO went about the problem for different domains [Global Network Auto-Login](https://stackoverflow.blog/2010/09/11/global-network-auto-login/), it's an interesting read – Wee Zel Sep 03 '17 at 11:03
  • just make them have the same $_SESSION backend, and set the cookie domain to `Domain=.mainsite.com` (the dot after the equals makes it available on sub.mainsite.com too) – hanshenrik Sep 03 '17 at 11:56

0 Answers0