dtvpl739577 2015-10-01 12:38
浏览 23

php,ctype_print不能处理$ _POST变量

So I'm new to php and I'm trying to do a simple input filter on my html form with ctype_print($_POST["commentContent"]) but it is not recognising illegal input. But when I use ctype_print($testCase) it is working. How can I do ctype_print() on a $_POST input value. What am I missing here?

My code: Html form:

<!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>

Here is the sample string i use in both $testcase and that i post to the form: "asdf \t"

Simple if php

$testCase = "asdf
\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条回答 默认 最新

  • doupaxia2478 2015-10-01 13:48
    关注

    Your code looks fine to me I think the main problem is, that you just type the characters into the comment box, so it is just text, you can test it with a simple file_put_contents(). If you type into the textarea the string asdf \t you will get a file with the content `asdf \t.

    If you type in (for example) asdf≤ENTER><ENTER> you will get a file with the content:

    asdf
    // newline
    // newline
    

    So I think your code works.

    <?php
    $testCase = "asdf
    \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>
    

    UPDATED: changed the echo output to clarify. The echo output now gives back the method ( ctype_print(), ctype_alnum() ) description taken from the PHP DOCS for the cases true or false

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?