Registration system PHP and MySQL

User Registration System In Php

User Registration System using PHP and MySQL database (Part-1)

In this Article, we’ll walk through the complete process of creating a user registration system where users can create an account by providing a username, an email and password in md5, login page and log out page using PHP and MySQL. We will also learn how a user creates Database, Connection with Database, Insertion, Selection, Selection with Comparison and Forms with Post method and Action.

Requirements:

Detail:

Create a database called registration. In the database named as registration, users table added. The users table will take the following four fields as input from registration.

You can create it on the MySQL prompt using the following SQL script:

CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `username` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `password` varchar(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

You can create this using a MySQL client like PHPMyAdmin

cr_image

And that’s it with the database.

Now create a folder called registration in your localhost Server i.e create the folder inside htdocs (if you are using XAMPP server) or inside www (if you are using WAMPP server).

Читайте также:  Рефераты php и mysql

Create a folder name registration and add the following file in it:

cr_image

Open these files up in a text editor of your choice. Try to use the VS Code.

Registering a user:

Open the register.php file and write the following code in it:

register.php

       

Sign Up

Already have an account? Login

A few things to note here:

First is that our form’s action attribute is set to register.php. This means that when the form submit button is clicked, all the data in the form will be submitted to the page (register.php). The part of the code that receives this form data is written in the db.php file and that’s why we are including it at the very top of the register.php file.

In the head section, a style.css file is linking. Open up the style.css file and write/paste the following CSS in it:

style.css

* < margin: 0px; padding: 0px; >body < font-size: 120%; background: #F8F8FF; >.header < width: 35%; margin: 50px auto 0px; color: #060606; background: #c3cfd0; text-align: center; border: 1px solid #a4a8ad; border-bottom: none; border-radius: 10px 10px 0px 0px; padding: 20px; >form, .content < width: 35%; margin: 0px auto; padding: 20px; border: 1px solid #B0C4DE; background: white; border-radius: 0px 0px 10px 10px; >.input-group < margin: 10px 0px 10px 0px; >.input-group label < display: block; text-align: left; margin: 3px; >.input-group input < height: 30px; width: 100%; padding: 5px 10px; font-size: 16px; border-radius: 5px; border: 1px solid gray; >.btn < padding: 10px; font-size: 15px; color: #060606; background: #c3cfd0; border: none; text-align: center; border-radius: 5px; >.error < width: 92%; margin: 0px auto; padding: 10px; border: 1px solid #a94442; color: #a94442; background: #f2dede; border-radius: 5px; text-align: left; >.success < color: #3c763d; background: #dff0d8; border: 1px solid #3c763d; margin-bottom: 20px; >.right

Now the form looks beautiful and reasonable.

Registration page looks like:

Let’s now write the code that will receive information submitted from the form in register.php and store/register the information in the database. As promised above, we’ll do this in the db.php file.

Now enter data in the form like:

Open a db.php file and write this code carefully in it:

db.php

 if (empty($email)) < array_push($errors, "Email is required"); >if (empty($password)) < array_push($errors, "Password is required"); >if ($password != $confirm_password) < array_push($errors, "Failed to Match"); >//fistly check in database that a user does not already exist with the same username and/or email. $get_all = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1"; $result = mysqli_query($con, $get_all); $user = mysqli_fetch_assoc($result); if ($user) < // if user exists if ($user['username'] === $username) < array_push($errors, "Username is already existed"); >if ($user['email'] === $email) < array_push($errors, "email is already existed"); >> // Finally, register user if no error if (count($errors) == 0) < $pwd = md5($password);//encrypt the password before inserting in the database $register = "INSERT INTO users (username, email, password) VALUES('$username', '$email', '$pwd')"; mysqli_query($con, $register); header('Location: login.php'); >> ?>

Remember a few things here:-

The if statement determines if the reg_user button on the registration form is clicked. Remember, in our form, the submit button has a name attribute set to reg_user and that is what we are referencing in the if statement.

All the data in we inserted to the input in the form is received and carefully filtered or checked to make sure that the form correctly filled. The two fields of a password are also compared with each other to make sure that they are similar or match.

If there are no errors, the user is registered or signed it up in the users table in the database with a password(md5). The password is in md5 for security reasons. It ensures the user that even if someone like hacker manages to gain access to the database, it would not be easy to read the password.

But error messages are not displaying now because our errors.php file is still empty. To display the errors while executing the code, write this code in the error.php file carefully.

error.php

After registration or adding the new user, Table will look like:

Important Points:

WAMPP or XAMPP is required for localhost to run the code.

First, run localhost server and for the register.php file, the URL is localhoast/registration/register.php

Create a table in the database before insertion and use mysqli_real_escape_string() to avoid possible string errors.

Carefully write/handle PHP in HTML.

The error.php file is a guide for errors only.

Источник

ibase_add_user

Returns true on success or false on failure.

See Also

User Contributed Notes 1 note

You can create users on Firebird/Interbase this way:

// attach to the server with proper privileges
if (( $service = ibase_service_attach ( ‘localhost’ , ‘sysdba’ , ‘masterkey’ )) != FALSE ) // get server version and implementation strings
$server_info = ibase_server_info ( $service , IBASE_SVC_SERVER_VERSION )
. ‘ / ‘
. ibase_server_info ( $service , IBASE_SVC_IMPLEMENTATION );
//ibase_service_detach($service);
>
else $ib_error = ibase_errmsg ();
>
echo $server_info ;

//add a new user with name ‘pippo’ and password ‘pp’
if (( $result = ibase_add_user ( $service , ‘pippo’ , ‘pp’ )) != FALSE ) echo ‘User created’ ;
ibase_service_detach ( $service );
>
else $ib_error = ibase_errmsg ();
//detach from server
ibase_service_detach ( $service );
>
?>

  • Firebird/InterBase Functions
    • fbird_​add_​user
    • fbird_​affected_​rows
    • fbird_​backup
    • fbird_​blob_​add
    • fbird_​blob_​cancel
    • fbird_​blob_​close
    • fbird_​blob_​create
    • fbird_​blob_​echo
    • fbird_​blob_​get
    • fbird_​blob_​import
    • fbird_​blob_​info
    • fbird_​blob_​open
    • fbird_​close
    • fbird_​commit_​ret
    • fbird_​commit
    • fbird_​connect
    • fbird_​db_​info
    • fbird_​delete_​user
    • fbird_​drop_​db
    • fbird_​errcode
    • fbird_​errmsg
    • fbird_​execute
    • fbird_​fetch_​assoc
    • fbird_​fetch_​object
    • fbird_​fetch_​row
    • fbird_​field_​info
    • fbird_​free_​event_​handler
    • fbird_​free_​query
    • fbird_​free_​result
    • fbird_​gen_​id
    • fbird_​maintain_​db
    • fbird_​modify_​user
    • fbird_​name_​result
    • fbird_​num_​fields
    • fbird_​num_​params
    • fbird_​param_​info
    • fbird_​pconnect
    • fbird_​prepare
    • fbird_​query
    • fbird_​restore
    • fbird_​rollback_​ret
    • fbird_​rollback
    • fbird_​server_​info
    • fbird_​service_​attach
    • fbird_​service_​detach
    • fbird_​set_​event_​handler
    • fbird_​trans
    • fbird_​wait_​event
    • ibase_​add_​user
    • ibase_​affected_​rows
    • ibase_​backup
    • ibase_​blob_​add
    • ibase_​blob_​cancel
    • ibase_​blob_​close
    • ibase_​blob_​create
    • ibase_​blob_​echo
    • ibase_​blob_​get
    • ibase_​blob_​import
    • ibase_​blob_​info
    • ibase_​blob_​open
    • ibase_​close
    • ibase_​commit_​ret
    • ibase_​commit
    • ibase_​connect
    • ibase_​db_​info
    • ibase_​delete_​user
    • ibase_​drop_​db
    • ibase_​errcode
    • ibase_​errmsg
    • ibase_​execute
    • ibase_​fetch_​assoc
    • ibase_​fetch_​object
    • ibase_​fetch_​row
    • ibase_​field_​info
    • ibase_​free_​event_​handler
    • ibase_​free_​query
    • ibase_​free_​result
    • ibase_​gen_​id
    • ibase_​maintain_​db
    • ibase_​modify_​user
    • ibase_​name_​result
    • ibase_​num_​fields
    • ibase_​num_​params
    • ibase_​param_​info
    • ibase_​pconnect
    • ibase_​prepare
    • ibase_​query
    • ibase_​restore
    • ibase_​rollback_​ret
    • ibase_​rollback
    • ibase_​server_​info
    • ibase_​service_​attach
    • ibase_​service_​detach
    • ibase_​set_​event_​handler
    • ibase_​trans
    • ibase_​wait_​event

    Источник

    How to Create New User Accounts with PHP — Adding Users to an Application Using PHP Code and MySQL Tables

    An Internet application administrator can obtain information from new users and then create account for them, or they can use PHP code to do it all automatically.

    One very good reason for using PHP is the way it enables a web site programmer to automate server tasks. Take, for example, the task of creating a new user account. Instead of a system administrator obtaining a new user’s details and then entering them onto a MySQL database, PHP will do all of that. Automatically. And it can even check to see if the new user name has already been used or not.

    The PHP programmer does all of this by writing code that will:

    • work out whether the web page needs to display a blank form or if it should process the new user’s details
    • if no user details have been entered then display a form asking for all relevant details (such as a proposed user name)
    • if user details have been entered then query the database to see if the user name already exists
    • inform the user if the id has already be taken
    • create the new id if it is available

    And, this can all be achieved with just a few lines of PHP code.

    Identifying Whether or Not the User Has Entered Their Details

    The web page should display:

    • either the results of processing the user input
    • or a blank form requesting user details

    The decision as to which one to do can be made quite simply:

     Enter New Username 
    "; > else

    Here code will be executed only if the user has entered details into a text box and then pressed the submit button.

    Processing the User Inputs

    If the user input is to be processed then the code's first job is to connect to the correct MySQL database, for example:

    $mysql_host = "localhost"; $mysql_database = "my_web_site"; $mysql_user = "my_web_site_user"; $mysql_password = "dt56o96"; mysql_connect($mysql_host, $mysql_user, $mysql_password); mysql_select_db($mysql_database); 

    This can be added into the PHP file itself, but it is much more efficient to save that to PHP file of its own, and then to use the include statement to access the code.

    Does the User Already Exist?

    The purpose of this particular PHP script is to create a new user account. It's first job, therefore, should be to ensure that this proposed account does not already exist. This can be done by formulating an appropriate SQL statement (and assumes that a table "USERS" already exist on the MySQL database):

    $SQL = "select * from USERS where username='$username'"; 

    And then running this query on the database:

    $result = mysql_query($SQL) or die (mysql_error()); 

    The programmer can now use the PHP mysql_numrows function to obtain the number of rows returned in the recordset:

    If the number returned is not zero then the user name already exists.

    Creating a new User Name

    If any rows are returned from the above query then the user should be notified that their chosen user name has already been used:

    However, if the user name is available then a simple insert statement can be used to create it:

    And then then connection to the database can be closed:

    If the programmer saves all of this into a PHP web page (for example "add_user.php") then they will have produced an easy and effective way of users being able to create their own accounts on any Internet based application.

    6 лет назад в #utopian-io от dorodor ( 57 )

    Источник

Оцените статью