Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> A Simple Register Script, This Is a Very Simple Register-Script
Feelay
post Jan 19 2008, 12:56 PM
Post #1


Kinda N00B
Group Icon

Group: Members
Posts: 220
Joined: 13-January 08
From: Sweden
Member No.: 27,579



Some time ago, i made a login-script.
But how do you use a login-script, if you can't register.
So this morning, I decided to make a register-script..


What you should already know:
The php basics and a little more.
How to use php and mysql together.
The HTML basics (to make the forms).



The first thing we should do, is creating the database tables. Here is the code:

CODE
CREATE TABLE `user` (
  `id` int(4) unsigned NOT NULL auto_increment,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  `level` int(4) default '1',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=latin1;


The first thing we do, is adding the "id" column. Nothing important with that one.
Then we add the "username" and "password" columns. Nothing important here either.
But then, we add the "level" column. In my login script, i have an admin system. This is why i made the "level" column. The default value is 1.. but if you change it to 9, the user will become an admin.



Now Lets Move On to "reg.php"
CODE
<?php
require_once 'database.php';
?>

<h1><strong>Register</strong></h1>

<form name="register" method="post" action="regcheck.php">
  <label>
  <input type="text" name="user" id="user">
  </label>
  Username <br><br>
    <label>
  <input type="password" name="pass" id="pass">
  Password<br />
  </label><label>
  <input type="submit" name="reg" id="reg" value="Register">
  </label>
</form>
<label></label>

<form name="Back" method="post" action="Login.php">
  <input type="submit" name="back" id="back" value="Back to Home">
<p> </p>


The first thing I do, is requiring the "database.php".
It will open a connection to the database.

Then, we add the forms and buttons
It isn't harder than that. Now we have the Register Page


Now, we need the regcheck.php page.

CODE
<?php
if(
    isset( $_POST['user'] ) &&
    isset( $_POST['pass'] )
)
{
    if( strlen( $_POST['user'] ) < 4 )
    {
        echo "Username Must Be More Than 4 Characters.";
    }
    elseif( strlen( $_POST['pass'] ) < 4 )
    {
        echo "Passwrod Must Be More Than 4 Characters.";
    }
    elseif( $_POST['pass'] == $_POST['user'] )
    {
        echo"Username And Password Can Not Be The Same.";
    }
    else
    {
        include( 'database.php' );

        $username = mysql_real_escape_string( $_POST['user'] );
        $password = md5( $_POST['pass'] );

        $sqlCheckForDuplicate = "SELECT username FROM user WHERE username = '". $username ."'";

        
        if( mysql_num_rows( mysql_query( $sqlCheckForDuplicate ) ) == 0 )
        {
            $sqlRegUser =     "INSERT INTO
                        user( username, password )
                    VALUES(
                        '". $username ."',
                        '". $password ."'
                        )";

            if( !mysql_query( $sqlRegUser ) )
            {
                echo "You Could Not Register Because Of An Unexpected Error.";
            }
            else
            {
                echo "You Are Registered And Can Now Login";
                $formUsername = $username;
                
                header ('location: Login.php');
            }
        }
        else
        {
            echo "The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
            $formUsername = $username;
        }
    }
}
else
{
    echo "You Could Not Be Registered Because Of Missing Data.";
}
?>



Now the first thing we do here, is to check if the password and user field is checked.
If they are, we check if the fields are more than 4 characters.
And then, we check if the password is the same as the username.
If no errors occurred, we include the database.php
Then make 2 variables.

The $username variable, and the $password variable.
As you can see, the password is md5 protected.

Then we check if the username exists.
if it doesn't, the account will be registered,
and the user will be redirected to the index.php page.
But.. If it is being used, the code will write that the username is being used.


As you can see, i do include the database.php in both files.
Here it is:

CODE
<?
$con = mysql_connect('localhost','mysql_username','mysql_password');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('mysql_database');
?>


The first thing we do here, is:
Start the connection to the database server.
If it couldn't connect, an error will occur.
Then, we select the database we want to use.


This is my third tutorial. Tell me if you liked it, and if you found any errors. tell me, and I will change them.

This post has been edited by Feelay: Jul 23 2008, 09:20 PM
Go to the top of the page
 
+Quote Post
Normano
post Jun 22 2008, 08:07 PM
Post #2


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 36
Joined: 28-August 07
Member No.: 24,433



U made many help full tutorials, however I think u forgot the <?php in the regcheck.php page, thanks for the tutorials happy.gif
Go to the top of the page
 
+Quote Post
Archimedes
post Jul 23 2008, 08:53 PM
Post #3


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 8
Joined: 22-July 08
Member No.: 31,591



In the regcheck.php document, you forgot some of the echo commands.

CODE
Line 16;
  "Username And Password Can Not Be The Same.";
Should be;
echo "Username And Password Can Not Be The Same.";

CODE
Line 42;
"You Are Registered And Can Now Login";
Should be;
echo "You Are Registered And Can Now Login";

CODE
Line 50;
"The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
Should be;
echo "The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";

CODE
Line 57;
  "You Could Not Be Registered Because Of Missing Data.";
Should be;
echo   "You Could Not Be Registered Because Of Missing Data.";


Other than that, nice job.
Go to the top of the page
 
+Quote Post
Feelay
post Jul 23 2008, 09:17 PM
Post #4


Kinda N00B
Group Icon

Group: Members
Posts: 220
Joined: 13-January 08
From: Sweden
Member No.: 27,579



QUOTE
U made many help full tutorials, however I think u forgot the <?php in the regcheck.php page, thanks for the tutorials
haha ^.^ Thanks for the comment smile.gif

QUOTE
In the regcheck.php document, you forgot some of the echo commands.


CODE
Line 16;
"Username And Password Can Not Be The Same.";
Should be;
echo "Username And Password Can Not Be The Same.";


CODE
Line 42;
"You Are Registered And Can Now Login";
Should be;
echo "You Are Registered And Can Now Login";


CODE
Line 50;
"The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";
Should be;
echo "The Username You Have Chosen Is Already Being Used By Another User. Please Try Another One.";


CODE
Line 57;
"You Could Not Be Registered Because Of Missing Data.";
Should be;
echo "You Could Not Be Registered Because Of Missing Data.";


Other than that, nice job.


So.. changed them. Thanks for the comment smile.gif

//Feelay

This post has been edited by Feelay: Jul 24 2008, 10:24 AM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. PHP: Writing A Generic Login And Register Script(14)
  2. PHP Tutorial: Form Verification And Simple Validation(12)
  3. Simple User Validation Script(5)
  4. Very Simple Login-script(18)
  5. Attack Script In Php(5)
  6. Creating A Php Login Script(3)


 



- Lo-Fi Version Time is now: 8th September 2008 - 06:53 AM