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条)

报告相同问题?