Connecting Tech Pros Worldwide Help | Site Map

Member Registration Script

By Blair Ireland
Senior Editor, TheScripts.com

As you can see, our action tag in that form is pointed at http://www.yoursite.com/register.php3. Change this accordingly, but I guess I have to explain what register.php3 is.

<?php
function showheader ($title) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> <?php echo $title ?> </TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<?php
}

function showfooter () {
?>
</CENTER>
</BODY>
</HTML>
<?php
}

mysql_connect("localhost", "username", "password");
mysql_select_db("users");

if ($Password == $Password2) {
    $user = mysql_query("SELECT * FROM personalize WHERE (Name='$Name')");
    if (mysql_num_rows($user) > 0) {
        showheader("User Name Taken !");
        ?>
        We are sorry to inform you that the User Name <B><?php echo $Name ?></B> Is already Taken.
        <?php
        showfooter();
    }
    else {
        $user = mysql_query("INSERT INTO personalize VALUES ('$Name','$Password','$News1','$News2','$Weather')");
        setcookie("site_user", $Name, time() + 31536000, "/");
        setcookie("site_pass", $Password, time() + 31536000, "/");
        showheader("Registration Success!");
        ?>
        You have registered.<BR>
        User Name: <?php echo $Name ?>
        <BR>
        Password: <?php echo $Password ?>
        <?php
        showfooter();
    }
}
else {
    showheader("Registration Error!");
    ?>
    Your Two Passwords Did Not Match
    <?php
    showfooter();
}
?>

The function showheader is just a handy way to show the top HTML. It takes the argument $title, which is shown in the <TITLE> </TITLE> part. showfooter takes no arguments, and just shows the bottom HTML.

mysql_connect("localhost", "username", "password");

Connects the script to the database, using the host, username and password provided.

mysql_select_db("users");

This selects the database containing our table.

Since we are confirming the password, we have to check if they match before proceeding. Therefore, we use the if statment

if ($Password == $Password2) {

Now that we are done that, we have to look for identical usernames already existing. We do this with a simple query.

$user = mysql_query("SELECT * FROM personalize WHERE (Name='$Name')");
    if (mysql_num_rows($user) > 0) {
        showheader("User Name Taken !");
        ?>
        We are sorry to inform you that the User Name <B><?php echo $Name ?></B> Is already Taken.
        <?php
        showfooter();
        exit;
    }

If the query results exceed none, then the process is ended, showing the header, the error, and the footer, followed by an exit.

Otherwise, we are proceeding.

$user = mysql_query("INSERT INTO personalize VALUES ('$Name','$Password','$News1','$News2','$Weather')");

This is the query that inserts the values into the database.

setcookie("site_user", $Name, time() + 31536000, "/");
setcookie("site_pass", $Password, time() + 31536000, "/");

The setcookie function might be new to you. It defines a cookie to be sent along with the rest of the header information. Cookies must be sent before any other headers are sent, so, you must do it before the <HTML> tag.

It looks like setcookie("cookie name", "cookie value", $expirydate, "path");

time()+31536000 means that we are setting the expiry date to exactly one year from now. It is represented by a timestamp. The path of "/" just means it can be used throughout your entire site.

After that, we are just calling our showheader function again, displaying the HTML, and showfooter().

« PHP News Script Profiles Script »