1

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>
RtHAitP30D
  • 306
  • 3
  • 6
  • 19
  • What do you mean by "does not work"? Is the output missing completely? Is the output there, but not as expected? Do you see any errors or notices? If you're unsure how to check for errors, see [this post](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – El_Vanja Mar 16 '20 at 19:00
  • Is `https://PhP-Playground.saatvikrk.repl.co` the URL of this script? If not, `$_GET['gaup']` will not ever be set in your scripts. – Barmar Mar 16 '20 at 19:01
  • `$allusernames` is not in the same script invocation as the result of submitting the form. You need to use a session variable if you want it to persist between invocations. – Barmar Mar 16 '20 at 19:01
  • And you're overwriting `$allusernames` each time, not appending to it. – Barmar Mar 16 '20 at 19:02

1 Answers1

0

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.

Everett
  • 8,746
  • 5
  • 35
  • 49
  • Hey, sorry I forgot to reply! Thank you so much for your tips, but when I do some inputs and run print_r() is says: Array ( [up_conf] => val1 ) – RtHAitP30D Mar 24 '20 at 15:08
  • If you are doing `print_r($_POST);` and that's all you're seeing, then that would suggest that you're submitting the form that defines the `up_conf` checkbox, and the fact that the value came through tells me that the checkbox was checked (because the values for checkbox inputs are not included if the box is not checked). Remember, a form only submits the inputs that have been defined inside of that form. This is one of the reasons why I encouraged you to only put 1 form on the page when you are debugging. Hope that helps! Remember to upvote any comments or answers that are helpful. – Everett Mar 24 '20 at 22:19