Create a Login for Your Website

I have always been interested in platforms like WordPress, Joomla, and Drupal which allow websites to use logins. I am trying to create my own login platform. It is pretty hard, but I assume with lots of PHP scripting, a little sweat, and a mySQL database, I will be able to make it. I am really interested to see how you create one.

Here’s what I’ve learned:

  1. Run this SQL command in your MySQL database (you must have one):CREATE TABLE `users` (
    `id` int(3) NOT NULL auto_increment,
    `login` varchar(8) default NULL,
    `password` varchar(8) default NULL,
    PRIMARY KEY (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=3 ;

    This will create the table that will record the usernames and passwords.

  2. Then, create the LOGIN.PHP file. This page will contain the form that will submit the user´s data.

    <?
    session_name
    (“MyLogin”);
    session_start();
    session_destroy();

    if($_GET['login'] == “failed”) {
    print $_GET['cause'];
    }
    ?>
    <form name=”login_form” method=”post” action=”log.php?action=login”>
    Login: <input type=”text” name=”user”><BR>
    Password: <input type=”password” name=”pwd”><BR>
    <input type=”submit”>
    </form>

  3. Now, create the LOG.PHP. This is the file that performs the action of the form.

    <?
    session_name
    (“MyLogin”);
    session_start();

    if($_GET['action'] == “login”) {
    $conn = mysql_connect(“localhost”,“user”,“password”); // your MySQL connection data
    $db = mysql_select_db(“DATABASENAME”); //put your database name in here
    $name
    = $_POST['user'];
    $q_user = mysql_query(“SELECT * FROM USERS WHERE login=’$name’”);

    if(mysql_num_rows($q_user) == 1) {

    $query
    = mysql_query(“SELECT * FROM USERS WHERE login=’$name’”);
    $data = mysql_fetch_array($query);
    if($_POST['pwd'] == $data['password']) {
    session_register(“name”);
    header(“Location: yourpage.php”); // success page. put the URL you want
    exit;
    } else {
    header(“Location: login.php?login=failed&cause=”.urlencode(‘Wrong Password’));
    exit;
    }
    } else {
    header(“Location: login.php?login=failed&cause=”.urlencode(‘Invalid User’));
    exit;
    }
    }

    // if the session is not registered
    if(session_is_registered(“name”) == false) {
    header(“Location: login.php”);
    }
    ?>

  4. If you had enough attention, you noticed that the login will lead the user to YOURPAGE.PHP. Add these lines of code to all your pages that you want to secure (including yourpage.php):

    <?
    require(“log.php”);
    ?>

    Printing the user name in the screen is very easy. Just add this code:

    <? print $_SESSION["name"]; ?>

Selected Randomly...

About the Author

Tyler

Hello, My name is Tyler Plack. I am the Tyler is TCS, TCSL, Tyler’s Computer Show and all of the stuff you hear about. I have many interesting facts about myself. 1. I swim for a swim team year round (google me and you might fnd some of my older meet entries) 2. I can’t stand bad manners 3. I am a perfectionist (”Is that spelled correctly?”) 4. I like to drink coffee and tea 5. I love to write for my school 6. I write just like I talk 7. I love to see the results of my hard work 8. I am really freaked out about anything somewhat supernatural 9. I read archives of blogs in my spare time (I don’t get a lot of it!) 10. One of my favorite restaurants is Noodles & Company and I love sushi Those are some quirks about myself. Now, what are some quirks about yourself? Don’t be shy. Leave a comment. You know you want to!

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>