Sessions and persistent connections to MySQL database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M. Katz

    Sessions and persistent connections to MySQL database

    I'm relatively new to PHP/MySQL and I've heard about maintaining a
    persistent connection to the database. To save overhead, I'd like to
    maintain the connection in a session variable, but I have a few
    questions first.

    I'm able to establish session variables and test them with isset() to
    avoid re-defining them with each new web-page load. So is this really
    as simple as

    if (!isset($_SESSI ON['connection'])) {
    $connection = mysql_connect(" SomeServer","Us erName","passwo rd");
    $_SESSION['connection'] = $connection;
    };

    Will this work? and is there a better way to do it--or a more
    conventional way? What should I be thinking about vis a vis security
    regarding this connection.

    Is there a recommended way to make the session's connection variable
    "expire"? I suppose I could store the time the connection was
    established and then check the elapsed time with each web-page
    re-load, dumping the session if it's been too long.

    Is there a preferred way to store $connection without using
    $_SESSION[]?

    Thanks,
    M. Katz
  • Guillaume Brocker

    #2
    Re: Sessions and persistent connections to MySQL database

    M. Katz wrote:
    [color=blue]
    > I'm relatively new to PHP/MySQL and I've heard about maintaining a
    > persistent connection to the database. To save overhead, I'd like to
    > maintain the connection in a session variable, but I have a few
    > questions first.
    >
    > I'm able to establish session variables and test them with isset() to
    > avoid re-defining them with each new web-page load. So is this really
    > as simple as
    >
    > if (!isset($_SESSI ON['connection'])) {
    > $connection = mysql_connect(" SomeServer","Us erName","passwo rd");
    > $_SESSION['connection'] = $connection;
    > };
    >
    > Will this work? and is there a better way to do it--or a more
    > conventional way? What should I be thinking about vis a vis security
    > regarding this connection.[/color]

    I'm not sure that this will work. A so created MySQL connection will be
    closed when the script ends. You will store the handle to connection in
    the session variable but it will invalid since the real connection will
    be closed between to script calls.

    There is another function in the MySQL module for opening persistent
    connections: mysql_pconnect.
    [color=blue]
    > Is there a recommended way to make the session's connection variable
    > "expire"? I suppose I could store the time the connection was
    > established and then check the elapsed time with each web-page
    > re-load, dumping the session if it's been too long.[/color]

    Using mysql_pconnect, the connection life-time will be managed by PHP.
    You don't need to make any explicite connection closure and after a
    while, remaining connections will be automatically closed by PHP.
    [color=blue]
    > Is there a preferred way to store $connection without using
    > $_SESSION[]?
    >
    > Thanks,
    > M. Katz[/color]

    Guillaume Brocker

    Comment

    • Tomislav Petrovic

      #3
      Re: Sessions and persistent connections to MySQL database

      M. Katz wrote:[color=blue]
      > I'm relatively new to PHP/MySQL and I've heard about maintaining a
      > persistent connection to the database. To save overhead, I'd like to
      > maintain the connection in a session variable, but I have a few
      > questions first.
      >[/color]

      If I am not entirely correct, somebody correct me....
      MySQL connection is a resource variable and thus cannot
      be used and stored into a session. It will not work.
      I tried it, before I found out this, and it did not. Maybe I
      did something wrong....

      For what you want to accomplish use mysql_pconnect which gives
      you a reusable persistent connection to mysql.

      Tomy.


      Comment

      • Kre¹o Kunjas

        #4
        Re: Sessions and persistent connections to MySQL database

        On 10 Feb 2004 00:39:53 -0800, M. Katz wrote:
        [color=blue]
        > I'm relatively new to PHP/MySQL and I've heard about maintaining a
        > persistent connection to the database. To save overhead, I'd like to
        > maintain the connection in a session variable, but I have a few
        > questions first.
        >
        > I'm able to establish session variables and test them with isset() to
        > avoid re-defining them with each new web-page load. So is this really
        > as simple as
        >
        > if (!isset($_SESSI ON['connection'])) {
        > $connection = mysql_connect(" SomeServer","Us erName","passwo rd");[/color]
        you must use mysql_pconnect instead
        [color=blue]
        > $_SESSION['connection'] = $connection;
        > };
        >
        > Will this work? and is there a better way to do it--or a more
        > conventional way? What should I be thinking about vis a vis security
        > regarding this connection.
        >
        > Is there a recommended way to make the session's connection variable
        > "expire"? I suppose I could store the time the connection was
        > established and then check the elapsed time with each web-page
        > re-load, dumping the session if it's been too long.
        >
        > Is there a preferred way to store $connection without using
        > $_SESSION[]?[/color]
        i think that you dont have to store it in session variables
        just use permanent connection (mysql_pconnect ).
        It's all explained in PHP manual

        Comment

        • Jochen Daum

          #5
          Re: Sessions and persistent connections to MySQL database

          Hi M!

          On 10 Feb 2004 00:39:53 -0800, MKatz843@onebox .com (M. Katz) wrote:
          [color=blue]
          >I'm relatively new to PHP/MySQL and I've heard about maintaining a
          >persistent connection to the database. To save overhead, I'd like to
          >maintain the connection in a session variable, but I have a few
          >questions first.[/color]

          Not dependant on if it works, I believe the overhead to connect to
          MySQL is lower than keeping the session data on the webserver.

          HTH, Jochen
          --
          Jochen Daum - Cabletalk Group Ltd.
          PHP DB Edit Toolkit -- PHP scripts for building
          database editing interfaces.
          Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

          Comment

          Working...