SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    indio
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Auth class doesn't work between frames

    I set up the user authentication system almost directly from the PHP Anthology Vol. I & II books, and it works fine until I start using frames.

    A description of my problem follows my code snippets.

    Here is my index.php:
    PHP Code:
    <?php
    // Include Magic Quotes stripping script
    require_once('./strip_quotes.php');

    // Include MySQL class
    require_once ('./MySQL.php');

    // Include Session class
    require_once ('./Session.php');

    // Include Auth class
    require_once ('./Auth.php');

    $host='localhost';   // Hostname of MySQL server
    $dbUser='dbuser';    // Username for MySQL
    $dbPass='password';    // Password for user
    $dbName='eremdb'// Database name

    // Instantiate MySQL connection
    $db=& new MySQL($host,$dbUser,$dbPass,$dbName);

    // Instantiate the Auth class
    $auth=& new Auth($db,'login.php','eremdb secret hash');

    // For logging out
    if ( isset ( $_GET['action'] ) && $_GET['action'] == 'logout' ) {
        
    $auth->logout();
    }

    include_once(
    "frames.php");
    ?>
    And here is frames.php:
    HTML Code:
    <html>
    <head>
    <frameset cols='50%,50%'>
    <frame name='one' src='mainmenu.php'>
    <frame name='two' src='mainmenu2.php'>
    </frameset>
    </head>
    </html>
    mainmenu.php and mainmenu2.php have the same contents with 1 minor exception (to test differing session vars):
    PHP Code:
    <?php
    //--main menu
    // Include Magic Quotes stripping script
    require_once('./strip_quotes.php');

    // Include MySQL class
    require_once ('./MySQL.php');

    // Include Session class
    require_once ('./Session.php');

    // Include Auth class
    require_once ('./Auth.php');

    $host='localhost';   // Hostname of MySQL server
    $dbUser='dbuser';    // Username for MySQL
    $dbPass='password';    // Password for user
    $dbName='eremdb'// Database name

    // Instantiate MySQL connection
    $db=& new MySQL($host,$dbUser,$dbPass,$dbName);

    // Instantiate the Auth class
    $auth=& new Auth($db,'login.php','eremdb secret hash');

    $_SESSION["test"] = "test";
    print_r($_SESSION);//--debug
    ?>
    The only difference in mainmenu2.php is the 2nd to last line:
    PHP Code:
    $_SESSION["test2"] = "test2"
    When I hit index.php, the login works fine (if I use incorrect login/password, I cannot log in) and the frameset loads: In the left frame is mainmenu.php which shows the contents of $_SESSION, which include an index called "test" set to "test"; in the right frame is mainmenu2.php which shows the contents of $_SESSION, which include an index called "test2" set to "test2". What should happen is that both frames show the same $_SESSION contents (i.e.; both indices, "test" and "test2").

    It seems as though this is a timing issue, because when I refresh the main page (both frames), the left frame then contains both indices; and when I refresh again, then both frames contain both indices.

    Help! Any ideas what's happening here? Or how to fix it? Work around it? Can someone please explain to me the nunces of using sessions in PHP with frames?

    I should point out that I am experienced with PHP but new to OOP. And if you would like any info I have not provided, I'll be glad to provide it.

    Thanks in advance!

    - Mike

  2. #2
    SitePoint Member
    Join Date
    Jan 2005
    Location
    indio
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for responding.

    I tried your first suggesting (placing the db connection code in an include file. I tried it exactly as you posted it, which didn't work because you forgot to instantiate the Auth class. Then I added the instantiation in the code, in this location:

    PHP Code:
    ...
    require_once(
    "dbConn.ini");

    // Instantiate the Auth class
    $auth=& new Auth($db,'login.php','eremdb secret hash');

    // For logging out
    if ( isset ( $_GET['action'] ) && $_GET['action'] == 'logout' ) {
        
    $auth->logout();
    } else {
    ... 
    I got the same results as before. What was your theory here? How was including the db connection code going to help my problem?

    Then I tried your second suggestion (actually, I had already tried this, but I tried again). This didn't work either: After logging into index.php, mainmenu.php loads in the left frame, but in the right frame I am asked to log in again (which tells me that mainmenu2.php did not receive the session and was redirected to the login page). And when I *do* log into that frame again, it loads 2 more frames (as if this frame were _top), but then instantly reloads the login.php file (replacing both new frames - I can see them load then disappear in one quick flicker).

    I find this all confounding! I have used sessions between frames before (and used javascript to reload them), but not with OOP - or, specifically, not with this Auth class. I have followed the code and cannot determine which part is messing up these sessions-between-frames, but I think it has to do with how (or when?) the page in a frame is loaded. The sessions works fine withone one frame, but when I load another frame with a javascript (or even with a frameset) it does not.

    Thanks again, and please keep trying if you have the patience!

    - Mike

  3. #3
    SitePoint Member
    Join Date
    Jan 2005
    Location
    indio
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    William -

    I made the change you suggested to frames.php:
    PHP Code:
    <?php
    session_start
    ();
    if ((isset(
    $_SESSION["LoginOK"]) && $_SESSION["LoginOK"] == 1))
        die(
    "found SESSION LoginOK");
    ?>
    <html>
    <head>
    <frameset cols='50%,50%'>
    <frame name='one' src='mainmenu.php'>
    <frame name='two' src='about:blank'>
    </frameset>
    </head>
    </html>
    And I changed index.php to use:
    PHP Code:
    header("Location: frames.php"
    It failed - after logging in, I was immediately redirected to login.php again; i.e. - frames.php did not even receive the session. I also tried including the classes and instantiating Auth, too:
    PHP Code:
    <?php
    // Include Magic Quotes stripping script
    require_once('./strip_quotes.php');

    // Include MySQL class
    require_once ('./MySQL.php');

    // Include Session class
    require_once ('./Session.php');

    // Include Auth class
    require_once ('./Auth.php');

    require_once(
    "dbConn.ini");

    // Instantiate the Auth class
    $auth=& new Auth($db,'login.php','eremdb secret hash');

    if ((isset(
    $_SESSION["LoginOK"]) && $_SESSION["LoginOK"] == 1))
        die(
    "found SESSION LoginOK");
    ?>
    <html>
    <head>
    <frameset cols='50%,50%'>
    <frame name='one' src='mainmenu.php'>
    <frame name='two' src='about:blank'>
    </frameset>
    </head>
    </html>
    Same result.

    My included php files are almost straight from the PHP Anthology books, which I thought you had (since you were kind enough to respond). Sorry! Would you like me to attach them?

    - Mike

  4. #4
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Taipei Taiwan
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Hi Mike,

    I read your first post again, and now I understand what you want to do .. I am sorry for my suggestions before, that can't help you solve problem.

    Here is the point:
    After you login, webserver send html data (frames.php ouput) to browser, and browser parsed the html data and created two frames in the window.

    *Right frame data will be load (link to webserver) after Left frame was done.*

    It means mainmenu.php and mainmenu2.php are not executed at the same time or near time, if you want mainmenu.php show $_SESSION['test'] and $_SESSION['test2'], you must reload mainmenu.php after mainmenu2.php was done.
    _~=*=~_~=*=~_~=*=~_~=*=~_~=*=~_
    ~*~ ..WILLIAM LOST IN UNLIMITED .. ~*~
    _~=*=~_~=*=~_~=*=~_~=*=~_~=*=~_


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •