how to make password >7 characters (validation)? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a form, but I am having trouble getting the strlen function to work.
Below is an example of the code - there is validation further down.
I've commented out the code that isn't working. Basically, all I want to do with this section of code is determine that the passwords match, and are more than 7 characters long.
Can anyone help?
if (isset($_POST['formName']) && $_POST['formName'] == "addUser") {
if ( ( $_POST['frmName'] != '') &&
($_POST['frmSurname'] != '') &&
($_POST['frmEmail'] != '') &&
($_POST['frmPassword1'] != '') ) {
if ($_POST['frmPassword1'] != $_POST['frmPassword2'] ) {
echo "Passwords do not match!";
}
/* if (strlen( ($_POST['frmPassword1']) < 7 ) {
echo "Passwords much be a minimum of 7 characters";
} */

Look at your ():
strlen( ($_POST['frmPassword1']) < 7 )
a b b a
^-----strlen-------------------^
You're not testing the length of the $_POST value, you're doing strlen on the boolean result of foo < 7, which will always be 0/1:
php > var_dump(strlen(true), strlen(false));
int(1)
int(0)
YOu need:
if (strlen($_POST['frmPassword1']) < 7) {
a b b a
Note the labels on the ().

This is where its messed up:
if (strlen( ($_POST['frmPassword1']) < 7 ) {
Let's start that statement over.
First you want the string represented by form field frmPassword1:
$_POST['frmPassword1']
Then you want the string length:
strlen($_POST['frmPassword1'])
Then you want to compare it to less than 8 because you specifically asked for more than 7 characters. Therefore, your expression would be:
strlen($_POST['frmPassword1']) < 8
Now make that a complete condition like so:
if( strlen($_POST['frmPassword1']) < 8 ){
//insert relevant code here telling users password is too short
}
Now you have a working block of code.

You are missing end )
if (strlen( ($_POST['frmPassword1']) < 7 ) {
1 2 3 3 2 # 1 is missing
So it would be
if (strlen( ($_POST['frmPassword1']) < 7 ) ){
1 2 3 3 2 1
NOTE : In your question you have mentioned that passwords match, and are more than 7 characters. So use <= (less than or equal).

Related

Php floating if statement Confused [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
function is_decimal( $it )
{
return is_numeric( $it ) && floor( $it ) != $it;
}
if (is_decimal == true){
echo "Decimal";
}
This gives me an error even though the int is not a decimal (float). Can someone help me. Thanks
It works fine for a decimal but not for an int.
Simple, when you call a function with a parameter you have to pass a parameter!
function is_decimal( $spaces )
{
return is_numeric( $spaces ) && floor( $spaces ) != $spaces;
}
$tst_var = 1.99
if (is_decimal($tst_var) == true){
echo "Error 3: Decimal causing Error as carpark can't have decimal spaces available.";
}
just use if(is_numeric( $spaces )){ ... } work for int, float even if they are represented by string

Comparison with && and || not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This first comparison will check if the value is 14 in length and the User ID is 1
if ($checkValueLenght === 14 && $UserID[0] === "1") { code... }
but I want it to check if it is 1 or 2, and I cant get this to work ...
if ($checkValueLenght === 14 && $UserID[0] === "1" || "2") { code... }
it goes trough the code whatever letter or number the UserID starts with, but not if it is not exactly 14 in length. What am I doing wrong?
You need this ...
if ($checkValueLenght === 14 && ($UserID[0] === "1" || $UserID[0] === "2")) { code... }
or better, this ...
if ($checkValueLenght === 14 && in_array($UserID[0],["1","2"]) { code... }
assuming that the value of $UserID[0] is indeed a string.

Let me understand this recursive function of PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to understand this recursive function. When i echo out with different numbers it returns different result. I can't understand how this is working actually.
<?php
function head_sum($x) {
return ($x == 1) ? $x : $x + head_sum($x - 1);
}
echo head_sum(5);//15
echo head_sum(2);//3
echo head_sum(3);//6
Your function right now says:
if variable x equals 1 then
return variable x
else
return variable x + this function( variable x -1 )
So this will keep running until $x equals 1. Until then it will keep adding itself.
So when you type echo head_sum(5):
head_sum(5); //$x = 5
head_sum(4); //$x = 5+4
head_sum(3); //$x = 5+4+3
head_sum(2); //$x = 5+4+3+2
head_sum(1); //$x = 5+4+3+2+1 = 15
This can also be represented like this (hope i can show this properly without whiteboard)
function headsum(5){
return 5 + head_sum( 5-1 ){
return 4 + head_sum( 4-1 ){
return 3 + head_sum( 3-1 ){
return 2 + head_sum( 2-1 ){
return 1
}
}
}
}
}
Hope This makes some sense :)
Every time you call head_sum it's called multiple times.
First pass-through goes all the way until the inner head_sum calls itself.
It continues to do so until it decreased by 1 down to 1 at which point the conditional exits out.
So for head_sum(5)
5 goes in and i saved to $x.
Inside recursion calls
head_sum( 5 -1 ) // 4
Then 3
Then 2
Then 1
The ( $x == 1 ) check exits the function after adding the total tally which at this point is incremented to:
5 + 4 + 3 + 2 + 1 = 15

Transform integer to alphanumeric sequence in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm working on a url shortener. I based mine on this one https://github.com/phpmasterdotcom/BuildingYourOwnURLShortener and more or less took the function to create the short codes, because i couldn't come up with an algorithm myself:
<?php
convertIntToShortCode($_GET["id"]); // Test codes
function convertIntToShortCode($id) {
$chars = "123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
$id = intval($id);
if ($id < 1) {
echo "ERROR1";
}
$length = strlen($chars);
// make sure length of available characters is at
// least a reasonable minimum - there should be at
// least 10 characters
if ($length < 10) {
echo "ERROR2";
}
$code = "";
while ($id > $length - 1) {
// determine the value of the next higher character
// in the short code should be and prepend
$code = $chars[fmod($id, $length)] . $code;
// reset $id to remaining value to be converted
$id = floor($id / $length);
}
// remaining value of $id is less than the length of
// self::$chars
$code = $chars[$id] . $code;
echo $code;
}
?>
Although it works, some of my numbers (database id) output strange shortcodes:
1 -> 2
2 -> 3
...
10 -> c
11 -> d
12 -> e
...
Is there any easy way i can modify this code, so that my generated short codes are longer than just one character (at least two or three characters for every shortcode), even for integers like 1, 2, 3 etc.?
Also is there anybody who can tell me, how this algorithm above works to output short codes for integers?
Thanks in advance
What you would like to do is convert that number to a different notation - one that includes both letters and numbers like base 36 which is actually alphanumeric -> a-z + 0-9.
So what you would need to do is:
$string = base_convert ( $number , 10, 36 );
Documentation:
string base_convert ( string $number , int $frombase , int $tobase );

Question about integers (php) [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
PHP: Shortest way to check if a variable contains positive integer?
How do I know, is a given variable a number without residue (and not a negative number)?
Like we have numbers (each number is a variable): 5; 6.416; -76; 254.
Its better to have some function, which would give true or false, like:
number "5" is true
6.416 is false (has residue "416")
-76 is false
254 is true.
Thanks.
if ( (int)$num == $num && (int)$num > 0 ){
return true;
}
This should do it:
function is_positive_int($n) {
return $n && abs(intval($n)) == $n;
}

Categories