Basic Registration Form with PHP

余生颓废 提交于 2019-12-25 00:34:54

问题


Can you please help me. I'm a beginner in PHP and still learning the basics of the language. I'm currently learning how to create sessions. Apparently there's a bug when I try out one of the examples in the book. The HTML form looks like this:

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

and the PHP script is something like:

<?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 } ?>

When I output both of the files on the browser, the browser reports that: Fatal Error: Can't use function value in write context in 'path of the file' and line of the code.

Please help out by fixing this and feel free to add on anything that you might find useful for me. Thanks in Advance...


回答1:


There are a number of issues I can see with your code.

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

Should be

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

Also, you're looking for a $_POST variable called username when your form field is called name.

As for the actual error you're seeing, that's because of the way you're using empty and trim together - you should check the 2 separately. E.g.

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



回答2:


I tink its going to be a good idea to trim your variables after receiving them trom the global variable before checking if they arer empty

        $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 />";
      }


来源:https://stackoverflow.com/questions/16598560/basic-registration-form-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!