Im getting an strange result with this particular code. Let me explain my self in short way because is a long code. Basically the hole thing works like this:
In the beginning of my script I call my class fg_membersite and some constants:
require_once("registration/include/membersite_config.php");
After that, I check some POST variables, if the code detects something wrong immediately calls the function the_error() (located on the same script). If there is no error, there is no call to the function and the execution goes.
An example of checking:
$result=mysql_query("SELECT id FROM ".table_links." WHERE url = '".$fgmembersite->SanitizeForSQL($_POST['j_title'])."';");
if (mysql_num_rows($result)>0) {
the_error('The url '. siteurl .'/'. $_POST['j_title'].' is currently in use, please use another one.');
}
After a series of POST and SQL checks there is the final part when I save to the database and print the result message:
$fgmembersite->DBLogin();
mysql_query("INSERT INTO " . table_links . " (id, usr_code, visits, url, datetime, type, description, secu_type, password, phone, address, email, photo, id_number) VALUES (NULL, '".$_SESSION['user_code']."', '0', '".$fgmembersite->SanitizeForSQL($_POST['j_title'])."', NOW(), '0', '".$fgmembersite->SanitizeForSQL($_POST['j_desc'])."', '".$secutype."', '".$mod_pass."', '".$phone_check."', '".$address_check."', '".$email_check."', '".$photo_check."', '".$id_check."');");
echo '<!DOCTYPE html>
<html lang="en">
<head>';
include("parts/head.php");
echo '</head>
<body>
<div id="footerfix">';
include("parts/header.php");
echo '<div class="container">
<center><br><br><br><img src="css/img/ok.png"/></center>
<div class="hero-unit">
<center>
<h2>Your track URL <a href="'. siteurl . '/' . $_POST['j_title'].'">'.siteurl . '/' . $_POST['j_title'].'</a> have been created.</h2>';
if ($secutype==0){echo '<p>This url is public. Click <a href="profile.php">here</a> to return to your dashboard.</p>';}
if ($secutype==1){echo '<p>This is a password protected url. Click <a href="profile.php">here</a> to return to your dashboard.</p>';}
if ($secutype==2){echo '<p style="font-size:16px">The visitors of this URL will need an special QR code. We offer you two options:</p>
<p>1. Download the <a href="http://chart.googleapis.com/chart?chs=70x70&cht=qr&chf=bg,s,FFFFFF00&chl='.$mod_pass.'">QR code image</a> and add it to your personal design <br>2. Or Download a ready to print business card model</p><br><p>Click <a href="profile.php">here</a> to return to your dashboard.</p>';}
echo '</center>
</div>
</div>';
include("parts/footer.php");
echo '
</div>
</body>
</html>';
}
Take in consideration that all the included files, like head.php and header.php need the fg_membersite loaded before you include them. Until now the code works fine, but if the function the_error is called, I get several errors telling me that the head.php and header.php don’t find the class loaded. This is the_error:
function the_error($error) {
echo '<!DOCTYPE html>
<html lang="en">
<head>';
include("parts/head.php");
echo '</head>
<body>
<div id="footerfix">';
include("parts/header.php");
echo '<div class="container"><center><br><br><br><img src="css/img/notok.png"/></center><div class="hero-unit"><center><h2>Error</h2>';
echo '<p>'.$error.'</p>';
echo '</center></div></div>';
include("parts/footer.php");
echo '</div></body></html>';
exit;
}
As you can see, the_error() is almost the same code I use to print the results. So, why the include works fine when I call them outside the function? Seems like when I use include in the PHP script outside a function it just copy the code from the external file into the original script (which is what I need) and when I use the include inside a function it first runs the remote script and then add the result (which will not work because the class is loaded on the script who use the include).
If I’m right and knowing the code flow, how do you suggest to solve this issue?
If I undertand your problem correctly, it could be that you are trying to access variables (the instance of your class) from the global scope inside your function. Since those included files are loaded from inside the function they will only have access to that scope.
You can just use:
In the first line inside
the_error()body;