0

The page where the problem occurs is Challenge1.php This page is one of many Challenge pages. Here is its code:

<?PHP
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}

ini_set('display_errors', 'On'); 
error_reporting(E_ALL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
  <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  <title>Challenges</title>
  <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css" />
  <script type='text/javascript' src='scripts/gen_validatorv31.js'></script>
  <link rel="STYLESHEET" type="text/css" href="style/pwdwidget.css" />
  <script src="scripts/pwdwidget.js" type="text/javascript"></script>
  <link rel="shortcut icon" type="image/ico"  href="favicon.ico" />
</head>
<body>
<?php include('nav.php'); ?>
<!-- Form Code Start -->
<div id='fg_membersite'>
<br/>
<?php 

if(!$fgmembersite->DBLogin())
{
    $fgmembersite->HandleError("Database login failed!");
    return false;
}

$query = mysqli_query($fgmembersite->connection,"SELECT Title, Points, description, Hint, HintCost, Hint2, Hint2Cost from solutions where id_solution ='1'");
$fgmembersite->connection;

include('Challenge.php'); ?>


</div>

</body>
</html>

Now, as you can see, at the top, it includes the file membersite_config.php . This file contains:

<?PHP
require_once("./include/fg_membersite.php");

$fgmembersite = new FGMembersite();

And followed by the DB connection strings to a MySQL db and such. Now the important part is that it includes the fg_membersite.php file. This file has 1000 lines of code and is where all the php code is kept. You also see that the config file creates $fgmembersite. Remember this, it's important in a sec.

Now, back to the beginning: Challenge1.php also includes challenge.php at the bottom. Every Challenge1-14.php page includes challenge.php as all those pages need that same code.

Now here's the problem. Code snippet from challenge.php:

$fgmembersite->car();

function CheckSol()
{
    $fgmembersite->car2();

now, functions "car" and "car2" simply echo "testing" and "testing again", respectively. These functions are written in fg_membersite.php. now here's the actual issue the function car() works, echoes "testing" but car2() does not. in fact, in php debug I get this error:

Notice: Undefined variable: fgmembersite in /customers/9/f/7/bvhtuinen.be/httpd.www/CTF/Challenge.php on line 22

Fatal error: Call to a member function car() on null in /customers/9/f/7/bvhtuinen.be/httpd.www/CTF/Challenge.php on line 22

Line 22 being the line where car2(); is at.

So ultimately, my question is: why can't I call a function within a function? (obviously, inside 'CheckSol, which by the way is called on Challenge1.php's form action, there is a lot more code which also needs $fgmembersite...)

I hope I'm clear enough and thank you all in advance!

Community
  • 1
  • 1
  • We need to see the code for the car and car2 methods. – Professor of programming Apr 08 '15 at 10:25
  • The notice says very clear: there is no variable `$fgmembersite` defined in function `CheckSol()`. If you think that `$fgmembersite` is the **global** variable `$fgmembersite` then you must tell PHP about that. Either pass it as a [function argument](http://php.net/manual/en/functions.arguments.php) or declare it as [`global`](http://php.net/manual/en/language.variables.scope.php) inside the `CheckSol()` function. – axiac Apr 08 '15 at 10:27
  • @Bonner no, you don't. `car2()` is not called, the fatal error message says that the function was not actually called because it was invoked using an invalid (undefined) object. – axiac Apr 08 '15 at 10:29
  • @axiac the error message that Jeno has mentioned does not match up with his statement where he says the car2() method is called on line 22, the error says the car() method is called, either this is a typo by Jeno or there is more to this. – Professor of programming Apr 08 '15 at 10:31
  • 1
    @Bonner I saw the mismatch but you still don't need to see the body of a function that doesn't run to tell why it doesn't produce any output :-) – axiac Apr 08 '15 at 10:33
  • @axiac I can see where you are coming from, without the global declaration there is no object because $fgmembersite is not declared within the scope of the CheckSol method, I get it, this is most likely the problem because the code being called looks procedural, therefore it is unlikely a case of a missed $this-> but I still don't think we should jump to conclusions. – Professor of programming Apr 08 '15 at 10:37

1 Answers1

0

You have to pass $fgmembersite as a parameter, or declare it global as below.

$fgmembersite->car();

function CheckSol()
{
    global $fgmembersite;
    $fgmembersite->car2();
mathieu
  • 477
  • 3
  • 9