PHP 里面 HTML 不工作

PHP inside HTML not working

为防止被误解为“重复问题”,这与here不是同一个问题。由于 PHP 配置没问题(phpinfo 功能有效,WordPress 开发安装也有效)。但我正在试验一些小脚本,它们可能会帮助我生成密码、用于测试目的或只是为了好玩。

这是用于生成密码的脚本(混合 HTML 和 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>

问题是,echo 语句不起作用。什么都不会生成。函数是从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借来的,没有报任何问题,代码看起来很干净。

是什么导致脚本不起作用?它只是唯一一个不起作用的脚本:( 引号生成器完美运行,WordPress 就像一个魅力。只有这个小脚本似乎被破坏了。

你检查错了$_GET,if(isset($_GET['psnum']))应该是这样的:

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

所以,这个条件:

if($pswnum != null)

永远是 false,永远不会进入它到达你的 for 循环;