html - Sharing session data across domains with PHP

921

My user is logged in atsite_url.com on a CMS, however I am now developing a mobile version of the site, outside the CMS on a subdomainm.site_url.com and would like the session to be carried over.

I have included a file with all the CMS' functions like this:

<?php include_once'/home/flyeurov/public_html/core/codon.config.php';?>

I can login fine on the mobile site:

<input type="hidden" name="mobileVersion" value="True">
<input type="hidden" name="redir" value="" />
<input type="hidden" name="action" value="login" />
<input class="login-btn" type="submit" name="submit" value="Log In" />

The above is processed bylogin.php. However, CMS login redirects it to CMS' page, and I need the mobile version to redirect to a page in /mobile directory wherem.site_url.com subdomain points. Hence, inlogin.php (not to be confused withmobile/login.php - mainlogin.php is for the CMS). I did this, and it redirects tomobile/crew_center.php - a page it should redirect to on a mobile version.

if (isset($_POST['mobileVersion'])) {
                header('Location: http://m.site_url.com/crew_center.php');
            } else {
                $this->post->redir = str_replace('index.php/', '', $this->post->redir);
                header('Location: '.url('/'.$this->post->redir));
            }

Maybe the above code has something to do with my problem.

My problem is that the session from the main CMS is not carried over to the mobile site. Inmobile/index.php I have this statement, which does not work. When logged in, the user should seecrew_center.php as it would be pointless to display the login form. At the moment, when the user is logged in on the CMS, the login page still appears on the mobile version.

However, if I drop the subdomain in the browser and typesite_url/mobile it will open the right pagecrew_center.php.

<?php include_once'/home/flyeurov/public_html/core/codon.config.php';

session_set_cookie_params(0, '/', 'site_url.com');
session_start();

if(Auth::LoggedIn())
{
    header("Location: crew_center.php");
} 
else
{
    header("Location: login.php");
}

?>

How can I fix this, and how can I share session data from a main domain to a subdomain with the use of PHP?


codon.config.php - http://pastebin.com/BWcbddGG

312

Answer

Solution:

You need to set the cookie's domain accordingly - add a. to your domain name:

session_set_cookie_params(0, '/', '.site_url.com');

This will share your cookie across all of your sub domains and the second level domain.

People are also looking for solutions to the problem: Using PHP to output MySQL query as SQLite

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.