繁体   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