doudi5524 2014-04-18 21:18
浏览 24
已采纳

如何在PHP中使用strrev()检查字符串是否已反转

Now I understand how to reverse a string in PHP:

<?php
echo strrev("Hello world!"); // outputs "!dlrow olleH"
?>

However, I have searched high and low and cannot figure out how to do what I want to do. (This is for a class project fyi)

I need to check that the password is the exact reverse of the username. So the user may choose whatever they want, but their password must be the exact reverse in order for them to proceed and log into the page.

EX: username: me Password: em

I do not need to store this in a file, just hard code it into the PHP script and have it check to make sure the one text field is the reverse of the other.

In theory (and I'm really new to this), I was thinking I should do something like this:

<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo ($_POST['password'] === strrev($_POST['username'])) ? 'true' : 'false'; // check    that password is reverse of username
?>

However, this is obviously not right (or I wouldn't be here). No matter what I put it, it is allowing the user to go on to the next page.

Full code:

<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo ($_POST['password'] === strrev($_POST['username'])) ? 'true' : 'false'; // check that password is reverse of username
?>

<html>
<head></head>
<body>
<form action="amanot.php" method="post">
<table>
<tr>
    <td><label>Username: </label></td>
    <td><input type="text" name="username" id="username"/></td>
</tr>
<tr>
    <td><label>Password: </label></td>
    <td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
    <td><label>Submit</label></td>
    <td><input type="submit" name="submit" id="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • drdyszuy488152 2014-04-18 21:32
    关注

    Edit

    I noticed you used my (original) answer in its entirety, but named it as an .html extension. That will not work.

    Use two seperate files. One as .html for the form and the other as amanot.php for the action file.

    My original answer had action="" instead of what you were using, plus there was a \ at the end of .../amanot.php\ in your file --- Try this now:

    HTML form (form.html)

    <html>
    <head></head>
    <body>
    <form action="amanot.php" method="post">
    <table>
    <tr>
        <td><label>Username: </label></td>
        <td><input type="text" name="username" id="username"/></td>
    </tr>
    <tr>
        <td><label>Password: </label></td>
        <td><input type="password" name="password" id="password"/></td>
    </tr>
    <tr>
        <td><label>Submit</label></td>
        <td><input type="submit" name="submit" id="submit"/></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    

    PHP (amanot.php) as a seperate file, not inside the form itself, it will not work.

    <?php
    if(isset($_POST['submit'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    if($password == strrev($username)){
    echo "match"; // replace with header("Location: valid.php"); exit;
    }
    
    else{
    echo "sorry"; // replace with header("Location: invalid.php"); exit;
    }
    
    } // brace for if(isset($_POST['submit']))
    
    ?>
    

    Original answer

    This works:

    <?php
    if(isset($_POST['submit'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    if($password == strrev($username)){
    echo "match"; // replace with header("Location: valid.php"); exit;
    }
    
    else{
    echo "sorry"; // replace with header("Location: invalid.php"); exit;
    }
    
    } // brace for if(isset($_POST['submit']))
    
    ?>
    
    <html>
    <head></head>
    <body>
    <form action="" method="post">
    <table>
    <tr>
        <td><label>Username: </label></td>
        <td><input type="text" name="username" id="username"/></td>
    </tr>
    <tr>
        <td><label>Password: </label></td>
        <td><input type="password" name="password" id="password"/></td>
    </tr>
    <tr>
        <td><label>Submit</label></td>
        <td><input type="submit" name="submit" id="submit"/></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系