PHP not outputting Array-ThrowExceptions

Exception or error:

so I wanted to try some backend style code and ive done some messing around with PHP. I’ve made a simple program (using html and php) that asks the user to create an account, then these usernames and passwords will be stored.

Im adding a feature (at the bottom) that asks if the user wants to display a certain username (un) and password (pw). However, when i enter some random un’s and pw’s, and type 1 (so program should output un 1 and pw 1) it does not work…

Any help would be grately appreciated!

<html>
  <body>
    <style>
      input {
        font-family: 'Trebuchet MS'
      }
      form {
        font-family: 'Trebuchet MS'
      }
      body {
        background-color: lightblue

      }
      h1 {
        font-family: 'Trebuchet MS'

      }
    </style>
    <h1 id = "signup_here">Signup here!</h1>
    <form action = "[the website uses my real name, so im not including it]" method = "get">
      Your Username: <input type = "text" name = "username">
      <br/>
      Your Password: <input type = "text" name = "password">
      <input type = "submit">
    </form>
    <br/>
    <?PHP
      $username = $_GET["username"];
      $password = $_GET["password"];
      if(!is_null($username) and !is_null($password)){
        echo "<h3>Your username is [$username] and your password is [$password]";
      }
    ?>
    <form method = "post">
      Please confirm: <input type = "checkbox" name = "up_conf" value = "val1">
      <input type = "submit">
    </form>
    <?PHP
      if(isset($_POST["up_conf"])) {
        echo "<h3>Thanks for signing up!</h3>";
      }
    ?>
    <?PHP
      if(isset($_POST["up_conf"])) {
        $allusernames = array($_GET["username"]);
        $allpasswords = array($_GET["password"]);
      }
    ?>
    <form action = "https://PhP-Playground.---.repl.co" method = "get">
      Get usernames and passwords? 1/2/3/etc.: <input type = "text" name = "gaup">
      <?PHP
        if(!is_null($_GET["gaup"])) {
          echo "Username:" . $allusernames[$_GET["gaup"]];
          echo "Password:" . $allpassword[$_GET["gaup"]];
        }
      ?>
    </form>
  </body>
</html>
How to solve:

I think you have a handful of things going on here and you may need to simplify them to get to the root of the particular problem.

A couple basic things:

  1. You don’t need a full URL as the action parameter of your <form> — you can (and often should) use relative URLs. E.g. <form action="/some-page" method="post"> — that way you wouldn’t need to censor the URL.

  2. When debugging forms, stick to one form to page — that will help avoid confusion between $_GET and $_POST

  3. A helpful debugging tactic is to use print_r() to show the entire contents of a variable. This can be combined with HTML’s <pre> tag for easier readability, e.g.

<pre>
<?php
   print_r($_POST);
?>
</pre>
  1. Don’t use <?PHP — instead stick to the lower-case <?php — I know PHP permits some really sloppy carelessness when it comes to capitalization, but I know I’ve been on servers that were configured in a way that things like short codes would not run.

  2. Don’t submit login forms using get — use post.

Hopefully that helps you see where your data is going.

Leave a Reply

Your email address will not be published. Required fields are marked *