Savage register form php. Creating and testing forms

If you need to make one of the sections of your website accessible to a limited but unspecified circle of people, the easiest way to do this is by registering and authorizing users. There are many ways to authorize users. You can use both web server tools and programming language tools. We'll talk about the case where PHP sessions are used.

You'd probably like to see a more modern way of creating this shape. I still have no plans to fully present it in a modern and relevant way, but you can see that the feedback form can be built using object-oriented techniques in PHP.

First, let's discuss all the steps we will take next. What do we even need? We need a script that will register the user, authorize the user, redirect the user somewhere after authorization. We will also need to create a page that will be protected from access by unauthorized users. For registration and authorization, we will need to create HTML forms. We will store information about registered users in a database. This means that we still need a script for connecting to the DBMS. All our work will be done by functions that we write ourselves. We will save these functions in a separate file.

So, we need the following files:

  • connection to the DBMS;
  • custom functions;
  • authorization;
  • registration;
  • protected page;
  • user shutdown script;
  • a script that checks the user's authorization status;
  • style sheet for the simplest design of our pages.

All this will be meaningless if you do not have a corresponding table in the database. Launch your DBMS management tool (PhpMyAdmin or the command line, whichever is more convenient) and run the following query in it:

CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT, `login` char(16) NOT NULL, `password` char(40) NOT NULL, `reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (` id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

I will name our script files like this (they will all be in one directory):

  • database.php;
  • functions.php;
  • login.php;
  • registration.php;
  • index.php;
  • logout.php;
  • checkAuth.php;
  • style.css.

The purpose of each of them, I am sure, is clear to you. Let's start with the connection script to the DBMS. You've already seen it. Just save the code for this script in a file called database.php. We will declare custom functions in the functions.php file. How will this all work? An unauthorized user tries to access a protected document index.php, the system checks whether the user is authorized, if the user is not authorized, he is redirected to the authorization page. On the login page, the user should see an authorization form. Let's do it.

User authorization Your login: Your password:

register.

Now our form needs to be given some form. At the same time, we will define rules for other elements. Looking ahead, I will present the contents of the style sheet in full.

/* style.css file */ .row ( margin-bottom:10px; width:220px; ) .row label ( display:block; font-weight:bold; ) .row input.text ( font-size:1.2em; padding:2px 5px; ) .to_reg ( font-size:0.9em; ) .instruction ( font-size:0.8em; color:#aaaaaa; margin-left:2px; cursor:default; ) .error ( color:red; margin-left:3px; )

If everything is done correctly, you should have the following in your browser:

Of course, we do not have a single registered user yet, and in order to log in, you need to register. Let's make a registration form.

User registration Enter your login: User registration Enter your login:

Publications on the topic