PHP component ready to use to implement basic login features.
This is another PHP
component ready
to use and reuse in your project which implements a "ready to use" login
system with PHP. In the past week I received several emails about the difficult
(from some PHP beginners) to implement this functionality in their projects,
in particular for these two common issues:
-
cannot maintain session value after log-in if you visit a new page
- cannot redirect an user in a specific page after the login
So
I prepared this component to help everyone which have the same problems.
This post is not so short... but don't worry... It's just because I want
to exagerate with the words to be clear :)
Download
this PHP component
Before
to start
Take a quick look at the package content:
In detail:
- login-form.php contains
a standard form with two fields, email and password.
- common-functions.php contains
two functions: redirect and remove HTML tag from the input field
- config.php includes
DB connection parameters
- login.php contains
a basic query to verify if an user exists and in this case it creates
a PHP session.
- logout.php contains
delete session code (if you are logged-in)
I supposed your login form
has these fields: email and password.
If login fails an error message will be displayed:
...otherwise if login is ok will be displayed a simple logout link:
You can modify everything how you prefer customizing the code. How? Read
the following paragraphs...
Step
1: include login-form.php
Create a new PHP page (for example index.php)
in the same
folder where
is login-form.php (don't
change it, ok?) :
Open index.php and include login-form.php where you want into the <body>tag using:
<?php include('login-form.php')?>
...in this way you are including the login form into your page. Now, add
this line of code on
top of index.php before
any kind of code in your page (it's important in order to mantain session
created after the user login... you can't forgot it ok? Otherwise it
doesn't work!):
<?php session_start(); ?>
Step
2: modify config.php
Change MySQL connection parameters in config.php:
Step
3: modify login.php
You have to modify some parameters in login.php so
that this component works well. In this example I used a table "USER" to
store the user data (email, password, name...) so, in the line 13 in
my FROM clausule there is "USER".
Change it with the name of the table you have in your database which store
the user data:
It's
a good practice using the primary
key value
to store a session ID. At line
19change
ID_USER_PK with the name of the primary kay you have in the user data
table.
Change line
25 and line
31 to
customizing a redirect page when an user submit the login form.
Step
4: change redirect in logout.php
Last step, modify the URL to redirect an user in a certain page after logout:
It's all! Tell me more infos if you have some problems with this component.
Download
this PHP component

