Programming Basics

  Home arrow Programming Basics arrow Page 36 - 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 - User Authentication


    (Page 36 of 37 )

    In PHP the greatest use of the 'md5()' function is for user authentication. Rather than store a user's password on the server, many programmers opt to store only an md5 hash of the password. This way of doing things is much more secure than storing the password itself.

    In order to implement this technique, you must store an md5 hash of the user's password when they first register or sign up for a login. Then, every time they log into the web site, you generate an md5 hash of the password they entered to login and compare it against the stored value. If they match, access is granted. Otherwise, it is not.

    Here is a simple script that illustrates how this could be done. This script assumes that a form has been submitted and that the password the user entered is stored in the '$_POST['password']' variable. The value that we will set into the '$storedpassword' variable is an md5 has of the word 'password'.

    <?php
    $storedpassword 
    '5f4dcc3b5aa765d61d8327deb882cf99';

    if(
    md5($_POST['password']) == $storedpassword) {
        echo "Access is granted!";
    } else {
        echo "Bad Password.";
    }
    ?>

    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 11 - Follow our Sitemap