1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Sessions Not Being Created As Desired

Discussion in 'PHP & Perl' started by PopUnder, Jan 9, 2013.

  1. PopUnder

    PopUnder Newbie

    Joined:
    Dec 19, 2012
    Messages:
    35
    Likes Received:
    5
    Home Page:
    Hi

    I am looking to create a site where a session lasts 30 minutes. There is a session table that is updated with a list of new sessions. I have the following thing coded. But I noticed that there are over 213 sessions created although the test-site only had 23 pageviews from 10 visits or so (which means 10 sessions at the maximum). Could you please tell me where I am going wrong that there were over 213 sessions created in one day?

    HTML:
          session_start();
          $result=mysql_query("SELECT * FROM $database_name.sessiontable_name WHERE id='".session_id()."';",$databaseconnect);
          $row = mysql_fetch_array($result);
          $id=$row["id"];
          $b=false;
          if(count($id)==0){
                $t=date("i");
                $id=session_id();
                $q="INSERT INTO $database_name.sessiontable_name (id, timesession) VALUES (\"$id\", $t);";
                mysql_query($q);
                $b=true;
     
          }
          else{
                $t=intval($row["timesession"]);
                if(abs($t-intval(date("i")))>30){
                $t=date("i");
                      $id=session_id();
                      mysql_query("UPDATE $database_name.sessiontable_name SET timesession=$t WHERE id=\"$id\";");
                      $b=true;
                }     
          }
     
    
     
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,439
    Likes Received:
    10,045
    Are you using multiple connections? The first mysql query has a resource parameter while the others don't.
     
    • Thanks Thanks x 1
  3. PopUnder

    PopUnder Newbie

    Joined:
    Dec 19, 2012
    Messages:
    35
    Likes Received:
    5
    Home Page:
    Apologies if I am not helping you with the correct reply. I am not a programmer, just trying to debug the code a developer wrote for me:

    By resource parameter, I am assuming it's the $databaseconnect variable you are referring to. It's defined as this in the relevant connection file:

    HTML:
            $databaseconnect = mysql_connect("localhost", "root", "");  
            $databaseconnect = mysql_connect("localhost", "username", "password");
    Would you count this as two resources?

    Would it help if I added the resource parameter to the end of the other mysql queries as well?
     
  4. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,439
    Likes Received:
    10,045
    Hi, thanks for the clarification,

    It 's 1 resource, although there 's not reason to have it declared twice. You can keep the line that has the correct username and password (i suppose the 2nd one) and delete the other.

    Let 's try something:

    Create 2 files on the same site (e.g. test1.php and test2.php) with the following content on both
    Code:
    <?php
    
    session_start();
    echo session_id();
    
    ?>
    
    Now, open test1.php and then open test2.php.

    Is the output the same in both cases?
     
    • Thanks Thanks x 1
  5. PopUnder

    PopUnder Newbie

    Joined:
    Dec 19, 2012
    Messages:
    35
    Likes Received:
    5
    Home Page:
    Hi Jazzc,

    Tried it..Yes, when tested from the same browser, it shows the same session id on both test1.php and test2.php
     
  6. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,439
    Likes Received:
    10,045
    Next one :)

    Add var_dump($row); after the $row = line, browse a bit and see if it 's always false or not.
     
  7. PopUnder

    PopUnder Newbie

    Joined:
    Dec 19, 2012
    Messages:
    35
    Likes Received:
    5
    Home Page:
    I have added it on my code. But could you clarify where I need to check this? Thanks a lot for the help.
     
  8. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,439
    Likes Received:
    10,045
    var_dump dumps the content of the variable in the html page. If it 's not viewable directly, try doing view source on the browser.

    For example, var_dump($i_do_not_exist); would display NULL

    To help you locate it if you have a lot of output you can also add a echo 'VAR_DUMP RESULT FOLLOWS<br>'; directly before it.
     
    • Thanks Thanks x 1
  9. PopUnder

    PopUnder Newbie

    Joined:
    Dec 19, 2012
    Messages:
    35
    Likes Received:
    5
    Home Page:
    Hi Jazzc

    Thanks a lot for all the help. Apparently, the bug was because of another script mistake and did not have anything to do with session IDs. Sorry for taking your time.
     
    • Thanks Thanks x 1