0

你能帮我么。我是 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 } ?>

当我在浏览器上输出这两个文件时,浏览器报告:致命错误:无法在“文件路径”和代码行的写入上下文中使用函数值。

请通过修复此问题提供帮助,并随时添加您可能认为对我有用的任何内容。提前致谢...

4

2 回答 2

2

我可以看到您的代码存在许多问题。

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

应该

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

此外,您正在寻找一个在调用表单字段时$_POST调用的变量。usernamename

至于您看到的实际错误,那是因为您使用emptytrim一起使用的方式 - 您应该单独检查 2。例如

$uname = isset($_POST['username']) ? trim($_POST['username']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
if(!empty($uname)  && !empty($email)) {
于 2013-05-16T22:47:11.390 回答
0

我认为在从全局变量中接收变量之后修剪变量是一个好主意,然后再检查它们是否为空

        $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 />";
      }
于 2013-05-16T22:40:21.690 回答