Results 1 to 7 of 7

Thread: Day 17 - Include: Easy cut and paste for Programmers - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 17 - Include: Easy cut and paste for Programmers - Become PHP Expert in 30 days

    Include: Easy cut and paste for Programmers

    In Day 15 and Day 16, we used same code for connecting to MySQL database. We have cut and paste same code two times. Imagine, we done this for all the PHP pages of our application, that will be lot of cut and paste.

    What if your MySQL user name or password changed ? You have to go through all the PHP files and change MySQL username and password.

    PHP Code:
    $db_server '127.0.0.1';
    $db_user 'fwhphp_user';
    $db_password 'k5BJRaX6SFbs';
    $db_name 'fwhphp_db'
    Don't repeat yourself (DRY)

    One of the programming concept of effective programming is Don't repeat yourself. We can use PHP functions and Includes to accomplish this.

    We have not covered functions yet, this is very important when you make large programs. Functions allow you to create your own custom functions, that do a specific task. We will use them in coming days.

    For now, lets learn include

    include

    include allows you to include another PHP program in your PHP program.

    If we moved the Database connection related stuff to its own file, for example database.php and include it in all our PHP programs, we don't have to cut and paste MySQL server user name and passwords in each file. This allow us to change the Database login details in one place.

    It is good practice to create a folder "includes" and put all your include files in it.

    So lets create file

    includes/database.php

    PHP Code:
    <?php

    $db_server 
    '127.0.0.1';
    $db_user 'fwhphp_user';
    $db_password 'k5BJRaX6SFbs';
    $db_name 'fwhphp_db';

    $mysqli = new mysqli($db_server$db_user$db_password$db_name);

    if (
    $mysqli->connect_errno) {
        echo 
    'Connect failed: ' $mysqli->connect_error;
        exit();
    }
    Now, edit following files

    1. signup.php
    2. lost_password.php

    Find

    PHP Code:
    $db_server '127.0.0.1';
    $db_user 'fwhphp_user';
    $db_password 'k5BJRaX6SFbs';
    $db_name 'fwhphp_db';

    $mysqli = new mysqli($db_server$db_user$db_password$db_name);

    if (
    $mysqli->connect_errno) {
        echo 
    'Connect failed: ' $mysqli->connect_error;
        exit();

    Replace it with

    PHP Code:
    include "includes/database.php"
    Now your programs still work. If you ever need to change database login details, just update includes/database.php file.

    require

    require is same as include.

    require is better than include as it will die if includes/database.php file not found. So it is better use require in your programs.

    Find

    PHP Code:
    include "includes/database.php"
    Replace With

    PHP Code:
    require "includes/database.php"
    Put all Includes at top

    It is good to have all your include/require at top of a program. So you know which other program files are included in your program. So updated lost_password.php file will look like.

    PHP Code:
    <?php

    include "includes/database.php";

    if (isset(
    $_POST['email'])) {
        
    $email $_POST['email'];
    } else {
        die(
    'Please enter your email address');
    }

    // now we have $email variable.
    // $email can contain space if user enter white space..
    // So lets remove any white space char using trim() function.

    $email trim($email);

    // lets write an SQL statement to search our users table
    // to find the user having email address == $email

    $sql "select * from users where email='$email'";

    // In above line, we used single quotes between email, because it is a string.

    // Now time to execute the SQL statement.
    $result $mysqli->query($sql);

    // lets see how many records found in database
    // If num_rows is 0, no user found

    if ($result->num_rows == 0) {
        die(
    "User with email address <b>$email</b> not found");
    }

    // lets get the user record from MySQLi result

    $userInfo mysqli_fetch_assoc($result);

    // $userInfo is an associate array.
    // Lets print it.
    // Now you can see all information about the user, including password.

    echo '<pre>';
    print_r($userInfo);

    // For real life application, we will sent password to users email
    // address instead of printing it to browser.
    // But for now, this is fine for us.



    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/

    The password has been sent to the e-mail address specified

    PHP Code:
     <?php

    include "includes/database.php"

    if (isset(
    $_POST['email'])) {
        
    $email $_POST['email'];
    } else {
        die(
    'Please enter your email address');
    }

    $email trim($email);

    $sql "select * from users where email='$email'";

    $result $mysqli->query($sql);

    if (
    $result->num_rows == 0) {
        die(
    "User with email address <b>$email</b> not found");
    }

    $userInfo mysqli_fetch_assoc($result);

    $name $userInfo['name'];
    $password $userInfo['password'];
    mail($email,"Your password""Hello $name, your password is $password");

    echo 
    'The password has been sent to the e-mail address specified';

  3. #3
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/lost_password.html

    The password has been sent to the e-mail address specified

    Code:
    <?php
    
    include "includes/database.php"; 
    
    if (isset($_POST['email'])) {    
       $email = $_POST['email'];
    } else {    
       die('Please enter your email address');
    }
    
    $email = trim($email);
    
    $sql = "select * from users where email='$email'";
    
    $result = $mysqli->query($sql);
    
    if ($result->num_rows == 0) {    
    die("User with email address <b>$email</b> not found");
    
    }
    $userInfo = mysqli_fetch_assoc($result);
    
    $name = $userInfo['name'];
    $password = $userInfo['password'];
    mail($email,"Your password", "Hello $name, your password is $password");
    echo 'The password has been sent to the e-mail address specified';

  4. #4
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    http://php.flashwebhost.com/stefin/lost_password.php
    New Password send to your email

    This one was pretty good and interesting, i like copy pasting

    PHP Code:


    <?php
    include "includes/database.php";  

    if (isset(
    $_POST['email'])) {    $email $_POST['email'];} else {    die('Please enter your email address');}
    $email trim($email);


    $sql "select * from users where email='$email'";
    $result $mysqli->query($sql);

    if (
    $result->num_rows == 0) {    die("User with email address <b>$email</b> not found");}
    $userInfo mysqli_fetch_assoc($result);
    $name $userInfo['name'];$password $userInfo['password'];mail($email,"Your password""Hello $name, your password is $password");
    echo 
    'New Password send to your email';
    Last edited by stefin; 08-03-2014 at 02:29 AM.

  5. #5
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Nice. We can keep frequently used code in another file and include them as needed.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  6. #6
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default

    http://php.flashwebhost.com/sibichan..._password.html

    Yes, I got the mail with password

    I used 'require' in lost_password.php

    PHP Code:

    <?php
    require "includes/database.php";  
    if (isset(
    $_POST['email'])) {

        
    $email $_POST['email'];

    } else {

        die(
    'Please enter your email address');}
    // now we have $email variable.

    // $email can contain space if user enter white space..

    // So lets remove any white space char using trim() function.
    $email trim($email);
    // lets write an SQL statement to search our users table

    // to find the user having email address == $email
    $sql "select * from users where email='$email'";
    // In above line, we used single quotes between email, because it is a string.
    // Now time to execute the SQL statement.

    $result $mysqli->query($sql);
    // lets see how many records found in database

    // If num_rows is 0, no user found
    if ($result->num_rows == 0) {

        die(
    "User with email address <b>$email</b> not found");

    }
    // lets get the user record from MySQLi result
    $userInfo mysqli_fetch_assoc($result);
    // $userInfo is an associate array.

    // Lets print it.

    // Now you can see all information about the user, including password.
    $name $userInfo['name'];

    $password $userInfo['password'];

    mail($email,"Your password""Hello $name, your password is $password");

    echo 
    'The password has been sent to the e-mail address specified';

  7. #7
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Good, require is better, but used include as everyone understand the meaning of the word and is easy to relate to what it do - include code from other file.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •