php,ctype_print不能处理$ _POST变量

时间:2015-10-01 12:38:51

标签: php html post

所以我是php的新手,我正在尝试使用ctype_print($_POST["commentContent"])在我的html表单上做一个简单的输入过滤器,但它没有识别非法输入。但是当我使用ctype_print($testCase)时,它正在发挥作用。如何ctype_print()输入值$_POST。我在这里缺少什么?

我的代码: Html表格:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>My Recipe Site</title>
    <link rel="stylesheet" type="text/css" href="mystyle.css"> 
  </head>
  <body>
    <h3>Comments:</h3>
    <form action="" method="POST">
      <textarea rows="10" cols="30" name="commentContent"></textarea>
      <br />
      <input type="submit" value="Post!">
      <br />
    </form>
  </body>
</html>

以下是我在$ testcase中使用的示例字符串,我发布到表单:“asdf \ n \ r \ t”

简单如果php

$testCase = "asdf\n\r\t";

if (ctype_print ( $_POST ["commentContent"] )) {
    echo "Great succes for: " . $_POST ["commentContent"];
} else {
    echo "DAMN, it dosent work: " . $_POST ["commentContent"];
}

echo "<br>";

if (ctype_alnum ( $testCase )) {
    echo "ctype on testcase works : " . $testCase;
} else {
    echo "ctype on testcase does not work : " .  $testCase;
}

1 个答案:

答案 0 :(得分:1)

你的代码看起来很好我认为主要的问题是,你只需在评论框中输入字符,所以它只是文本,你可以用简单的file_put_contents()来测试它。如果在textarea中键入字符串asdf\n\r\t,您将获得一个内容为“asdf \ n \ r \ t”的文件。

如果您输入(例如)asdf≤ENTER><ENTER>,您将获得一个包含以下内容的文件:

asdf
// newline
// newline

所以我认为你的代码有效。

<?php
$testCase = "asdf\n\r\t";

file_put_contents( "test.txt", $_POST ["commentContent"] );
    if ( ctype_print( $_POST ["commentContent"] ) ) {
    echo "every character in text will actually create output: " . $_POST ["commentContent"];
} else {
    echo "text contains control characters or characters that do not have any output or control function at all: " . $_POST ["commentContent"];
}

echo "<br>";

if ( ctype_alnum( $testCase ) ) {
    echo "every character in text is either a letter or a digit: " . $testCase;
} else {
    echo "NOT EVERY character in text is either a letter or a digit: " . $testCase;
}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>My Recipe Site</title>
        <link rel="stylesheet" type="text/css" href="mystyle.css">
    </head>
    <body>
        <h3>Comments:</h3>
        <form action="" method="POST">
            <textarea rows="10" cols="30" name="commentContent"></textarea>
            <br />
            <input type="submit" value="Post!"> <br />     </form>

    </body>
</html>

更新:更改echo输出以澄清。 echo输出现在返回方法(ctype_print(),ctype_alnum())描述取自PHP DOCS的情况为真或假