Jump to content

Cant seem to validate passwords


slannesh

Recommended Posts

Hi. I am new to PHP and  I'm trying to check if the passwords entered in are not validating properly. Basically its supposed to see if the user typed in the password correctly on both text boxes.

 

I get no errors but when I submit info from one page to the other, even if the passwords match it still says that the "Password is not typed in correctly".

 

submit.php

<?php

$username = $_POST['username'];

$password = $_POST['password'];

$re_password = $_POST['re_password'];

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

 

if ($username = '') {

echo "Username is blank";

}

elseif ($password = '') {

echo "Password is blank";

}

elseif ($password == $re_password) {

echo "Password is not typed in correctly";

}

elseif ($firstname = '') {

echo "First name is blank";

}

elseif ($lastname = '') {

echo "Last name is blank";

}

else

echo "WOOT";

?>

 

 

 

new_user.html

<head>

<meta http-equiv="Content-Type" content="test/html; charset=utf-8" />

<title>Prototype_NewUser</title>

<link href="./main.css" rel="stylesheet" type="text/css"></link>

</head>

<body>

 

<form action='./submit.php' method='post'>

<label for="username">User name:</label>

<input type="text" id="username" name="username" /><br />

<label for="password">Password:</label>

<input type="text" id="password" name="password" /><br />

<label for="re_password">Re-enter password:</label>

<input type="text" id="re_password" name="re_password" /><br />

<label for="firstname">First Name:</label>

<input type="text" id="firstname" name="firstname" /><br />

<label for="lastname">Last Name:</label>

<input type="text" id="lastname" name="lastname" /><br />

<input type='submit' name='submit' value='submit'>

 

</form>

 

</body>

</html>

Link to comment
Share on other sites

if ($username = '') {

 

A single equal sign is an assignment.  What your doing there is assigning an empty value to $username, rather than checking to see if it is empty.  You need to use double-equals to test for a condition:

if ($username == ''){ 

 

Link to comment
Share on other sites

<?php
//...


elseif ($password == $re_password) {
echo "Password is not typed in correctly";
}


//...
?>

 

 

 

Assuming the above code is checking that the password matches the re-typed password field, you'll want to display an error if they don't match. To do that, you would use not equals:

 

<?php
//...


elseif ($password != $re_password) {
echo "Password is not typed in correctly";
}


//...
?>

 

 

 

Also, I would recommend checking out the trim() function:

http://php.net/manual/en/function.trim.php

 

Using trim() on the form input helps prevent errors from visitors accidentally typing a space before/after the username for example.

 

<?php
$username = trim($_POST['username']);
?>

Link to comment
Share on other sites

slannesh, before you start coding something I believe that reading the manual is still a good practice... Specifically to your code, you should check out how PHP operators work. I don't mean to be rude, but you should do your homework before asking for tips or help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.