how to share a session with other websites(subdomains) ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolatt
    New Member
    • May 2009
    • 9

    how to share a session with other websites(subdomains) ?

    hi all,

    I want to use session for storing username & password.
    I will have a main page mainpage.mydoma in.com which will save username+passwo rd in a session on the server.

    Now when i access email.mydomain. com i want it to retrieve the username+passwo rd from the session previously created by mainpage.mydoma in.com.

    The web applications are hosted on the same machine using apache virtual hosts.
    Someone plz advise...

    thanks
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    If you want to be able to pass session data between subdomains you need to add/modify the following to the php.ini file.

    session.cookie_ domain = .mydomain.com

    If you are unable to modify the the php.ini file you can add the following before the session.start() function on any page which creates the session cookie.

    ini_set("sessio n.cookie_domain ", ".mydomain.com" );

    Comment

    • coolatt
      New Member
      • May 2009
      • 9

      #3
      thanks for replying.

      but how do i make email.mydomain. com access the data stored in the session created by mainpage.mydoma in.com ?

      I want the mainpage domain to store username+passwo rd.
      in session_test.ph p i put:
      Code:
      <?php
      session_start();
      $_SESSION['userName'] ="MyUserName";
      $_SESSION['password'] ="MyPassword";
      
      print "Session Test";
      
      ?>
      then in session_show.ph p i put :
      Code:
      <?php
      session_start();
      if(isset($_SESSION['userName']))
      {
        print "Session id: ".session_id()."<br>";
        print "Your session username: ".$_SESSION['userName']. "<br>";
        print "Your session password: ".$_SESSION['password']."<br>";
      }
      else
      {
        print "Session does not exist";
      }
      ?>
      when i run the above test i get the session id.
      Last edited by Atli; Mar 31 '10, 10:50 AM. Reason: Changed [quote] tags to [code] tags.

      Comment

      • chathura86
        New Member
        • May 2007
        • 227

        #4
        just do one of the step which i have mentioned above

        after that you can use it as it is,
        i mean set $_SESSION['foo'] = "bar"; in one domain
        and you can get it from the other domain
        by $_SESSION['foo']

        no other modifications are required

        Regards

        Comment

        • coolatt
          New Member
          • May 2009
          • 9

          #5
          both email.mydomain. com & mainpage.mydoma in.com are already using sessions.

          I tried to put the following code in email.mydomain. com but it didnt print theh username & password:

          Code:
          if(isset($_SESSION['userName']))
          {
            print "Session id: ".session_id()."<br>";
            print "Your session username: ".$_SESSION['userName']. "<br>";
            print "Your session password: ".$_SESSION['password']."<br>";
          }
          
          else
          {
            print "I cannot retrive data from another domain's session";
          }
          Is it because email.mydomain. com is already using session ??
          Last edited by Atli; Mar 31 '10, 10:50 AM. Reason: Changed [quote] tags to [code] tags.

          Comment

          • chathura86
            New Member
            • May 2007
            • 227

            #6
            which method you used ?

            editing the php.ini or setting it on the php code?

            Regards

            Comment

            • coolatt
              New Member
              • May 2009
              • 9

              #7
              editing the php.ini
              /////////////////////////

              Comment

              • chathura86
                New Member
                • May 2007
                • 227

                #8
                then the session should be shared on the sub domains also. but remember to restart apache after changing the php.ini

                Regards

                Comment

                • coolatt
                  New Member
                  • May 2009
                  • 9

                  #9
                  yes i had restarted apache. any other steps i have to follow to make it work ?
                  plz advise on this.

                  Comment

                  • coolatt
                    New Member
                    • May 2009
                    • 9

                    #10
                    it does not work when both domains implement their own session management :-(

                    Comment

                    Working...