簡體   English   中英

PHP基本注冊表

[英]Basic Registration Form with PHP

你能幫我么。 我是PHP的初學者,仍然在學習該語言的基礎知識。 我目前正在學習如何創建會話。 當我嘗試書中的一個例子時,顯然有一個錯誤。 HTML表單如下所示:

<form action="session_registration_form_script.php" method="post">
    <label for="name">Name:</label>
    <input type="text" name="name" />
    <label for="email">Email:</label>
    <input type="text" name="email" />
    <input type="submit" value="Register" />
</form>

和PHP腳本是這樣的:

<?php

    // Initialize session data
    session_start();

    /*
     * If the user is already registered, display a 
     * message letting them know.
     */

     if(isset($_SERVER['username'])) {
         echo "You're already registered as $_SESSION[username]";
     }

     // Checks if the form was submitted
     else if($_SERVER['REQUEST_METHOD'] == 'POST') {

         /*
          * If both the username and email fields were filled
          * out, save the username in a session variable and 
          * output a thank you message to the browser. To 
          * eliminate leading and trailing whitespace, we use the 
          * trim() function
          */
          if(!empty(trim($_POST['username']))
          && !empty(trim($_POST['email']))) {

              // Store escaped $_POST values in variables 
              $uname = htmlentities($_POST['username']);
              $email = htmlentities($_POST['email']);

              $_SESSION['username'] = $uname;
              echo "Thanks for registering! <br />",
              "Username: $uname <br />",
              "Email: $email <br />";
          }

          /*
           * If the user did not fill out both fields, display 
           * a message letting them know that both fields are 
           * required for registration
           */

           else {
               echo "Please fill out both fields! <br />";
           }
     }

     // If the form was not submitted, dispalys the HTML form

     else {

?>

<?php } ?>

當我在瀏覽器上輸出兩個文件時,瀏覽器會報告: 致命錯誤:無法在“文件路徑”和代碼行的寫入上下文中使用函數值。

請通過修復此問題來幫助您,並隨意添加您可能對我有用的任何內容。 提前致謝...

我可以在您的代碼中看到許多問題。

 if(isset($_SERVER['username'])) {
     echo "You're already registered as $_SESSION[username]";
 }

應該

 if(isset($_SESSION['username'])) {
     echo "You're already registered as ".$_SESSION['username'];
 }

此外,當您的表單字段名為name時,您正在尋找名為username$_POST變量。

至於你看到的實際錯誤,那是因為你一直使用emptytrim的方式 - 你應該分別檢查2。 例如

$uname = isset($_POST['username']) ? trim($_POST['username']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
if(!empty($uname)  && !empty($email)) {

我認為在檢查變量是否為空之前,在接收到變量之后修剪變量是一個好主意

        $uname = trim($_POST['username']);
        $email = trim($_POST['email']);
      if(!empty($uname)  && !empty($email)) {
    //if(!empty($_POST['username']) && !empty($_POST['email']))
    //{

          // Store escaped $_POST values in variables 
          $uname = htmlentities($_POST['username']);
          $email = htmlentities($_POST['email']);

          $_SESSION['username'] = $uname;
          echo "Thanks for registering! <br />",
          "Username: $uname <br />",
          "Email: $email <br />";
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM