Solved

How to Share Session Between Two PHP web codeigniter apps

Posted on 2010-11-28
3
2,419 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
Comment Utility
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
Comment Utility
Great

Thanks
0
 
LVL 108

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

763 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

13 Experts available now in Live!

Get 1:1 Help Now