1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Include Php File In Page

Discussion in 'Troubleshooting and Problems' started by Xarcell, Jan 9, 2011.

  1. Xarcell

    Xarcell Well-Known Member

    I'm not even sure this can be done.

    I purchased a baby name script and is installed at my forum root different in a different DB. Now I'm trying to include it into a page.

    This is what I have:

    library/XenForo/Pages/BabyNames.php

    Code:
    <?php
    class XenForo_Pages_BabyNames
    {
         public static function includeFile(XenForo_ControllerPublic_Abstract  $controller, XenForo_ControllerResponse_Abstract &$response)
        {
            ob_start();
            include('http://inspireromance.com/babynames/index.php');
            $babyNames = ob_get_contents();
            ob_end_clean();
    
            $params = array(
                'babyNames'  => $babyNames
            );
    
            $response->params = array_merge(
                $response->params,
                $params
            );
        }
    }
    Page:
    Code:
    <div class="baseHtml messageText">
    
    {xen:raw $babyNames}
    
    </div>
    PHP Callback: XenForo_Pages_BabyNames :: includeFile


    The error I get when visiting the page:

    Anything I can do to make this work?
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Code:
    include('http://inspireromance.com/babynames/index.php');
    
    Try using the server path instead of the URL.
     
  3. Xarcell

    Xarcell Well-Known Member

    Ok, this is the error I get now.

    It separate databases, could that be the cause?
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    What is on line 4 of that file?

    It looks to me like that script is not compatible with the level of PHP error reporting used by xenForo. It runs fine on its own. Perhaps you can modify the script to change error reporting levels before and after its execution:

    http://php.net/manual/en/function.error-reporting.php

    Try adding this to the beginning of the script:

    Code:
    error_reporting(E_ERROR);
    
    And add this to the end of it to change it back to the level xenForo uses:

    Code:
    error_reporting(E_ALL | E_STRICT & ~8192);
    
     
  5. Xarcell

    Xarcell Well-Known Member

    I don't know php.

    But I think line 4 is a variable?
    Code:
    $letter=$_GET[letter];
    I added what you said to the beginning and end of the index.php file. Should it be added to all of the files?

    Anyhow, this is the error it gives now:
     
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    That is another error which can be suppressed with lower levels of error reporting. Try changing the error reporting level in that file as well (babynames/origin.php).

    Ultimately this is a problem with the babynames script. But I think you can suppress it by changing the error reporting level.
     
  7. Xarcell

    Xarcell Well-Known Member

    Ok, I added the error handling to the origin.php and now I get a blank page.

    origin.php
    Code:
    //include "conn.php";
    
    error_reporting(E_ERROR);
    
    //create a cache every 3 hour
    
    function writeCache($data,$cacheFile='default_cache.txt'){
        if(!$fp=fopen($cacheFile,'w')){
             trigger_error('Error opening cache file');
             exit();
        }
        if(!flock($fp,LOCK_EX)){
            trigger_error('Unable to lock file');
            exit();
        }
        if(!fwrite($fp,serialize($data))){
            trigger_error('Error writing to cache file');
            exit();
        }
        flock($fp,LOCK_UN);
        fclose($fp);
    }
    function readCache($cacheFile){
         if(!file_exists($cacheFile)){
             trigger_error('Invalid cache file');
             exit();
         }
         return unserialize(file_get_contents($cacheFile));
    }
       
    /************************************************************************************************/
    
    //Main body of the page
    //Check if there is an existing cache and if time is not yet expired
    // define cache file
    $cacheFile="cache/origins.txt";
    // define expire time in seconds (24 hours)
    $expireTime=486400;
    // check to see if cache file is valid (time triggered caching)
    
    if(file_exists($cacheFile) && filemtime($cacheFile) > (time()-$expireTime)){
        $data=readCache($cacheFile);
                                  }
    else{
    
    $sq = "SELECT origin FROM names group by origin order by origin,name";
    $rs = mysql_query($sq) or die(mysql_error());
     
    while($datas=mysql_fetch_array($rs)){
               $data .= "<option value='$datas[origin]'>$datas[origin]</option>";
                             }
    
    writeCache($data,$cacheFile);
    
    }
    
    $options = $data;
    
    error_reporting(E_ALL | E_STRICT & ~8192);
     
  8. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    A blank page is often a suppressed error. Try adding this line to your library/config.php file:

    Code:
    ini_set('display_errors', true);
    
    That may reveal an error message on the blank page.
     

Share This Page