Navigation


PHP: Writing A Generic Login And Register Script

June 4th, 2007, Php, General, admin, Print This Post Print This Post

Now there are basically 3 functions that a user management system provides: login, register, and protection. A user management system can do more than this but that is all that this tutorial will be covering. I will try to explain what I am doing as I go along but to fully understand what is happening you should have a basic knowledge of PHP, SQL, and HTML. This tutorial assumes you are using MySQL, adjust accordingly for a different DBMS.

First off lets define the database table where our users will be stored. Using phpMyAdmin run this statement to create our table:
CODE

CREATE TABLE tblUsers (
fldId INT NOT NULL AUTO_INCREMENT,
fldUsername VARCHAR(40) NOT NULL,
fldPassword VARCHAR(40) NOT NULL
);

Now a little explanation as to what this will do. It will create a table in your database called tblUsers with fields fldId, fldUsername, and fldPassword. The last two fields are self explanitory they contain the username and password of the user. The fldId is the user id automatically assigned by the database. For more information on the syntax read the MySQL documentation.

Lets continue by creating the script where our users will register. Open your favorite text editor and enter the following:

CODE

<?php

?>

This tells the webserver that we are starting a php code section. You can have more than one in a script and you can include HTML in your code files as well, more on that later. Lets create a function that will actually do the work of adding the user to the database. Lets call it registerUser, now enter the following in between the php tags:

CODE

function registerUser() {
mysql_connect(’server’, ‘username’, ‘password’, ‘database’);
$username = $_POST[’username’];
$password = md5($_POST[’password’]);

$sql = “INSERT INTO tblUsers (fldUsername, fldPassword) VALUES ($username, $password);”;

mysql_query($sql);
}

We now have a very basic registration function. Now we need to create the form the user will see. So below the ?> lets start our HTML. It should look a bit like this:

CODE

<html>
<head>
<title>Registration</title>
</head>
<body>
<form action=”<?php $_SERVER[’PHP_SELF’].”?register=true” ?>” method=”post”>
Username: <input type=”text” name=”username”>
Password: <input type=”password” name=”password”>
<input type=”submit” value=”Register”>
</form>
</body>
</html>

Now this HTML defines a form with 2 input fields and a button. The thing to look at though is the action attribute of the form tag. Here we have another php code section. This puts the path of the current script as our action with the variable register equal to true. We will deal with that in our code later. For now your code should look like this:

CODE

<?php
function registerUser() {
mysql_connect(’server’, ‘username’, ‘password’, ‘database’);
$username = $_POST[’username’];
$password = md5($_POST[’password’]);

$sql = “INSERT INTO tblUsers (fldUsername, fldPassword) VALUES ($username, $password);”;

mysql_query($sql);
}
?>

<html>
<head>
<title>Registration</title>
</head>
<body>
<form action=”<?php $_SERVER[’PHP_SELF’].”?register=true” ?>” method=”post”>
Username: <input type=”text” name=”username”>
Password: <input type=”password” name=”password”>
<input type=”submit” value=”Register”>
</form>
</body>
</html>

There is one more thing left to do. Handle the variable we passed to the script called register. Lets do that now. Here is the code:

CODE

<?php
if ($_GET[’register’] == ‘true’) {
registerUser();
}

function registerUser() { ….

Here we use an if statement to check and see if it has been set to true if it is we call the function we defined earlier

Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etc

del.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb

Leave a reply



Last 25 post


Subcribe

    Add to Google Reader or Homepage

    Subscribe in NewsGator Online

    Add to My AOL

    Add Ask N Answer to Newsburst from CNET News.com

    Subscribe in Bloglines

    Subscribe in FeedLounge

Pages


Category


Archive


Links