2012-01-13 17 views
0

を定義していません。エラーがある場合はメッセージを表示しますが、エラーがなければ何もしません。私のページが読み込まれるとき、私の関数は定義されていないと言います。以下は私のHTMLページです。PHP関数は、私は何のデフォルト値、例と機能を持っている

<?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(); } ?> 

私の関数の中で、私は関数内で$メッセージのデフォルト値を設定していますので、私はこのエラーを取得していますなぜ私はわかりません。 output_message($ message = "")。

誰かが自分のコードを見て、私の機能が定義されていないと言っている理由を教えてもらえますか?ありがとう。

+3

'?>'の前に ''の最後がない? 'output_message()'関数はどこで定義されていますか?また、開発のためにまともなエラー報告を有効にすることをお勧めします。これをスクリプトの一番上に置きます。 'ini_set( 'display_errors'、 'On'); error_reporting(E_ALL); ' – Phil

+1

' $ message'変数を初期化して未定義の変数(未定義関数には関係ない...)の警告を避けることもできます。 – jeroen

+0

@philそれはdocの上部に含まれるmy includesフォルダにある関数ファイルにあります。 – user982853

答えて

0

私は本当にあなたがどこでoutput_messageを使っているのかわかりませんが、私の最初の推測は、それらの関数 "redirect_to"が問題であるかもしれないということです。あなたは右のあなたがoutput_message機能を使用しているの.phpファイルに含まをした

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

チェック:

これらの関数の時間のほとんどは、このような何かを行います。

また、私はフィルとのJeroenがコメントとして語った助言に従うことをお勧めします:

  • 行方不明>タグ
  • 前init_setを追加します( "はdisplay_errors"、 "オン");? $メッセージのようにするerror_reporting(E_ALL)
  • が初期化変数が
  • は(この1つは私のものです)
1

あなたのコメントに基づいてロジックとレンダリングを混在しないようにしよう警告や副作用を回避するために、あると思われます定義されていない関数ではなく、変数です。

問題は関数そのものではなく、変数$messageは関数に変数が指定されていない場合は空の文字列に設定されます。

問題は、あなたの関数呼び出しである:

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

あなたも$messageという変数を使用して関数を呼び出しているが、あなたの関数内の変数$messageとは全く無関係。このグローバル変数は存在しないため、phpが警告を出しています。

あなたはあなたの関数を定義した方法は、あなたのようにそれを呼び出すことができることを意味します。何の問題もなく

<?php echo output_message(); ?> 

。変数が指定されない場合、関数の変数$messageは空の文字列に設定されます。

しかし、使用して:$any_variable_nameは、あなたが(この場合はグローバルスコープ)にあるスコープで定義されていない場合

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

すると、警告を生成します。

関連する問題