Question

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>
Was it helpful?

Solution

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>

OTHER TIPS

That logic should be correct. If by 'next page' you mean the page that your PHP script is executing on, then that is the correct behavior.

If you would like to stop the current page from loading, try this instead.

<?php
$username = $_POST['username'];
$password = $_POST['password'];
if($_POST['password'] !== strrev($_POST['username'])) {
    header('Location: ' . $_SERVER['HTTP_REFERER']);
}
else {
    echo 'Successful';
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top