Struggling with setting Session via jquery

Discussion in 'PHP' started by stephan2307, Mar 29, 2010.

  1. #1
    Not sure if this is a js or php problem.

    I have a login form which I submit to a php page via jquery for executing. Then once the login was successful I load another page content.

    When I do a check on the login script the session get set properly.

    I do have session_start on every file.

    Any idea why this problem could be? Has it maybe something to do with the fact that the ajax files are in a subfolder ?
     
    stephan2307, Mar 29, 2010 IP
  2. meloncreative

    meloncreative Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Have you set a session_name so it is not using the correct session?

    Is the session being unset somewhere in the ajax script?

    Post some code?
     
    meloncreative, Mar 29, 2010 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    I haven't set session_name but I checked the session_id and it is the same.

    No unset is being used.

    Here is some code

    index.php:
    
    
    <?php
        session_start();
    ?>
    <html>
        <head>
            <script type="text/javascript" src="js/jquery.js"></script>
    
            <style>
            .pane { 
                height: 300px;
                width: 300px;
                float: left;
                border: 1px solid #000000;
            }
            </style
        </head>
        <body>
        <script type="text/javascript">
    
        
        function login(pane) {
            
            $.post('ajax/login.php?p='+pane,  {h: document.getElementById('host_'+pane).value, u: document.getElementById('user_'+pane).value, p: document.getElementById('pass_'+pane).value }, function(data) {
              
               loadDatabases(pane);
            });
    
        }
           
        function loadDatabases (pane) {
             $('#pane_'+pane).load('ajax/loadDatabases.php?p='+pane);
        }    
            
        function loadLoginPage(pane) {
            $('#pane_'+pane).load('ajax/loginForm.php?p='+pane);
        }    
        
        $(window).load(function () {
            loadLoginPage('1');
            loadLoginPage('2');
        });
        
        </script>
        <div id="pane_1" class="pane">
        </div>
        <div id="pane_2" class="pane">
        </div>
        </body>
    </html>
    
    
    PHP:
    ajax/login.php
    
    <?php
    
        session_start();
    
        
        $_SESSION[$_GET['p']]['host'] = $_POST['h'];
        $_SESSION[$_GET['p']]['user'] = $_POST['u'];
        $_SESSION[$_GET['p']]['pass'] = $_POST['p'];        
        
    
    ?>
    
    
    PHP:
    ajax/loadDatabases.php:
    
    <?php
    
        session_start();
    
       
        
        $link = mysql_connect($_SESSION[$_GET['p']]['host'], 
                              $_SESSION[$_GET['p']]['user'], 
                              $_SESSION[$_GET['p']]['pass']);
        
        $db_list = mysql_list_dbs($link);
    
        while ($row = mysql_fetch_object($db_list)) {
            echo $row->Database . "\n";
        }
        
    ?>
    
    PHP:
     
    stephan2307, Mar 29, 2010 IP
  4. meloncreative

    meloncreative Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    so presume it gets to the loadDatabases.php and says cannot connect?

    and in both ajax scripts you have done a print_r($_SESSION);

    should all be working. i have recently written an app that uses sessions and ajax a lot and it has no problems. admittedly the scripts are all in the same folder but it shouldnt be an issue. maybe move it into the root to discount that possibility?
     
    meloncreative, Mar 29, 2010 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    Yes it can't connect because the session variables disappear.

    I do a pirnt_r($_SESSION) after setting them and they are correctly set.
    If I do a print_r($_SESSION) before I try to connect to the db the session is empty. Looks like it's not getting set at all. I usually expect this behaviour if session_start(); isn't used but as I have it I really don't know what the problem is.

    I moved the files into the root folder and the problem is still there.

    any more ideas?
     
    stephan2307, Mar 29, 2010 IP
  6. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #6
    also just found out that if I set a session variable in the index.php that value will stay at all times. bizzare.
     
    stephan2307, Mar 29, 2010 IP
  7. meloncreative

    meloncreative Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    did you try moving them into the same folder?

    also try takin ajax out of the equation. load the scripts directly, use static variables instead of the $_POST ones

    basically just strip it all back until it works, then figure out what fixed it
     
    meloncreative, Mar 29, 2010 IP
  8. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #8
    yeah I did move the files into same table.

    will give the other idea a go
     
    stephan2307, Mar 29, 2010 IP
  9. meloncreative

    meloncreative Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    cool good luck, quite intrigued to find out whats breaking it
     
    meloncreative, Mar 29, 2010 IP
  10. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #10
    right I did some test.

    in loadDatabases.php if I hardcode the connection details the script works. It loads all the database names into my div.

    So what I did then is simply copied the form into my index.php file and submited it via a normal POST and at the beginning of the file (but after session_start();) I check if a POST has been sent and if then I set Session variables. Then I output all session variables and they are correctly set. Then if I reload the page the session variables get lost.

    How confusing.
     
    stephan2307, Mar 29, 2010 IP
  11. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #11
    YAY I found the problem and fixed it.

    The problem was that I tried to set SESSIONS like this
    
    $_SESSION[1]['host'] ='xxx';
    $_SESSION[1]['user'] ='xxx';
    $_SESSION[1]['pass'] ='xxx';
    
    PHP:
    and if I changed it to
    
    $_SESSION['pane_1']['host'] ='xxx';
    $_SESSION['pane_1']['user'] ='xxx';
    $_SESSION['pane_1']['pass'] ='xxx';
    
    PHP:
    it worked. So it seems that you can't have numbers as key or something like that.

    Strange but I fixed it

    thanks for your help
     
    stephan2307, Mar 29, 2010 IP