WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Function to remember password
Categories : PHP, Authentication, Personalization and Membership Click here to Update Your Picture
Jose Santos
Date : May 26th 2004
Grade : 4 of 5 (graded 6 times)
Viewed : 10583
File : 3889.php
Images : No Images for this code example.
Search : More code by Jose Santos
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Hi !

This script assumes :
- The database has two tables : users and people (Structure bellow).
users to store user information (login, password, ...) and people which is used as the user profile.
The DB is based on the DB package of PEAR (http://pear.php.net).

- When creating a user, a profile is created by default.
(create a registration in the people table and update the registration of this user in the users table with the id of the inserted people registrar).

- The passwords stored in the database are encrypted using the crypt() function, and you can't discover this password.

CREATE TABLE USERS(
  ID              NUMBER(10)                    NOT NULL,
  LOGIN           VARCHAR2(15 BYTE)             NOT NULL,
  PASSWORD        VARCHAR2(50 BYTE)             NOT NULL,
  LOGINEXPIRE     DATE,
  PASSWORDEXPIRE  DATE,
  IDPEOPLE        NUMBER(10),
)

CREATE TABLE PEOPLE(
  ID              NUMBER(10)                    NOT NULL,
  NAME            VARCHAR2(80 BYTE),
  FROM            VARCHAR2(200 BYTE),
  EMAIL           VARCHAR2(50 BYTE),
  HOMEPAGE        VARCHAR2(80 BYTE)
)

Someone regists of these tables

Users:
======

282 | filipe     | $1$DqQoFOw/$rF1NZCmRe5BuIOJglUF9Z/ | 01-01-2003 | 01-01-2003 | 302
281 | ernesto | $1$6ugrvDyI$NWDsSJXOsYwtVXVG5Zilg0  | 01-01-2004 | 01-01-2004 | 301

People:
=======

301  |                           |    |                   |     (comments: this is a empty profile)
302  | Jos? Filipe Lopes Santos  | R. Conde S. Bento, Santa Cristina do Couto, Santo Tirso | jfilipe@med.up.pt | http://users.med.up.pt/jfilipe



What does this script do?
- Verify if a user with the supplied credentials exists.
- Verify if this user has a profile (has an people registration, that contains extended information for this user (name, email, home, ...)).
- Generate a new random password.
- Encrypt this password.
- Update the new password in the database.
- Send the new password (not encrypted) to the user.


<?php
/**
* Remember password.
*
* This function remember the password
*
* @return array(error message,sucess message,mensage code)
* @param string $dsn dsn to access to database
* @param string $login user login
* @param string $session_name session name
* @desc Remember password.
*/

function RememberPassword($dsn,$login,$session_name){

   
$error_msg="";
   
$sucess_msg="";
   
$exist = false; // bool that indicates if user exist
   
$id_link = -1; // id of this people
   
$email = ""; // user email
   
$exist_email = false; // bool (true = have email)
   
$chars_random = "123456789abcdefghijlmnopqrstuvwxz"; // characters to use to generate ramdom password
   
$random_pass = ""; // random password
   
$enc_random_pass = ""; // random passwod encrypted
   
$full_name = ""; // user full name
   
    // estabilish conection to database
   
$db = DB::Connect($dsn);
   
   
// verify if that user exist and, have an id to link to people
   
$sql = "select idpeople from users where login='".$login."'";
   
$st = $db->query($sql);
   
    if (
DB::isError($st)){ // ocurred error? catch this
       
$db->disconnect();
       
$error_msg = $st->getUserInfo();
        return array(
$error_msg,$sucess_msg);   
    }
   
    if (
$row = $st->fetchRow()){
        if (
$row[0] != "") $id_link = $row[0];
       
$exist = true;       
    }
   
   
$st->free();
   
    if (!
$exist){ // the user dont exist
       
$db->disconnect();
       
$error_msg = "Invalid user";
        return array(
$error_msg,$sucess_msg);       
    }
   
    if (
$id_link == -1){ // dont exist id to link to people
       
$db->disconnect();
       
$error_msg = "These user, dont had link to people table";
        return array(
$error_msg,$sucess_msg);
    }
   
   
// verify if exist these id link
   
$sql2 = "select count(*) from people where id=".$id_link;
   
$count = $db->getOne($sql2);
   
    if (
DB::isError($count)){
       
$db->disconnect();
       
$error_msg = $count->getUserInfo();
        return array(
$error_msg,$sucess_msg);   
    }
   
    if (
$count == 0){ // dont exis any people with this id
       
$db->disconnect();
       
$error_msg = "Dont have any regist in people to this user";
        return array(
$error_msg,$sucess_msg);
    }
       
   
// verificy if had introduced email
   
$sql3 = "select email,name from people where id=".$id_link." and email is not null";
   
$st2 = $db->query($sql3);
   
    if (
DB::isError($st2)){
       
$db->disconnect();
       
$error_msg = $st2->getUserInfo();
        return array(
$error_msg,$sucess_msg);   
    }
   
    if (
$row = $st2->fetchRow()){
       
$email = $row[0];   
       
$full_name = $row[1];
       
$exist_email = true;
    }
   
   
$st2->free();
   
    if (!
$exist_email){ // email not specified
       
$db2->disconnect();
       
$error_msg = "Email not specified";
        return array(
$error_msg,$sucess_msg);       
    }
       
   
// generate random password
     
for ($j=0;$j<=10;$j++)
       
$random_pass .= $chars_random[rand(0,strlen($chars_random))];
       
   
// sen the new pass to user
     
$subject = $session_name.": login and password to access";

     
$message = "Dear $full_name, we had received your request to send password\n\n";
     
$message .= "\tlogin : $login\n";
     
$message .= "\tPassword : $random_pass\n\n";
     
$message .= "Please, change your password for your security";

     
mail($email,$subject,$message);

     
// encrypt the random password
     
$enc_random_pass = crypt($random_pass);
   
     
// update password in database
     
$update = "update users set password=? where login=?";
     
$data = array($enc_random_pass,$login);
     
$prep = $db->prepare($update);
     
$exe = $db->execute($prep,$data);
     
      if (
DB::isError($exe)){
         
$db->disconnect();
         
$error_msg = $exe->getUserInfo();
          return array(
$error_msg,$sucess_msg);   
      }
     
     
// close conection to database
     
$db->disconnect();
       
     
$sucess_msg = "Your password had sended to your email";   
    return array(
$error_msg,$sucess_msg);
}

?>



redirect redirection ip address authentication authenticate addr
Categories : Authentication, HTTP, Network, PHP
Is there some possibility to link a database to an htaccess file, so that instead of having a passwd file you would have a database with DES-crypted password and username fields?
Categories : Authentication, PHP, General SQL, Databases
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
Simple Password example
Categories : PHP, Authentication, Security, HTTP
phpAddQuote v1.2 - UPDATED! Lets users add their own quotes to your website. You specify how many quotes appear on the page at a time. Easier install!
Categories : HTML and PHP, Complete Programs, PHP, Databases, Personalization and Membership
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
HTTP Basic Authentication via POP3.
Categories : Authentication, HTTP, Email, PHP
Import the yahoo address book.
Categories : PHP, CURL, Authentication
Authentication script to authenticate users in Active Directory through LDAP.
Categories : LDAP, Authentication, Cookies, PHP
MD5 secured login
Categories : PHP, Java Script, Authentication, Security
A simple PHP login script that you can modify to suite your needs. It use a session to store data in a session file submited by the page.
Categories : PHP, Sessions, Security, Authentication
Implementing a "Members ONLY" area
Categories : PHP, MySQL, Databases, Authentication
SQL / PHP based Integrated Authentication
Categories : PHP, Authentication, Databases, MySQL
PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character.
Categories : PHP, Algorithms, Security, Authentication, Encryption
user-authentication with php3 and msql
Categories : Authentication, mSQL, PHP, Databases