Programming Basics

  Home arrow Programming Basics arrow Page 21 - PHP Strings Primer
PROGRAMMING BASICS

PHP Strings Primer
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2003-07-11

    Table of Contents:
  • PHP Strings Primer
  • The Basics
  • Single Quotes
  • Double Quotes
  • Heredoc
  • Concatenation
  • Displaying Strings
  • echo
  • print
  • printf
  • Strings Formatting
  • Preparing user input for comparisons
  • Capitalization
  • Reversing strings
  • Padding strings
  • Multiple Lines
  • Data Preparation
  • Adding and Removing Slashes
  • Dealing with HTML Tags and Entities
  • Counting
  • Checking password strength
  • Generating Statistics
  • Substrings (and searching)
  • Extracting Substrings
  • Counting Paragraphs
  • Filtering Words
  • Working with email addresses
  • Manually Stripping Tags
  • Password Strength Revisited
  • Handling URLs and Base64-encoding
  • Parsing URLs
  • Encoding for URLs
  • Encoding for Email
  • Hashing
  • Verifying Integrity
  • User Authentication
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    PHP Strings Primer - Checking password strength


    (Page 21 of 37 )

    Passwords are used everywhere in today's world. Unfortunately, poor passwords are used everywhere also. But, we can do something to help eliminate poor passwords from being used in systems we build. Namely, we can check to make sure they adhere to certain lengths. It is also possible to verify that they don't use the same characters within their password too frequently. Let's take a look at a basic script that will accomplish both of these tasks.

    <?php
    $password 
    'mississippi';
    $length strlen ($password);
    $unique strlen (count_chars ($password3));

    if (
    $length &lt6)
      echo "Sorry. Passwords must be at least 6 characters.";

    $difference $length $unique;
    if (
    $difference &gt2)
      echo "Sorry. Too many of the same characters used.";
    ?>

    The password coded into this script will pass the first check but fail the second. The word mississippi is longer than 6 characters so it is obvious why it passes the first check. The second is not quite so evident, so let's take a closer look. In order to understand what is happening, we must know how the 'count_chars()' function works.

    As you can see, the first parameter passed to the function is the string we want to analyze. The second parameter tells 'count_chars()' how to operate. There are 5 possible modes of operation, each represented by an integer between 0 and 4. Each of the modes is detailed below.

  • 0 -This option returns an array with the ASCII value of the character as they key and the frequency it appeared as the value. All ASCII values from 0 to 255 are returned, regardless of their frequency. This is 'count_chars()' default mode of operation if none is specified.
  • 1 -This mode returns an array similar to mode 0, with the exception that only ASCII values with a frequency of greater than zero are listed.
  • 2 -ASCII values with frequency equal to zero are returned in an array as described in mode 0.
  • 3 -This mode returns a string containing each unique character used in the input string.
  • 4 -This last mode is the opposite of mode 3. It returns a string that contains all characters not used in the input string.

    Now that we understand the different modes that can be used with 'count_chars()', the result from using the function in the above code should be clear. If we were to echo out the result from the 'count_chars()' function call, it would appear as so:

    imps

    The result is the unique characters in the string. So, when we take the length of that result and subtract it from the length of the entire string, we are able to determine that the difference is outside of our threshold of 2.

    There are many other password validation routines that you can run to be sure that passwords are secured. Other common methods are to check for a combination of alpha and numeric characters, ensure that the password does not contain common words, enforce the use of numeric characters in the middle of the password, and a whole host of others. We will examine some of these methods later in this tutorial.

    More Programming Basics Articles
    More By Matt Wade

    blog comments powered by Disqus
  • PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap