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

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?
LVL 36
Loganathan NatarajanLAMP DeveloperAsked:
Who is Participating?
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.

Dave BaldwinFixer of ProblemsCommented:
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
Loganathan NatarajanLAMP DeveloperAuthor Commented:
Is there any other way with out Cookie? Because cookie may be disabled know?
0
Dave BaldwinFixer of ProblemsCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Dave BaldwinFixer of ProblemsCommented:
Note that anything you do that alters the session cookie must be done on Each and Every page in the session.
0
Loganathan NatarajanLAMP DeveloperAuthor Commented:
Thanks Dave Baldwin. Let me workaround.
0
Ray PaseurCommented:
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

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 DeveloperAuthor Commented:
Thanks ray, will check
0
Loganathan NatarajanLAMP DeveloperAuthor Commented:
Thanks Ray.
0
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
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.