Solved

How to Share Session Between Two PHP web codeigniter apps

Posted on 2010-11-28
3
2,453 Views
Last Modified: 2013-11-13
dear.

i am facing problem with open blog. in open blog i just print

My problem is this i am using openblog with my personal codeigniter website. when user login to my website and move to blog then i am unable to access session data i blog.

means session set after login of user... like ( $this->session->userdata('user_id') ). when user move to blog by clicking im unable to access $this->session->userdata('user_id') ..

Application Paths are  :

domain.com
domain.com/blog

when we login and move from domain.com to domain.com/blog then session missed...

this means session reset.

so please tell me how to change / edit open blog so this becomes good with my script.

please help me.
0
Comment
Question by:activesoftware
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34225957
This is not specific to CodeIgniter, but I can show you how to set the session cookie so it covers all sub-domains and sub-directories.
<?php // RAY_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://us.php.net/manual/en/function.setcookie.php

    setcookie($sess_name, session_id(), NULL, '/', $host, FALSE, TRUE);

}





// PROVE THAT THE COOKIE WORKS IN MULTIPLE DOMAINS

// LOAD UP SOME INFORMATION TO SHOW SESSION CONTENTS

$_SESSION["cheese"] = "Cheddar";

if (!isset($_SESSION["count"])) $_SESSION["count"] = 0;

$_SESSION["count"] ++;





// PUT UP TWO LINKS WITH DIFFERENT SUBDOMAINS

// STRIP OFF THE DOT THAT WAS NEEDED FOR SETCOOKIE

$gost = ltrim($host,'.');

$dmn_link = 'http://'    . $gost . '/RAY_dump_session.php'; // var_dump() SCRIPT

$www_link = 'http://www' . $host . '/RAY_dump_session.php';



echo "<br/><a target=\"_blank\" href=\"$www_link\">$www_link</a>" . PHP_EOL;

echo "<br/><a target=\"_blank\" href=\"$dmn_link\">$dmn_link</a>" . PHP_EOL;





// SHOW WHAT IS IN COOKIE AND IN $_SESSION

echo "<pre>";

echo "COOKIE ";

var_dump($_COOKIE);

echo PHP_EOL . PHP_EOL;

echo "SESSION ";

var_dump($_SESSION);

echo "</pre>";





?>

<form method="post">

<input type="submit" value="CLICK ME" />

</form>

Open in new window

0
 

Author Closing Comment

by:activesoftware
ID: 34226367
Great

Thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34226390
Thanks for the points - it's a great question! ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This article discusses four methods for overlaying images in a container on a web page
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now