SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member slicemaster101's Avatar
    Join Date
    May 2004
    Location
    USA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Storing Database connection information in a dedicated config file

    Well,

    I am quit new to PHP and I am really getting to like it. I have been messing around with PHP and MySQL now for a few months and I have been leaning some practical design concepts. Anyways one practical design concept that I would really like to learn is how to utilize and store common configuration information such as Database names, logins, and passwords in a dedicated PHP file to be used by a host of others. This is so if I ever had to change the username and password for accessing a given database, I would only have to change it in one file rather then all of the files that make use of that data. This type of configuration file is used in many common PHP scripts like phpMyAdmin and PHPBB2. How do the do it?

    Thanks,
    Slice

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    most of the scripts and tutorials I've seen, simply use the include() function. they create a small file, usually named db.inc, that contains all the pertinent information for a given database and table. then they just
    Code:
    include ('db.inc');
    in the appropriate files.

    I presume this is what you meant.

    Lite...

  3. #3
    SitePoint Member slicemaster101's Avatar
    Join Date
    May 2004
    Location
    USA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by litebearer2
    most of the scripts and tutorials I've seen, simply use the include() function. they create a small file, usually named db.inc, that contains all the pertinent information for a given database and table. then they just
    Code:
    include ('db.inc');
    in the appropriate files.

    I presume this is what you meant.

    Lite...
    Could you recommend a tutorial on how to do this, and possibly a practical example?

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok example...


    the db.inc file
    Code:
    <?PHP
    // db.inc
    // this connects to the artists database table
    
    // make sure you place your own values in these variables
    
    $db_location = "localhost";  
    $db_username = "your user name here";  
    $db_password = "your password here";  
    $db_database = "the database name here";   
    
    // open the connection if it fails exit
    $conn = mysql_connect($db_location, $db_username, $db_password); 
    
    if (!$conn) {
     echo "could not connect" . "<br>";
      exit;
    }
     
    // connect to the database, if it fails exit
    
    $result2 = mysql_select_db($db_database,$conn) or die ("Could not open database"); 	
    if(!$result2) {
      echo "could not open db" . "<br>";
      exit;
    }
    
    ?>
    the php file(s) that will need to connect to the database
    Code:
    <?PHP
    
    // this next line can be used in all the files/pages
    // that use the artists database
    
    include('db.inc');
    
    // the rest of your php code -- search, add, modify, delete
    // records in the artists database
    
    ?>
    My major problem with a lot of the tutorials is their presumption that the reader has any understanding of what is happening. When I first began to use mysql I could not for the life of me find out what I was supposed to substitute for 'localhost'. Til it dawned on me that frequently, 'localhost' is literally what needs to be there, and that it was not a figurative value.

    Google searches starting with key words 'php' 'mysql' 'manual' and then your question, tend to generate some very useful pages.

    Hope this helps. If you need more feel free to ask - I have all the answers - just that often many of them are wrong

    Lite...

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a note about '.inc' file extension.

    By default, your webserver doesn't know what '.inc' files are and how to handle them. And when it doesn't know the file type, it returns the file as text/plain. So, what happens if user just types www.site.com/db.inc in browser address line?

    In your own interest , always use '.php' extension for files containing php code (even single line)...

  6. #6
    SitePoint Member slicemaster101's Avatar
    Join Date
    May 2004
    Location
    USA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Yes all this has been very helpful. I got it all figured out now…
    Thanks all.

    Quote Originally Posted by stereofrog
    Just a note about '.inc' file extension.

    By default, your webserver doesn't know what '.inc' files are and how to handle them. And when it doesn't know the file type, it returns the file as text/plain. So, what happens if user just types www.site.com/db.inc in browser address line?

    In your own interest , always use '.php' extension for files containing php code (even single line)...
    Also, I know to always use *.PHP as like you said if a malicious user figures out the location and name of the configuration file that is not *.PHP they can access the content held with in them in plain text and gain access to logins and passwords that should not be public knowledge for security reasons.

    Signed,
    Slice


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
  •