Super Tiny Script Needed

Discussion in 'PHP' started by Brentnow, Sep 9, 2007.

  1. #1
    I have an online demo for a software program I sell. In my demo I allow users to save their preferences. This changes the config.php file, which is okay.

    But every 30 to 60 minutes I would like the config to be replaced with a "fresh" version, that does not have the demo user's preferences. (Users make choices that are not ideal to showcase the product well.)

    So I need a script that will replace the config file that has been altered with a config file that is "prestine".

    I assume the script would not need to be more than one short line of code, and would work in conjunction with a cron task, which also needs to be written.

    I would really appreciate some help, and would also be perfectly willing to make a payment to your paypal account for your time if you wish :)

    I greatly look forward to your response.
    Thank you,
    Brent
     
    Brentnow, Sep 9, 2007 IP
  2. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    
    file_put_contents('config.php', 'ideal config data')
    
    ?>
    PHP:
    That should overwrite any changes.
     
    streety, Sep 9, 2007 IP
  3. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you. But will this really overwrite the user's preferences and return the config file back to its "default" state? How does your script "know" what the default state is? I was assuming that the script would call a file from a directory to overwrite the user's config. This over-writing would then bring the config back to the original default state.
    Also, this needs to be run every 30 to 60 minutes.
    Can you explain please?
    Thank you.
    Brent
     
    Brentnow, Sep 9, 2007 IP
  4. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #4
    run a cron job every X minutes, thatll solve that problem
    basically get the cron to run something like streety suggested
     
    m0nkeymafia, Sep 9, 2007 IP
  5. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It would write 'ideal config data' to the file overwriting what is already there. You would need to change 'ideal config data' to the actual data.

    Alternatively you could do the following

    <?php
    
    file_put_contents('config.php', file_get_contents('config-pristine.php'));
    
    ?>
    PHP:
    That would allow you to just rename the 'pristine' config file without copy and pasting it into this rewriting file.

    A cron job would be the best way to set it up so that it does the rewrite every 30 or 60 minutes. I've previously written instructions on running a PHP script with a cron job on my blog. I hope it helps.
     
    streety, Sep 9, 2007 IP
  6. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Excellent! That is exactly what I was looking for.
    I will check out your blog for the cron instructions.
    Thank you so much!
    Brent
     
    Brentnow, Sep 9, 2007 IP
  7. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have Linux with cPanel. Refering to your blog:
    Should I enter into the cron file something like the code:

    "lynx -dump http://www.my-website.com/cron_script.php" ?

    I would of coarse insert the suggested code you posted into a file named cron_script.php. I would like it to run every 30 minutes.
    Brent
     
    Brentnow, Sep 9, 2007 IP
  8. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Okay. I have a cron task now opperating. But I am getting an error.

    My cron command is:
    php -q /home/my-username-here/public_html/examples/demo1/cron_script.php


    My cron_script.php is:
    <?phpfile_put_contents('/examples/demo1/config.php',
    file_get_contents('/examples/demo1/config-pristine.php'));?>

    My error message sent to me is:
    PHP Fatal error: Call to undefined function: phpfile_put_contents() in /home/my-username-here/public_html/examples/demo1/cron_script.php on line 1
    <br />
    <b>Fatal error</b>: Call to undefined function: phpfile_put_contents() in <b>/home/my-username-here/public_html/examples/demo1/cron_script.php</b> on line <b>1</b><br />

    I made sure the config files both exist in the directory.

    Any ideas?
    Brent
     
    Brentnow, Sep 9, 2007 IP
  9. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Your cron_script.php should looks something like this.

    <?php
    file_put_contents('/examples/demo1/config.php', file_get_contents('/examples/demo1/config-pristine.php'));
    ?>
    PHP:
     
    Vbot, Sep 9, 2007 IP
  10. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you Vbot, but that is exactly the same code as I already had. I repasted it all on one line just to make sure. But I am getting exactly the same error message.
    Any suggestions or work arounds?
    Brent
     
    Brentnow, Sep 9, 2007 IP
  11. seppo0010

    seppo0010 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    file_put_contents was created on PHP 5, so in PHP 4 you don't have that function.

    A workaround, that will work on PHP 4 and PHP 5
    
    <?php
    if ( !function_exists('file_put_contents') && !defined('FILE_APPEND') ) {
        define('FILE_APPEND', 1);
        function file_put_contents($n, $d, $flag = false) {
            $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
            $f = @fopen($n, $mode);
            if ($f === false) {
                return 0;
            } else {
                if (is_array($d)) $d = implode($d);
                $bytes_written = fwrite($f, $d);
                fclose($f);
                return $bytes_written;
            }
        }
    }
    
    file_put_contents('/examples/demo1/config.php', file_get_contents('/examples/demo1/config-pristine.php'));
    ?>
    
    PHP:
     
    seppo0010, Sep 9, 2007 IP
  12. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thank you so much! Yes, I do not have PHP5. I have installed your script as-is.
    But I get the following error:

    PHP Parse error: syntax error, unexpected '{' in /home/I-inserted-my-username-here/public_html/examples/demo1/cron_script.php on line 1
    <br />
    <b>Parse error</b>: syntax error, unexpected '{' in <b>/home/I-inserted-my-username-here/public_html/examples/demo1/cron_script.php</b> on line <b>1</b><br />


    My cron task command is currently:
    php -q /home/I-inserted-my-username-here/public_html/examples/demo1/cron_script.php

    I think we are SO close now.
    Can you help a little more please?
    Brent
     
    Brentnow, Sep 10, 2007 IP
  13. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I'm way too used to PHP5 now. I think the first problem is you need a space between <?php and the start of the function.

    I can't see anything wrong with seppo0010's code. So no idea why it's not working.

    The following is taken from the php manual so should work.

    <?php
    $filename = '/examples/demo1/config.php';
    $somecontent = file_get_contents('/examples/demo1/config-pristine.php');
    
    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {
    
        // In our example we're opening $filename in append mode.
        // The file pointer is at the bottom of the file hence
        // that's where $somecontent will go when we fwrite() it.
        if (!$handle = fopen($filename, 'w')) {
             echo "Cannot open file ($filename)";
             exit;
        }
    
        // Write $somecontent to our opened file.
        if (fwrite($handle, $somecontent) === FALSE) {
            echo "Cannot write to file ($filename)";
            exit;
        }
    
        echo "Success, wrote ($somecontent) to file ($filename)";
    
        fclose($handle);
    
    } else {
        echo "The file $filename is not writable";
    }
    ?> 
    PHP:
    As you're using PHP4 don't forget the big event at the end of the year when support for PHP4 ends and in all likelihood it becomes hacker central.
     
    streety, Sep 10, 2007 IP
  14. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thank you streety.
    This one is a bugger though.

    I have used your exact code, but now get the following error:

    PHP Warning: file_get_contents(/examples/demo1/config-pristine.php): failed to open stream: No such file or directory in /home/my-username-here/public_html/examples/demo1/cron_script.php on line 2
    <br />
    <b>Warning</b>: file_get_contents(/examples/demo1/config-pristine.php): failed to open stream: No such file or directory in <b>/home/my-username-here/public_html/examples/demo1/cron_script.php</b> on line <b>2</b><br />

    I have set permissions to 777 for both config files and cron script.
    I have also checked the paths and names, and of coarse replaced (my-username-here) with actual username. It looks like it should work to me, but I am not a php coder.

    Any more ideas?
    I think we are even closer to success now.
     
    Brentnow, Sep 10, 2007 IP
  15. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Here is the latest error message:

    PHP Warning: file_get_contents(/examples/demo1/config-pristine.php): failed to open stream: No such file or directory in /home/my-username-here/public_html/examples/demo1/cron_script.php on line 3
    <br />
    <b>Warning</b>: file_get_contents(/examples/demo1/config-pristine.php): failed to open stream: No such file or directory in <b>/home/my-username-here/public_html/examples/demo1/cron_script.php</b> on line <b>3</b><br />
    The file /examples/demo1/config.php is not writable

    The last sentence is also a head scratcher.
    Also, I don't have a good php editor, but I have been trying to maintain the proper format as in the example above.
    Brent
     
    Brentnow, Sep 10, 2007 IP
  16. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #16
    i use textpad, notepad with syntax highlighting!!

    right lemmie have a bash at those errors
    firstly with file_get_contents i'd suggest trying absolute paths, google it youll find how to get it. with some file functions [and ive not checked about this one] that will hopefully solve those errors

    the last error basically means the file your trying to edit is read only, youll want to CHMOD the file permissions, youll need to make sure they are writeable by your server
     
    m0nkeymafia, Sep 10, 2007 IP
  17. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    I'll check out textpad.
    Also, the file in the last error is 777.
    I assume that is okay.
     
    Brentnow, Sep 10, 2007 IP
  18. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #18
    yeah that should make it writeable, only other thing i can think of is that its not being found? any advances on the errors your getting?
     
    m0nkeymafia, Sep 11, 2007 IP
  19. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    I do not know what you mean by "advances".
    I have been studying this as best I can and am trying a few different things.
    But I am still getting the error:

    PHP Warning: file_get_contents(/examples/demo1/config-pristine.php): failed to open stream: No such file or directory in /home/my-username-here/public_html/examples/demo1/cron_script.php on line 3
    <br />
    <b>Warning</b>: file_get_contents(/examples/demo1/config-pristine.php): failed to open stream: No such file or directory in <b>/home/my-username-here/public_html/examples/demo1/cron_script.php</b> on line <b>3</b><br />
    The file /examples/demo1/config.php is not writable

    I am really hoping to get this cron task and script working so my config can get over-written with a "new" pristine copy of the config file.

    Anyone have new script or cron task ideas?
    Thank you,
    Brent
     
    Brentnow, Sep 11, 2007 IP
  20. Brentnow

    Brentnow Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Success!!

    I needed to add a longer path in the script code, even though the script is in the same directory as the config.
    Here is my new code:

    <?php
    $filename = '/home/my-username-here/public_html/examples/demo1/config.php';
    $somecontent = file_get_contents('/home/my-username-here/public_html/examples/demo1/config-pristine.php');
    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {
    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'w')) {
    echo "Cannot open file ($filename)";
    exit;
    }
    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
    }
    echo "Success, wrote ($somecontent) to file ($filename)";
    fclose($handle);
    } else {
    echo "The file $filename is not writable";
    }
    ?>

    Thanks to everyone, and especially streety!
    Thank you!
    Brent
     
    Brentnow, Sep 11, 2007 IP