SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best Connection practices?

    Hello,

    I have a very busy gaming website that is fully database driven. The site receives upto 15k uniques a day and 60 million page views per month.

    Currently, each new page is included into the main index.php file. The connection to the database is achieved via the following in the index.php:

    Code:
    include("config.php");
    The contents of the config.php file look like this:

    Code:
    $dbserver = "localhost";                        
    $dbuser = "userhere";
    $dbpass = "passhere"; 
    $db = "dbnamehere";
    mysql_connect($dbserver, $dbuser, $dbpass);
    mysql_select_db($db);
    So here is an example of what a user may do and how the site responds:

    User visits site which loads index.php. User then goes to the games page which includes games.php into the index.php file in the appropriate place. So index.php is thus loaded twice. Once with the standard data and then once with the new included file.

    There is no code anywhere in any script that close the db connection.

    So my question to you is - is there a problem with this setup? And if so, what would be the best way to do this.

    Thanks.

  2. #2
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Well, for one thing, I would definitely check the connection with some sort of error checking. If the database is down you don't want the user to do anything really.

    I have also requested for this thread to be moved to the PHP forum since it is more of a PHP conventions question.

  3. #3
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone else have any input on this?

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well to start with I wouldn't put my code to connect to the database inside a file called config.php.

    I am not sure by the way you explained it if config.php is included twice within the same request or across different requests. But doesn't really matter because by default mysql_connect will reuse the existing connection so you won't get two connections. But you probably want to use require_once() instead of include().

    You don't need to explicitly call mysql_close() because the connection will be closed when the script ends anyway.

    Another thing is you really should put the config.php file outside of the web root otherwise if php stops working for whatever reason the contents of any php file could be displayed in plain text.

    Also you might want to consider using persistent connections.

    http://www.php.net/manual/en/feature...onnections.php
    http://www.php.net/manual/en/functio...l-pconnect.php

    There are some things to be carefull of when using persistent connections so read up.


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
  •