0

To prevent from misunderstanding as “duplicate question”, this is NOT the same question as here. Since PHP configuration is all right (phpinfo function works and WordPress developing installation works as well). But I'm experimenting with little scripts, that may help me with password generation, for testing purposes, or just for fun.

This is script for generating passwords (mixed HTML with PHP):

<?php
    global $alpha;
    global $echo;

    $alpha_lower = 'abcdefghijklmnopqrstuvwxyz';
    $alpha_upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $alpha_nums  = '0123456789';
    $alpha_spec  = '$ß\'"_-*/\\!§()[]{}';
    $alpha = "";
    $echo = "";
    $upper = null;
    $lower = null;
    $numbers = null;
    $special = null;
    $pswlen = null;
    $pswnum = null;

    if(isset($_GET['upper'])) {
        $upper = $_GET['upper'];
    }

    if(isset($_GET['lower'])) {
        $lower = $_GET['lower'];
    }

    if(isset($_GET['numbers'])) {
        $numbers = $_GET['numbers'];
    }

    if(isset($_GET['special'])) {
        $special = $_GET['special'];
    }

    if(isset($_GET['pswlen'])) {
        $pswlen = $_GET['pswlen'];
    }

    if(isset($_GET['psnum'])) {
        $pswnum = $_GET['pswnum'];
    }

    if($lower != null) {
        $alpha .= $alpha_lower;
    }
    if($upper != null) {
        $alpha .= $alpha_upper;
    }
    if($numbers != null) {
        $alpha .= $alpha_nums;
    }
    if($special != null) {
        $alpha .= $alpha_spec;
    }

    function generatePassword($length = 8, $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$ß\'"_-*/\\!§()[]{}') {
        $chars  = $alphabet;
        $count  = mb_strlen($chars);
        $result = "";

        for ($i = 0, $result = ''; $i < $length; $i++) {
            $index = rand(0, $count - 1);
            $result .= mb_substr($chars, $index, 1);
        }

        return $result;
    }

    if($alpha != "") {
        if($pswnum != null) {
            if($pswlen != null) {
                for($i = 0; $i < (int)$pswnum; ++$i) {
                    $echo .= generatePassword($pswlen, $alpha);
                    if($i < $pswnum - 1) {
                        $echo .= "<br />";
                    }
                }
            }
        }
    }
?>
<!DOCTYPE HTML>
<html lang="cs-CZ" type="text/html">
    <head>
        <meta charset="utf-8" />
        <title>Generátor hesel</title>
        <style type="text/css">
            .passwords {
                font-family: Consolas, Courier New, Courier, monospace;
                font-size: 12pt;
                font-weight: bold;
            }
        </style>
    </head>
    <body>
        <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            Heslo má mít tyto znaky:
            <label for="lower"><input type="checkbox" name="upper" id="lower" checked /> Malá písmena</label>
            <label for="upper"><input type="checkbox" name="lower" id="upper" checked /> Velká písmena</label>
            <label for="numbers"><input type="checkbox" name="numbers" id="numbers" checked /> Číslice</label>
            <label for="special"><input type="checkbox" name="special" id="special" checked /> Speciální znaky</label><br />
            Heslo má být takto dlouhé:
            <input type="number" name="pswlen" value="15" /><br />
            Hesel má být tolik:
            <input type="number" name="pswnum" value="5" /><br />
            <input type="submit" value="Vygeneruj!" />
        </form>
        <p class="passwords"><?php echo $echo; ?></p>
    </body>
</html>

The problem is, that the echo statement doesn't work. Nothing gets generated. The function was borrowed from here (original function was pretty much like same, though, just using another sources). File is saved with .php extension, so it should work. But it doesn't. I tried also make the important variables global (maybe in the wrong place). Nothing seems to work. I ran the code through validator, no single issue was reported, the code seems clean.

What makes the script not working? It's just the only one script that doesn't work :( Quotes generator works perfectly and WordPress just like a charm. Just this one little script seems be broken.

Community
  • 1
  • 1
Polda18
  • 162
  • 14
  • 2
    As echo is most propably working: Use var_dump($echo) to make sure that the variable is not empty – Hossam Mar 26 '17 at 13:27
  • 1
    you could check ` if($alpha != "") { if($pswnum != null) { if($pswlen != null) {` which one of these is failing.. echo your variables and debug – Suraj Rao Mar 26 '17 at 13:29
  • your alpha/pswlen is empty, no password will be generated if so. check your function behavior – Roljhon Mar 26 '17 at 13:30
  • Holy s**t :D `var_dump($pswnum);` revealed the cause of malfunction: Ommited letter `w` in `$_GET['pswnum']` :D My keyboard sure needs fixing :D – Polda18 Mar 26 '17 at 13:41

1 Answers1

2

you are checking the wrong $_GET , if(isset($_GET['psnum'])) should be as follows :

if(isset($_GET['pswnum'])) {
//                ^
    $pswnum = $_GET['pswnum'];
}

so , this condition :

if($pswnum != null)

will always be false and never will get inside it to reach your for loop;

hassan
  • 7,812
  • 2
  • 25
  • 36