PHP Newbie question

Discussion in 'Programming/Scripts' started by Yabadoo, Jul 21, 2008.

  1. Yabadoo

    Yabadoo Member

    The following php code is used in a part of an index.php file which is a part of my website.

    if ($config['webmessage_DBstatus'] == 1) {
    $query = mysql_query("SELECT * FROM newsgroups LIMIT 1");
    $result = mysql_fetch_array($query);
    echo '<br>';
    echo '<br>';
    echo 'Database Size: '. showdbsize(). '<br>';
    echo 'Current Retention: '. avgRetention() . ' days<br>';
    echo 'Amount of Files in database: '. GetFileCount().'<br>';
    echo 'Amount of headers in database: '. GetHeaderCount().'<br>';
    }

    Because this takes to much cpu load i want to do this in a seperate php file which i can run in cron job once every 2 hours.
    So i want to write the result of this file tot a .cache file.

    I use the 2 php file's listed below.
    hour.cron.php
    <?
    include_once "../inc/functions.inc.php";
    GetConfig(1);
    include_once "other.functions.php";

    cache_dbstatuse();
    ?>

    other.functions.php
    <?
    if(!function_exists('file_put_contents'))
    {
    function file_put_contents ($filename, $data)
    {
    if($fp = fopen($filename, 'w'))
    {
    fwrite($fp, $data);
    fclose($fp);

    return true;
    }else{
    return false;
    }
    }
    }

    function cache_dbstatus()
    {
    $content = $config['web_title'];
    file_put_contents ('../cache/dbstatus.cache', $content);
    }
    ?>
    I don't now how to program the above listed code from my index file to write it to the file ..cache/webtitle.cache...

    Any help is welkom!

    Thanks in advance.
     
  2. falko

    falko Super Moderator Howtoforge Staff

    You can run
    Code:
    crontab -e
    to create the cron job. The cron job could look as follows:
    Code:
    0 */2 * * * /usr/bin/php /path/to/phpscript
     
  3. Yabadoo

    Yabadoo Member

    Thanks for the answer Falko, but this was not my question. I know how to make a cronjob, but what i don't now is how to translate this php code
    if ($config['webmessage_DBstatus'] == 1) {
    $query = mysql_query("SELECT * FROM newsgroups LIMIT 1");
    $result = mysql_fetch_array($query);
    echo '<br>';
    echo '<br>';
    echo 'Database Size: '. showdbsize(). '<br>';
    echo 'Current Retention: '. avgRetention() . ' days<br>';
    echo 'Amount of Files in database: '. GetFileCount().'<br>';
    echo 'Amount of headers in database: '. GetHeaderCount().'<br>';

    to run this in a cron job, and the output must go to a seperate file wich
    i can call in my index.php.

    function cache_dbstatus()
    {
    $content = $config['web_title'];
    file_put_contents ('../cache/dbstatus.cache', $content);
    }
    ?>

    in this way..???
     
  4. falko

    falko Super Moderator Howtoforge Staff

    Instead of using echos I'd save the output in a variable and then write the content of that variable to a file using file_put_contents().
     
  5. Yabadoo

    Yabadoo Member

    I solved it in another way.....
    The code below is a php file, the output i redirect to a file. (i run this as a cron job every 2 hours...)

    GetConfig(1);
    if(!function_exists('file_put_contents'))
    {
    function file_put_contents ($filename, $data)
    {
    if($fp = fopen($filename, 'w'))
    {
    fwrite($fp, $data);
    fclose($fp);

    return true;
    }else{
    return false;
    }
    }
    }
    $empty = "";
    file_put_contents ('../cache/dbstatus.cache', $empty);

    $query = mysql_query("SELECT * FROM newsgroups LIMIT 1");
    $result = mysql_fetch_array($query);
    echo '<br>';
    echo '<br>';
    echo 'Database Size: '. showdbsize(). '<br>';
    echo 'Current Retention: '. avgRetention() . ' days<br>';
    echo 'Amount of Files in database: '. GetFileCount().'<br>';
    echo 'Amount of headers in database: '. GetHeaderCount().'<br>';



    in the overal funtion.php file i added this routine
    function dbstatus()
    {
    echo file_get_contents (PATH . '/cache/dbstatus.cache');
    }

    In my index.php i added the folowing line
    {
    dbstatus
    }


    This works okay for me now.... maybe not the best solution, but it works for me!!

    Thanks for the reply.
     

Share This Page