user982853
user982853

Reputation: 2488

Php function not define

I have a function with a default value of nothing, example:

function output_message($message="") {
  if (!empty($message)) { 
    return "<p class=\"message\">{$message}</p>";
  } else {
    return "";
  }
}

I then am echoing the message on my log in form. If there are errors it will display a message but if there are no errors it should just do nothing. When my page loads it says my function is not defined. Below is my html page.

<?php
require_once("../../includes/functions.php");
require_once("../../includes/session.php");
require_once("../../includes/database.php");
require_once("../../includes/user.php");

if($session->is_logged_in()) {
  redirect_to("index.php");
}

// Remember to give your form's submit tag a name="submit" attribute!
if (isset($_POST['submit'])) { // Form has been submitted.

  $username = trim($_POST['username']);
  $password = trim($_POST['password']);

  // Check database to see if username/password exist.
  $found_user = User::authenticate($username, $password);

  if ($found_user) {
    $session->login($found_user);
    redirect_to("index.php");
  } else {
    // username/password combo was not found in the database
    $message = "Username/password combination incorrect.";
  }

} else { // Form has not been submitted.
  $username = "";
  $password = "";
}
<html>
  <head>
    <title>Photo Gallery</title>
    <link href="../stylesheets/main.css" media="all" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="header">
      <h1>Photo Gallery</h1>
    </div>
    <div id="main">
        <h2>Staff Login</h2>
        <?php echo output_message($message); ?>

        <form action="login.php" method="post">
          <table>
            <tr>
              <td>Username:</td>
              <td>
                <input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" />
              </td>
            </tr>
            <tr>
              <td>Password:</td>
              <td>
                <input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <input type="submit" name="submit" value="Login" />
              </td>
            </tr>
          </table>
        </form>
    </div>
    <div id="footer">Copyright <?php echo date("Y", time()); ?>, Kevin Skoglund</div>
  </body>
</html>
<?php if(isset($database)) { $database->close_connection(); } ?>

I am not sure why I am getting this error because in my function i am setting the default value of $message in the function. output_message($message="").

Can someone look at my code and tell me why it is telling me my function is not defined. Thanks.

Upvotes: 0

Views: 1464

Answers (2)

jeroen
jeroen

Reputation: 91744

Based on your comment, it seems that it is not the function that is not defined, but the variable.

The problem is not with the function itself, there your variable $message is set to an empty string if no variable is supplied to the function.

The problem is with your function call:

<?php echo output_message($message); ?>

Here you are calling your function with a variable, also called $message, but completely unrelated to the variable $message in your function. This global variable does not exist and that is why php is giving you a warning.

The way you defined your function, means that you can call it like:

<?php echo output_message(); ?>

without any problems; if no variable is supplied, the $message variable in the function is set to an empty string.

However, using:

<?php echo output_message($any_variable_name); ?>

will generate a warning when $any_variable_name is not defined in the scope you are in (in this case the global scope).

Upvotes: 1

guiman
guiman

Reputation: 1334

I dont really know where are you using the output_message but my first guess is that those functions "redirect_to" may be the problem.

Those functions most of the time do something like this:

   header( 'Location: http://some_where_on.yourweb' );

Check that you have made the right includes in the .php file where you are using the output_message function.

Also, i would recommend to follow the advises Phil and jeroen told as comments:

  • missing ?> before tag
  • add init_set("display_errors","On"); error_reporting(E_ALL)
  • initialize variables like $message to avoid warnings and side effects
  • Try not to mix rendering with logic (this one is mine)

Upvotes: 0

Related Questions