2011-08-06 302 views
1

我得到一個未定義的變量錯誤$id行15變量& 21,有人可以解釋爲什麼嗎?我看不出有什麼問題。未定義變量?

<?php 
function userIsLoggedIn() 
{ 
    if (isset($_POST['action']) and $_POST['action'] == 'login') 
    { 
     if (!isset($_POST['email']) or $_POST['email'] == '' or 
      !isset($_POST['password']) or $_POST['password'] == '') 
     { 
      $GLOBALS['loginError'] = 'Please fill in both fields'; 
      return FALSE; 
     } 
     $password = md5($_POST['password'] . 'chainfire db'); 

     if (databaseContainsAuthor($_POST['email'], $password, $id)) 
     { 
     include 'db.inc.php'; 
      session_start(); 
      $_SESSION['loggedIn'] = TRUE; 
      $_SESSION['email'] = $_POST['email']; 
      $_SESSION['password'] = $password; 
      $_SESSION['id'] = $id; 
      return TRUE; 
     } 
     else 
     { 
      session_start(); 
      unset($_SESSION['loggedIn']); 
      unset($_SESSION['email']); 
      unset($_SESSION['password']); 
      unset($_SESSION['id']); 
      $GLOBALS['loginError'] = 'The specified email address or password was incorrect.'; 
      return FALSE; 
     } 
    } 
    if (isset($_POST['action']) and $_POST['action'] == 'logout') 
    { 
     session_start(); 
     unset($_SESSION['loggedIn']); 
     unset($_SESSION['email']); 
     unset($_SESSION['password']); 
     unset($_SESSION['id']); 
     header('Location: ' . $_POST['goto']); 
     exit(); 
    } 
    session_start(); 
    if (isset($_SESSION['loggedIn'])) 
    { 
     return databaseContainsAuthor($_SESSION['email'], $_SESSION['password'], $_SESSION['id']); 
    } 
} 
function databaseContainsAuthor($email, $password, $id) 
{ 
    include 'db.inc.php'; 

    $email = mysqli_real_escape_string($link, $email); 
    $password = mysqli_real_escape_string($link, $password); 

    $sql = "SELECT COUNT(*) FROM author 
      WHERE email='$email' AND password='$password'"; 
    $result = mysqli_query($link, $sql); 

    if (!$result) 
    { 
     $error = 'Error searching for author.'; 
     include 'error.html.php'; 
     exit(); 
    } 
    $row = mysqli_fetch_array($result); 

    $sql = "SELECT id FROM author 
      WHERE email='$email'"; 
    $id = mysqli_query($link, $sql); 
    if (!$id) 
    { 
     $error = 'Error searching for id.'; 
     include 'error.html.php'; 
     exit(); 
    }  

    if ($row[0] > 0) 
    { 
     return TRUE; 
    } 
    else 
    { 
     return FALSE; 
    } 
} 

變量$iddatabaseContainsAuthor($email, $password, $id)定義,然後存儲在$_SESSION['id']會話,這樣自然$id = mysqli_query($link, $sql);應該已經過去了,但它不是?

+0

[「注意:未定義的變量」和「注意:未定義的索引」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – animuson

回答

1

幾東西 在使用它之前應該定義變量$ id(不是必需的但是很好的做法)

因此,例如

$id = NULL; 
if (databaseContainsAuthor($_POST['email'], $password, $id)) 

還設置databaseContainsAuthor函數內的$ id並不意味着$ id將這一職能範圍之外的變化。

你可以把它推向全球,但被認爲是不好的做法,

也是你的功能databaseContainsAuthor

包含此代碼

if ($row[0] > 0) 
{ 
    return TRUE; 
} 
else 
{ 
    return FALSE; 
} 

將返回TRUE或FALSE。但要注意,一旦代碼返回一個值,沒有代碼後,將運行

,這意味着這部分可能也被註釋掉,因爲這是它永遠不會被運行

$sql = "SELECT id FROM author 
      WHERE email='$email'"; 

    $id = mysqli_query($link, $sql); 
    if (!$id) 
    { 
     $error = 'Error searching for id.'; 
     include 'error.html.php'; 
     exit(); 
    }  
return語句後
+0

謝謝,$ id變量的原因是將作者id存儲在session()中,以便我可以將id與作者提交的文章鏈接起來。如果有更好的方式來存儲當前用戶的ID,那就是當前登錄的用戶,在session()中,我完全贊成。 – rumspringa00

+0

你正在做的事情很好,把if/return語句放在賦值$ id = mysqli的代碼之後.....把這個函數定義改爲databaseContainsAuthor($ email,$ password,&$ id) – bumperbox

+0

我做了你的建議,但我仍然得到未定義的變量錯誤$ 14行,21中的任何想法? Op用更新後的代碼進行更新。謝謝。 – rumspringa00

2

函數內部變量(或定義)的變量不會影響腳本的其餘部分。例如:

<?php 
function changeVariabe($person) { 
    $person = 'Bob'; 
} 
$person = 'Alice'; 
changeVariable($person); 
echo "Hello $person!"; // Outputs: Hello Alice! 

這可以通過引用傳遞的變量,這樣可避免:

<?php 
function changeVariabe(&$person) { 
    $person = 'Bob'; 
} 
$person = 'Alice'; 
changeVariable($person); 
echo "Hello $person!"; // Outputs: Hello Bob! 

您也可以使用全局變量,像這樣:

<?php 
function changeVariabe() { 
    global $person; 
    $person = 'Bob'; 
} 
$person = 'Alice'; 
changeVariable(); 
echo "Hello $person!"; // Outputs: Hello Bob!