2014-11-20 100 views
-1

我對web開發和PHP非常陌生。我正在創建一個帶有會員區域的網站,並希望顯示歡迎「用戶名」消息。在我的實際代碼中有一個歡迎消息顯示用戶的全名。有人可以幫我嗎?下面我粘貼我用於login.php和login-home.php(會員區)的代碼。謝謝將歡迎信息從全名更改爲用戶名(PHP)


的login.php

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

if(isset($_POST['submitted'])) 
{ 
    if($fgmembersite->Login()) 
    { 
     $fgmembersite->RedirectToURL("login-home.php"); 
    } 
} 

?> 
<!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>Login</title> 
     <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css" /> 
     <script type='text/javascript' src='scripts/gen_validatorv31.js'></script> 
</head> 
<body> 

<!-- Form Code Start --> 
<div class="logo_container"></div> 
<div class='splash-container'> 
    <div id='fg_membersite'> 
     <form id='login' action='<?php echo $fgmembersite->GetSelfScript(); ?>' method='post' accept-charset='UTF-8'> 
     <fieldset > 
     <legend>Orcaly</legend> 

     <input type='hidden' name='submitted' id='submitted' value='1'/> 

     <div><span class='error'><?php echo $fgmembersite->GetErrorMessage(); ?></span></div> 
     <div class='container'> 
      <label for='username' ></label><br/> 
      <input type='text' placeholder="UserName"name='username' id='username' value='<?php echo $fgmembersite->SafeDisplay('username') ?>' maxlength="50" /><br/> 
      <span id='login_username_errorloc' class='error'></span> 
     </div> 
     <div class='container'> 
      <label for='password' ></label><br/> 
      <input type='password' placeholder="Password" name='password' id='password' maxlength="50" /><br/> 
      <span id='login_password_errorloc' class='error'></span> 
     </div> 

     <div class='container'> 
      <input type='submit' name='Submit' value='Submit' /> 
     </div> 
     <div class='short_explanation'><a href='reset-pwd-req.php'>Forgot Password?</a></div> 
     <div class='register'><a href='register.php'>Register</a></div> 
     </fieldset> 
     </form> 
     <!-- client-side Form Validations: 
     Uses the excellent form validation script from JavaScript-coder.com--> 

     <script type='text/javascript'> 
     // <![CDATA[ 

      var frmvalidator = new Validator("login"); 
      frmvalidator.EnableOnPageErrorDisplay(); 
      frmvalidator.EnableMsgsTogether(); 

      frmvalidator.addValidation("username","req","Please provide your username"); 

      frmvalidator.addValidation("password","req","Please provide the password"); 

     // ]]> 
     </script> 
    </div> 
</div> 
<!-- 
Form Code End (see html-form-guide.com for more info.) 
--> 

</body> 
</html> 

登錄-home.php

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

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

?> 
<!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>Home page</title> 
     <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css"> 
</head> 
<body> 
<div class="logo_container"></div> 
    <div class='splash-container'> 
     <div id='fg_membersite'> 
     <fieldset> 
     <legend>Home Page</legend> 
     <div class='confirmation'>Welcome back <?= $fgmembersite->UserFullName(); ?>!</div> 
     <div class='confirmation'><a href='change-pwd.php'>Change password</a></div> 
     <div class='register'><a href='access-controlled.php'>A sample 'members-only' page</a></div> 
     <br> 
     <div class='register'><a href='logout.php'>Logout</a></div> 
     </fieldset> 
     </div> 
    </div> 
</body> 
</html> 
+2

'$ fgmembersite'的數據結構是什麼? – Nico 2014-11-20 20:54:06

+0

在我的fg_membersite.php中:函數UserFullName() { return isset($ _ SESSION ['name_of_user'])?$ _ SESSION ['name_of_user']:''; } – Palejo 2014-11-20 21:04:44

+0

在我的fg_membersite.php中: – Palejo 2014-11-20 21:05:08

回答

1

答案很簡單,你需要改變這一行代碼:

<div class='confirmation'>Welcome back <?= $fgmembersite->UserFullName(); ?>!</div> 

這樣$ fgmembersite-> UserFullName();成爲一個變量,保存您的用戶名或返回您的用戶名的函數。現在它是一個返回用戶全名的函數。

這可能會超出您目前的編程技能。有一件非常簡單和可行的事情就是在用戶登錄時將用戶名存儲爲會話變量。然後,您可以替換$ fgmembersite-> UserFullName();用$ _SESSION [「用戶名」]或任何你想要調用它。

或者,您可以定義自己的函數來返回用戶名,或者如果用戶名由於某種原因而不可用,則可以使用其他值(這是稍微好一點的設計)。