Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Storing user name and password for MYSQL

  1. #1
    Join Date
    Sep 2007
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Storing user name and password for MYSQL

    I think I seen it some where but cant find it again
    Is there a way to store
    $username="name";
    $password="password";
    $database="mydatabase";
    that is used to access MYSQL from a PHP page
    in a seperate file and have the PHP page get it from there when needed.

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    you could store them in an xml file?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The common way of doing it is storing them in a PHP file as variables, then including it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Sep 2007
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    The common way of doing it is storing them in a PHP file as variables, then including it.
    Can you give excample of how to do it

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    You can learn how to include files in a PHP file here


    PHP Code:
    dbop.php
    <?
        
    require_once("dbconfig.php");    
    ?>
    PHP Code:
    dbconfig.php
    <?
        $dbuser 
    "root"//Mention the database user who can access the database and perform the operations in your case.
        
    $dbpassword "somepassword";    
    ?>
    Last edited by codeexploiter; 09-12-2007 at 06:21 AM.

  7. #7
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    PHP Code:
    $dbuser "root"
    Ouch. Never ever ever use root to do logins or anything to do with databases when the pages are going to be viewed on the internet. If a hacker gets into your site, and finds out your root password through your page, consider your database gone.

    If you created separate accounts in MySQL with different privileges, the most damage a hacker could do is delete all your accounts (That's if you set the privileges to allow to delete)
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I usually do it this way because I like to use a $_CONFIG array. However, if you only have primitive values to store it would be simpler to use defines:
    Code:
    define('DBUSER', 'root'); // Why the heck are you using the root account?
    define('DBHOST', 'localhost');
    define('DBPASSWORD', 'somepassword');
    Code:
    mysql_connect(DBHOST, DBUSER, DBPASSWORD);
    If you use global variables in a function, you have to worry about writing
    Code:
    global $dbhost, $dbuser, $dbpassword;
    all the time. Defines remove the necessity for this.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Also, do not (is that clear enough?) DO NOT store your username / password in anything but a .php page (or a page which the server will parse as php if it is accessed.)

    The reason for this is simple. A few years ago, there was a silly fad to simply use *.inc for an include file. As you can see, this is not secure...

    View a couple of these, and presto! you have the database username, password and table names...

    be very, very careful where you store those variables.

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I was going to post a similar reply to boogyman's suggestion of xml. Very bad idea.
    It would store it fine and be accessible, but it would not be secure at all.

    Using .php (and variations) is the only way to go, since php is secure in itself, and you are using it anyway. You could, in theory, store it some other way, but the necessarily piece is that php can use the data, so might as well make that the only way to hack it. If you use two methods, it just doubles the odds against you, not to mention that if your PHP isn't secure, there's no real point to it anyway. (Generally php should be secure, unless you have a security flaw in a script.)


    the trend for .inc is kinda weird, though in some cases can be a nice distinction.
    Clearly, it's not secure, and a bad idea as such, with these exceptions:
    1. Use it for nonsecure code (ie some function that doesn't really give anything away about your system). //shrug... probably not the best idea.
    2. Configure your host to treat .inc as a php parsed extension, and it will become secure.
    3. I like, in some cases, using .inc.php, since it does differentiate, but keeps it as .php.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •