SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Writing to a file help

    Hi folks,

    I am trying to create a script that will take the MYSQl DB details that have been sent from a form and store it in a file for usage at a later stage.

    Am I doing this correctly?

    PHP Code:
    $File "db-settings.php"
    $Handle fopen($File'w');

    $Data "$dbuser = $_POST['dbuser'];\n"
    fwrite($Handle$Data); 

    $Data "$dbpass = $_POST['dbpass'];\n"
    fwrite($Handle$Data); 

    $Data "$db = $_POST['db'];\n"
    fwrite($Handle$Data); 

    $Data "$host = $_POST['host'];\n"
    fwrite($Handle$Data); 

    print 
    "Data Written"
    fclose($Handle); 
    Any advice would be greatly appreciated

  2. #2
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Kansas City, MO
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to escape $dbuser and $dbpass and so forth. Otherwise you'll end up with:

    = username
    = password
    = db
    = host

    see below.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Kansas City, MO
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $File "db-settings.php"
    $Handle fopen($File'w');

    $Data "\$dbuser = {$_POST['dbuser']};\n"
    fwrite($Handle$Data); 

    $Data "\$dbpass = {$_POST['dbpass']};\n"
    fwrite($Handle$Data); 

    $Data "\$db = {$_POST['db']};\n"
    fwrite($Handle$Data); 

    $Data "\$host = {$_POST['host']};\n"
    fwrite($Handle$Data); 

    print 
    "Data Written"
    fclose($Handle); 

  4. #4
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I prefer using an ini-file (not very Unix but still), protected by htaccess.
    To read/write from and to ini-files I wrote a simple PHP class.
    For example an ini-file would contain:
    setting = value; #Comments
    FOR SALE: 1 set of morals, never used, will sell cheap

  5. #5
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ZareMedia,
    Thanks for the great tip

    I have added this for error checking:
    PHP Code:

    if(!$Handle) {
        echo 
    'Error, the file could not be opened or there was an error creating it.';
        exit;

    Should I be checking anytinhg else?

  6. #6
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way to check that data was written?

  7. #7
    SitePoint Addict
    Join Date
    Feb 2005
    Location
    Brisbane, Australia
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at the file?


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
  •