Solved

check passwords

Posted on 2014-11-10
3
63 Views
Last Modified: 2014-11-18
Im a bit stuck how to sort a problem Ive been given. Ive been asked to write a function to check a password, Ive written everything else but stuck on the rule
No 4 conservative characters from your previous 2 passwords

So something like a new password "banannas" that checks with "bana332" will fail cause "bana" exists.

Ive got the last 2 entries of passwords stored in an array, but stuck how to compare the passwords with every option.

Anyone got any pointers or sample code?????
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40433479
You can convert each of the strings to ann array with

$pass1=str_split($str1);
pass2=str_split($str2);

Loop through the array for the new pass word and compare each character to the contents of the old password with: in_array();

Cd&
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40433815
No 4 conservative characters from your previous 2 passwords
Do you mean consecutive characters?  Do you have the previous 2 passwords at hand (perhaps stored in a data base table)?  If you do it's quite likely that an exposure of your data base would reveal all of the client passwords, which is why we do not store passwords in clear text.

Please tell us a little more about the assignment.  It sounds like there is something fishy going on.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40434001
I don't have time to monitor this one tonight, so here is my take on a possible code example and test case.  Best of luck with it, ~Ray
http://iconoun.com/demo/temp_tonelm54.php

<?php // demo/temp_tonelm54.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28554387.html
// REF: http://php.net/manual/en/function.in-array.php
// REF: http://php.net/manual/en/function.substr.php

function in_substring($needle, $haystack, $length=4, $case=TRUE)
{
    // IF CASE-INSENSITIVE, NORMALIZE THE DATA PRIOR TO COMPARISON
    if (!$case)
    {
        $needle   = strtoupper($needle);
        $haystack = strtoupper($haystack);
    }

    // MAKE THE SUBSTRINGS OF THE NEEDLE
    while ($needle_parts[] = substr($needle, 0, $length))
    {
        $needle = substr($needle, 1);
        if (strlen($needle) < $length) break;
    }

    // MAKE THE SUBSTRINGS OF THE HAYSTACK
    while ($haystack_parts[] = substr($haystack, 0, $length))
    {
        $haystack = substr($haystack, 1);
        if (strlen($haystack) < $length) break;
    }

    // LOOK FOR NEEDLE PARTS IN THE HAYSTACK PARTS
    foreach ($needle_parts as $needle_part)
    {
        if (in_array($needle_part, $haystack_parts))
        {
            return TRUE;
        }
    }
    return FALSE;
}

// TEST CASES
$tests = array
( // FIND abc IN ABCDE case-insensitive
  (object)array
  ( 'needle'=> 'abc'
  , 'haystack' => 'ABCDE'
  , 'len' => 3
  , 'case' => FALSE
  )
, // FIND abc IN ABCDE case-Sensitive
  (object)array
  ( 'needle'=> 'abc'
  , 'haystack' => 'ABCDE'
  , 'len' => 3
  , 'case' => TRUE
  )
, // FIND thump IN thing
  (object)array
  ( 'needle'=> 'thump'
  , 'haystack' => 'thing'
  , 'len' => 4
  , 'case' => TRUE
  )
, // FIND th IN thing
  (object)array
  ( 'needle'=> 'th'
  , 'haystack' => 'thing'
  , 'len' => 2
  , 'case' => TRUE
  )
, // FIND bana IN Bananas Case-insensitive
  (object)array
  ( 'needle'=> 'BANA'
  , 'haystack' => 'BaNaNas'
  , 'len' => 4
  , 'case' => FALSE
  )
, // FIND bana IN Bananas Case-Sensitive
  (object)array
  ( 'needle'=> 'bana'
  , 'haystack' => 'Bananas'
  , 'len' => 4
  , 'case' => TRUE
  )
)
;
foreach ($tests as $test)
{
    echo PHP_EOL . '<br>';
    echo $test->needle;
    echo ' IN ';
    echo $test->haystack;
    echo '? ';
    echo "(LEN=$test->len, CASE=$test->case) ";
    echo '<b>';
    var_dump(in_substring($test->needle, $test->haystack, $test->len, $test->case));
    echo '</b>';
    echo PHP_EOL;
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question