Categories > TinyButStrong general >

Variables not set when using include_once or require_once

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Anthony
Date: 2011-03-30
Time: 16:14

Variables not set when using include_once or require_once

I have a custom error handler to control html status codes such as 404, 301, etc. Sometimes Apache calls my error_handler.php controller file and sometimes another PHP script calls it. When Apache calls it, all the variables in the error_handler.php file get set and display as expected in the HTML template file; however, when I use the code below to include the file, then not all variables are getting set. I'm not sure what the problem is and perhaps someone may be able to tell me the problem here.

function PageNotFound () {
    header("HTTP/1.0 404 Not Found");
    $error = "404";  // this variable is used in error_handler.php
    include_once(SYSTEMPATH . 'assets/errorpages/error_handler.php');  // include custom error page
    exit;
}


Here's my HTML template file for error_hanlder.php. Both "section_title" and "wrong_path" do not get set when I use the include_once in the above script, but do get set when error_handler.php is called by Apache.
[onload;file=../html/global/header_global.html]
        <div id="wrapper">
            <div id='left_col'>
               
            </div>
            <div id='right_col'>
                <h2>[onshow.section_title; noerr]</h2>
                <p>[onload.wrong_path; noerr]</p>
                <p>We are unable to locate the page you were looking for. The address may be incorrect or the page may no
                longer exist.</p>
                <div id="quick">
                    <h3>Send Quick Error Report</h3>
                    <p>You may send a quick report of this error which will send the relevant details to our Web Editor.</p>
                    <form action="error_report.php" method="post">
                        <div>
                            <input type="hidden" name="referer" value="">
                            <input type="hidden" name="error_report" value="[onload.wrong_path; noerr]">
                        </div>
                        <p>
                            <input type="submit" name="submit_quick" value="Report this error">
                        </p>
                    </form>
                </div>
            </div>
        </div>
        [onload;file=../html/global/footer_global.html]


Both variables are referenced in the error_handler.php file that calls the HTML template:

$section_title = "Page not found";
$wrong_path = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
By: Skrol29
Date: 2011-04-01
Time: 01:26

Re: Variables not set when using include_once or require_once

Hi,

Your line
include_once(SYSTEMPATH . 'assets/errorpages/error_handler.php');
is called by a function, so when the script "error_handler.php" create variables $section_title and $wrong_path, those variables are local to the function, not global to PHP.
but [onload] and [onshow] can merge only global variables.
By: Anthony
Date: 2011-04-01
Time: 02:11

Re: Variables not set when using include_once or require_once

Yep...that's the problem. Just needed someone to get me to see the trees and forest. I was thinking that perhaps it was a wrong implementation of TBS, but variable scope is always important to consider. Simply looked to long at the code and didn't see the obvious.

Thanks!