Using a single database connection script for multiple domains

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #16
    What's your session write function look like? Are you storing session data in the database?

    You might need to create a separate instance of Database specific to the 'site' db inside of your session handling functions/class.

    Comment

    • fjm
      Contributor
      • May 2007
      • 348

      #17
      Originally posted by pbmods
      What's your session write function look like? Are you storing session data in the database?
      Yes, sessions are being stored in the database.

      [PHP] public function write($id, $data)
      {
      $time = time() + $this->life_time;
      $newid = mysql_real_esca pe_string($id);
      $newdata = mysql_real_esca pe_string($data );
      $sql = "REPLACE sessions(sessio n_data,expires, session_id) VALUES('$newdat a', '$time', '$newid')";
      $rs = $this->_LinkID->Query($sql);
      return TRUE;
      }[/PHP]
      Pbmods.. I have been at this for quite a while. I just can't figure it out. I was thinking that maybe my dbal script was not correct. Please let me know what you think.

      Thanks,

      Frank

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #18
        Where is $this->_LinkID initialized?

        Comment

        • fjm
          Contributor
          • May 2007
          • 348

          #19
          Originally posted by pbmods
          Where is $this->_LinkID initialized?
          In the constructor of sessions.php. It is being passed from the action/process script which is my default index page from the docroot.

          I posted an example of that above. Here it is again:
          This is for the sessions in the index page
          [PHP]
          require_once(". ./common/inc/db.php");
          $db = new Database('app') ;
          require_once(". ./common/inc/sessions.php");
          $sess = new SessionManager( $db); session_start() ;
          [/PHP]
          This is for the site, also in the index page:
          [PHP]
          $db = new Database('site' );
          $id = (int) $db->QuoteSmart($id );
          require_once("l ib/classes/page.php");
          $page = new Page($id,$db);
          [/PHP]

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #20
            Ah. Okay, I see what the problem is.

            In Database::Selec tDB():
            [code=php]
            public function SelectDB($dbNam e)
            {
            mysql_select_db ($dbName);
            return true;
            }
            [/code]

            Note that mysql_* methods will operate on the last-opened connection unless you specify a connection resource.

            In order to keep your connections separate, you need to do this:

            [code=php]
            public function SelectDB($dbNam e)
            {
            mysql_select_db ($dbName, $this->_LinkID);
            return true;
            }
            [/code]

            And similarly for Database::Query ().

            Comment

            • fjm
              Contributor
              • May 2007
              • 348

              #21
              Ok, I have made those changes, but the same error is appearing.

              Here is the db.php after the changes:

              [PHP] private $_LinkID;
              public $Query;
              public $Row;
              public $NRows;
              public $Record;

              public function __construct($db Name)
              {
              $this->LinkID = null;
              $this->Query = "";
              $this->Row = 0;
              $this->NRows = 0;
              $this->Record = array();
              $this->Connect('local host','cindy',' password');
              $this->SelectDB($dbNa me);
              }

              public function Connect($host,$ user,$pass)
              {
              $this->_LinkID = mysql_connect($ host,$user,$pas s);
              return $this->_LinkID;
              }

              public function SelectDB($dbNam e)
              {
              mysql_select_db ($dbName, $this->_LinkID);
              return true;
              }

              public function Query($sql)
              {
              $this->Query = mysql_query($sq l, $this->_LinkID);
              return $this->Query;
              }[/PHP]
              I was thinking it was exactly the same thing but I guess not.. Pbmods, do you think I would be better off maybe putting the two dbal scripts back the way they were before? I'm truly lost on this one.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #22
                Perhaps $db is getting overwritten? Try renaming $db in one of your files:
                [code=php]
                require_once(". ./common/inc/db.php");
                $sess_db = new Database('app') ;
                require_once(". ./common/inc/sessions.php");
                $sess = new SessionManager( $sess_db); session_start() ;
                [/code]

                Comment

                • fjm
                  Contributor
                  • May 2007
                  • 348

                  #23
                  Ok, I changed the "site" calls from $db->... to $db1->... and I am still gettint the error. I do however think you are on the right track with the db class. I just don't have the knowledge to know exactly what to change.

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #24
                    Hm.

                    Try adding this to your Database class:

                    [code=php]
                    public function Query($sql)
                    {
                    $bt = debug_backtrace ();
                    echo '<pre>', $sql, '</pre> (Called from ', $bt[0]['file'] . ':' . $bt[0]['line'], ')<br />';

                    $this->Query = mysql_query($sq l, $this->_LinkID);
                    return $this->Query;
                    }
                    [/code]

                    I think you want $bt[0]. Might be $bt[1]; I can't remember.

                    Comment

                    • fjm
                      Contributor
                      • May 2007
                      • 348

                      #25
                      ok, thanks for the help pbmods. Here is the output:

                      SELECT session_data FROM sessions WHERE session_id = '11r84vpddpvvnh 31lrn5e8m6t6' AND expires > 1215405874
                      (Called from C:\Program Files\Apache Software Foundation\Apac he2.2\htdocs\co mmon\inc\sessio ns.php:50)
                      SELECT t1.title FROM site AS t1 WHERE t1.page_id = 1
                      (Called from C:\Program Files\Apache Software Foundation\Apac he2.2\htdocs\ca detdispatch\lib \classes\page.p hp:49)
                      REPLACE sessions(sessio n_data,expires, session_id) VALUES('', '1215407315', '11r84vpddpvvnh 31lrn5e8m6t6')
                      (Called from C:\Program Files\Apache Software Foundation\Apac he2.2\htdocs\co mmon\inc\sessio ns.php:66)

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #26
                        That last one is what's causing the error?

                        Comment

                        • fjm
                          Contributor
                          • May 2007
                          • 348

                          #27
                          Yes. It is the last one.

                          Notice that the select statement above that has a linkid for "site" and the select statement (last one) uses a linkid for the "app". For some reason the db script isn't switching back from the site to the session script.

                          Comment

                          • pbmods
                            Recognized Expert Expert
                            • Apr 2007
                            • 5821

                            #28
                            Only thing that comes to mind is the use of mysql_real_esca pe_string() and possibly $db->QuoteSmart() .

                            mysql_real_esca pe_string() also takes a connection resource as its second parameter. I can't imagine that would cause the connections to go out of sync, but I guess it's worth a shot.

                            Failing that, I'd need to see the full source of the session handler and index.php to get a better idea of what's going on.

                            Comment

                            • fjm
                              Contributor
                              • May 2007
                              • 348

                              #29
                              Originally posted by pbmods
                              Only thing that comes to mind is the use of mysql_real_esca pe_string() and possibly $db->QuoteSmart() .

                              mysql_real_esca pe_string() also takes a connection resource as its second parameter. I can't imagine that would cause the connections to go out of sync, but I guess it's worth a shot.

                              Failing that, I'd need to see the full source of the session handler and index.php to get a better idea of what's going on.
                              Ok, Pbmods. I will try that and see where it takes me. It really is strange. I will let you know what happens. Thanks so much for the help!

                              Comment

                              • fjm
                                Contributor
                                • May 2007
                                • 348

                                #30
                                Originally posted by pbmods
                                You might need to create a separate instance of Database specific to the 'app' db inside of your session handling functions/class.
                                This is really weird. I created a totally seperate instance of the db inside the session class then instantiated the session in the index file. I am still getting the exact same error. How can this be??

                                I have the db instance totally seperated from the index file. Wouldn't this indicate that the problem is in fact inside the database class?

                                Comment

                                Working...