• Create a User Login Using PHP Code

    Step 1 – create a table dbUsers with the scripts below:

    1
    2
    3
    4
    5
    6
    7
    8
    
    CREATE TABLE dbUsers(
    id int NOT NULL AUTO_INCREMENT,
    username varchar(16),
    password char(16),
    email varchar(25),
    PRIMARY KEY(id),
    UNIQUE (username)
    )

    Step 2 – create a file dbConfig.php with the codes below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    <?
    // Replace the variable values below
    // with your specific database information.
    $host = "localhost";
    $user = "UserName";
    $pass = "Password";
    $db = "dbName";
     
    // This part sets up the connection to the
    // database (so you don't need to reopen the connection
    // again on the same page).
    $ms = mysql_pconnect($host, $user, $pass);
    if ( !$ms )
    {
    echo "Error connecting to database.\n";
    }
     
    // Then you need to make sure the database you want
    // is selected.
    mysql_select_db($db);
    ?>

    Step 3 – create a file register.php with the codes below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    
    <?php
     
    // dbConfig.php is a file that contains your
    // database connection information. This
    // tutorial assumes a connection is made from
    // this existing file.
    include ("dbConfig.php");
     
     
    //Input vaildation and the dbase code
    if ( $_GET["op"] == "reg" )
    {
    $bInputFlag = false;
    foreach ( $_POST as $field )
    {
    if ($field == "")
    {
    $bInputFlag = false;
    }
    else
    {
    $bInputFlag = true;
    }
    }
    // If we had problems with the input, exit with error
    if ($bInputFlag == false)
    {
    die( "Problem with your registration info. "
    ."Please go back and try again.");
    }
     
    // Fields are clear, add user to database
    // Setup query
    $q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "
    ."VALUES ('".$_POST["username"]."', "
    ."PASSWORD('".$_POST["password"]."'), "
    ."'".$_POST["email"]."')";
    // Run query
    $r = mysql_query($q);
     
    // Make sure query inserted user successfully
    if ( !mysql_insert_id() )
    {
    die("Error: User not added to database.");
    }
    else
    {
    // Redirect to thank you page.
    Header("Location: register.php?op=thanks");
    }
    } // end if
     
     
    //The thank you page
    elseif ( $_GET["op"] == "thanks" )
    {
    echo "<h2>Thanks for registering!</h2>";
    }
     
    //The web form for input ability
    else
    {
    echo "<form action=\"?op=reg\" method=\"POST\">\n";
    echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
    echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n";
    echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n";
    echo "<input type=\"submit\">\n";
    echo "</form>\n";
    }
    // EOF
    ?>
     
    Step 4 - create a file login.php with the codes below:
    <pre lang="PHP" line="1" colla="+">
    <?php
    session_start();
    // dBase file
    include "dbConfig.php";
     
    if ($_GET["op"] == "login")
    {
    if (!$_POST["username"] || !$_POST["password"])
    {
    die("You need to provide a username and password.");
    }
     
    // Create query
    $q = "SELECT * FROM `dbUsers` "
    ."WHERE `username`='".$_POST["username"]."' "
    ."AND `password`=PASSWORD('".$_POST["password"]."') "
    ."LIMIT 1";
    // Run query
    $r = mysql_query($q);
     
    if ( $obj = @mysql_fetch_object($r) )
    {
    // Login good, create session variables
    $_SESSION["valid_id"] = $obj->id;
    $_SESSION["valid_user"] = $_POST["username"];
    $_SESSION["valid_time"] = time();
     
    // Redirect to member page
    Header("Location: members.php");
    }
    else
    {
    // Login not successful
    die("Sorry, could not log you in. Wrong login information.");
    }
    }
    else
    {
    //If all went right the Web form appears and users can log in
    echo "<form action=\"?op=login\" method=\"POST\">";
    echo "Username: <input name=\"username\" size=\"15\"><br />";
    echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
    echo "<input type=\"submit\" value=\"Login\">";
    echo "</form>";
    }
    ?>

    Step 5 – create a file members.php with the codes below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    <?php
    session_start();
     
    if (!$_SESSION["valid_user"])
    {
    // User not logged in, redirect to login page
    Header("Location: login.php");
    }
     
    // Member only content
    // ...
    // ...
    // ...
     
    // Display Member information
    echo "<p>User ID: " . $_SESSION["valid_id"];
    echo "<p>Username: " . $_SESSION["valid_user"];
    echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);
     
    // Display logout link
    echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";
    ?>

    Step 6 – create a file logout.php with the codes below:

    1
    2
    3
    4
    5
    6
    7
    8
    
    <?php
    session_start();
    session_unset();
     
    session_destroy();
    // Logged out, return home.
    Header("Location: index.php");
    ?>

Comments on this post

Leave a Reply

  • Security Code :


    2 + one =