Solved

check passwords

Posted on 2014-11-10
3
61 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
  • 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 109

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 109

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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