WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 14751
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  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);
}

?>



Complete, simple working example of login screen and check on a unique page using php functions, cookies and mysql database.
Categories : PHP, Cookies, MySQL, HTML and PHP, Authentication
HTTP Basic Authentication via POP3.
Categories : Authentication, HTTP, Email, PHP
user-authentication with php3 and msql
Categories : Authentication, mSQL, PHP, Databases
AUTH (.htaccess style) - a login system that uses PostgreSQL.
Categories : PHP, Authentication, Databases, PostgreSQL
Authentication HTTP protocol POST
Categories : Authentication, HTTP, PHP
PHP3 Authentication using MySQL (also has nice query_db() function to return an array of arrays from a MySQL query result).
Categories : Authentication, MySQL, PHP
Basic Authentication with sessions
Categories : PHP, Beginner Guides, Authentication, Form Processing, Sessions
Simple and fast user authentication
Categories : PHP, PHP Classes, Authentication
Password using php, Javascript, and html form
Categories : Security, PHP, Authentication, Java Script
Form Security - Match A Value For Success
Categories : PHP, Authentication, HTML and PHP, Sessions, Security
PHP4 MYSQL Authentication Script with cookie. Short & Sweet
Categories : Authentication, Apache, Cookies, PHP, MySQL
Password protection for Phorum 3.1.x with userlevels and log.
Categories : PHP, MySQL, Authentication, Security
Authenticator for Exchange Server LDAP
Categories : PHP, Authentication, LDAP, Security, Sessions
PHP4 AND MySQL Authentication
Categories : PHP, MySQL, Authentication, Databases
Basic WWW-Authentication example
Categories : PHP, Authentication