2016-01-12 32 views
-1

我試圖修改論壇的登錄表單,以便在登錄後,它會去,「歡迎,用戶名」。php - 保存用戶名作爲變量,然後在其他地方回顯

這裏的signin.php內容一大塊 -

if($errors == false){ 

    $user = $db->get_row("SELECT * FROM " . TABLES_PREFIX . "users WHERE email = '" . $_POST['username_or_email'] . "' OR username = '" . $_POST['username_or_email'] . "' ORDER BY id DESC LIMIT 0,1"); 

    if($user AND ($user->password == encode_password($_POST['password']))){ 
     if($user->status == 'pending'){ 
      $layout->AddContentById('alert', $layout->GetContent('alert')); 
      $layout->AddContentById('alert_nature', ' alert-danger'); 
      $layout->AddContentById('alert_heading', '{{ST:error}}!'); 
      $layout->AddContentById('alert_message', '{{ST:your_account_not_activated}}'); 
     }elseif($user->status == 'banned'){ 
      $layout->AddContentById('alert', $layout->GetContent('alert')); 
      $layout->AddContentById('alert_nature', ' alert-danger'); 
      $layout->AddContentById('alert_heading', '{{ST:error}}!'); 
      $layout->AddContentById('alert_message', '{{ST:your_account_is_banned}}'); 
     }else{ 
      Users_LoggedIn($user, isset($_POST['remember_me'])); 

      // **I'm guessing this is exactly where I should store the username as var** 

      header('Location: '.FORUM_URL); // redirects to index.php 
      exit; 
     } 
    }else{ 
     $layout->AddContentById('alert', $layout->GetContent('alert')); 
     $layout->AddContentById('alert_nature', ' alert-danger'); 
     $layout->AddContentById('alert_heading', '{{ST:error}}!'); 
     $layout->AddContentById('alert_message', '{{ST:the_info_is_not_correct}}'); 
    } 
} 

而且,當時在我的index.php想我會加上這麼幾行去類似的信息(僞代碼) -

echo '<div id="username">Welcome, '+$USERNAME+'</div>'; 

我試着玩$ _SESSION ['用戶名'],但我很無用的PHP,所以任何指導將是非常有益的,謝謝。

+0

即C/JS串聯語法''+ $ USERNAME +''你想使用點。 –

+0

http://php.net/manual/en/reserved.variables.session.php –

+0

'Users_LoggedIn()'做了什麼?我想它會保存對用戶的引用,以便在隨後的請求中提供用戶名。試試'print_r($ _ SESSION)'看看它是否在那裏,也許它在$ _SESSION ['user'] ['username']'或類似的東西,或者它只是保存用戶ID,你會需要從數據庫中提取用戶(或修改'Users_LoggedIn()'方法來存儲用戶名)。 – dotcomly

回答

0

您可以使用會話

在您的登錄腳本:

session_start(); 

...如果你想

}else{ 
    Users_LoggedIn($user, isset($_POST['remember_me'])); 

    $_SESSION['Username'] = $user['username']; 

    header('Location: '.FORUM_URL); // redirects to index.php 
    exit; 
} 

顯示它:

session_start(); 

echo '<div id="username">Welcome, '. $_SESSION['Username'] .'</div>'; 

更多示例這裏:http://php.net/manual/en/session.examples.php

+0

我所得到的只是一個空白頁面提交... –

+0

你知道你的'$ user'變量包含什麼嗎?你應該嘗試使用'var_dump()'或'print_r()'來查看它的內容。也許我們將不正確的值分配給會話。 –

+0

這仍然是'我的頭',但謝謝你給它一個鏡頭。 –

相關問題