?
Solved

How can I access user session from main domain to sub domain?

Posted on 2014-08-17
8
Medium Priority
?
506 Views
Last Modified: 2014-10-28
I have a main website like www.test.com and sub domain as www.blog.test.com   ( this source also part of main source)

Project source is

/Source  ---> pointed to www.test.com  (php codeigniter, mysql site)
-- about
-- pages
-- blog (wordpress)  ---> pointed to www.blog.test.com (wordpress)
-- images
-- images

Open in new window


How do I access/share the session from www.test.com to www.blog.test.com sub domain?

If I access like this www.test.com/blog .. the session works fine but not working www.blog.test.com/session?
0
Comment
Question by:Loganathan Natarajan
  • 4
  • 3
8 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 total points
ID: 40267051
You have to set the cookie params to apply to the domain and all subdomains.  This is in accordance with standard cookie operations and protocols.  http://us2.php.net/manual/en/function.session-set-cookie-params.php   Read the note about domain.
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 40267189
Is there any other way with out Cookie? Because cookie may be disabled know?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40267232
Cookies are almost Always used.  The other way is to put the session_id in a querystring in the URL for each page so it gets passed from page to page.  If sessions are working and you are not seeing the session id in the address bar, then cookies are being used.  http://us2.php.net/manual/en/session.idpassing.php
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40267234
Note that anything you do that alters the session cookie must be done on Each and Every page in the session.
0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 40267439
Thanks Dave Baldwin. Let me workaround.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1600 total points
ID: 40267470
Here is the demonstration script that shows how it is done.

<?php // demo/session_cookie_domain.php
/**
 * QUESTION: WHEN CLIENTS VISIT MY SITE SOMETIMES THEY USE www.mysite.org
 * BUT SOMETIMES THEY USE mysite.org WITHOUT THE WWW.  HOW CAN I HANDLE
 * THE SESSION ISSUES THAT ARISE FROM THIS?
 *
 * ANSWER: ONE WAY IS TO REWRITE THE URL TO REMOVE THE SUBDOMAIN IF IT
 * IS WWW.  FOR EXAMPLE:
 *
 *     Options +FollowSymlinks
 *     RewriteEngine on
 *     RewriteCond %{http_host} ^www\.example\.org [NC]
 *     RewriteRule ^(.*)$ http://example.org/$1 [R=301,NC]
 *
 * ANOTHER WAY IS TO MODIFY THE SESSION COOKIE SO IT WORKS ACROSS ALL OF
 * YOUR SUBDOMAINS.  YOUR CHOICE WILL LARGELY DEPEND ON THE WAY YOU WANT
 * TO HANDLE OTHER SUBDOMAINS (OTHER THAN WWW).
 */

// DEMONSTRATE HOW TO START SESSIONS THAT WORK IN DIFFERENT SUBDOMAINS PHP 5.2+
error_reporting(E_ALL);


// MAKE THE SESSION COOKIE AVAILABLE TO ALL SUBDOMAINS
// MAKE A DOMAIN NAME THAT OMITS WWW OR OTHER SUBDOMAINS
// BREAK THE HOST NAME APART AT THE DOTS
$x = explode('.', strtolower($_SERVER["HTTP_HOST"]));
$y = count($x);
// POSSIBLY 'localhost'
if ($y == 1)
{
    $host = $x[0];
}
// MAYBE SOMETHING LIKE 'www2.atf70.whitehouse.gov'
else
{
    // USE A DOT PLUS THE LAST TWO POSITIONS TO MAKE THE HOST DOMAIN NAME
    $host
    = '.'
    . $x[$y-2]
    . '.'
    . $x[$y-1]
    ;
}

// START THE SESSION AND SET THE COOKIE FOR ALL SUBDOMAINS
$sess_name = session_name();
if (session_start())
{
    // MAN PAGE http://php.net/manual/en/function.setcookie.php
    setcookie
     ( $sess_name
     , session_id()
     , NULL                // THIS IS WHERE YOU CAN SET THE TIME
     , DIRECTORY_SEPARATOR
     , $host
     , FALSE
     , TRUE
     )
     ;
}


// PROVE THAT THE COOKIE WORKS IN MULTIPLE DOMAINS
// LOAD UP SOME INFORMATION TO SHOW SESSION CONTENTS
if (!isset($_SESSION["count"])) $_SESSION["count"] = 0;
$_SESSION["count"] ++;


// STRIP OFF THE DOT THAT WAS NEEDED FOR SETCOOKIE
$gost = ltrim($host,'.');
$dmn_link = 'http://'    . $gost . $_SERVER['REQUEST_URI'];
$www_link = 'http://www' . $host . $_SERVER['REQUEST_URI'];

// PUT UP TWO LINKS WITH DIFFERENT SUBDOMAINS
echo "<br/>Click these links to the _SESSION and _COOKIE data" . PHP_EOL;
echo "<br/><a href=\"$www_link\">$www_link</a>" . PHP_EOL;
echo "<br/><a href=\"$dmn_link\">$dmn_link</a>" . PHP_EOL;


// SHOW WHAT IS IN COOKIE AND IN $_SESSION
echo "<pre>";
echo "PHPSESSID: ";
var_dump($_COOKIE['PHPSESSID']);
echo PHP_EOL . PHP_EOL;
echo "SESSION: ";
var_dump($_SESSION);
echo "</pre>";

// CREATE A FORM TO UPDATE THE COOKIE
$form = <<<ENDFORM
<form method="post">
<input type="submit" value="CLICK ME" />
</form>
ENDFORM;
echo $form;

Open in new window

0
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 40267966
Thanks ray, will check
0
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 40410077
Thanks Ray.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question