Learn to build a CodeIgniter Login and Registration with source code - PowerPoint PPT Presentation

About This Presentation
Title:

Learn to build a CodeIgniter Login and Registration with source code

Description:

Looking for a simple login system? Look no further! We’ll show you how to build a CodeIgniter login and registration system from scratch. – PowerPoint PPT presentation

Number of Views:1
Slides: 14
Provided by: BeProblemSolver
Tags:

less

Transcript and Presenter's Notes

Title: Learn to build a CodeIgniter Login and Registration with source code


1
Learn to build a CodeIgniter Login and
Registration with source code
by Pawan Hey, guys today we will learn how to
build a simple yet powerful Codeigniter login and
registration system from scratch. And dont
worry at the end of the tutorial we will give you
the source of Codeigniter user login and
registration. If you are interested in trying to
build a registration form in HTML and send emails
via PHPMailer then do check out this article as
well. And as usual, we have already built a login
system with simple PHP. Lets start coding away
  • Table of Contents
  • What is CodeIgniter?
  • How to Install CodeIgniter 4 via Composer?
  • Install CodeIgniter 4 Manually
  • CodeIgniter login and registration
  • Link to Database
  • Conclusion

What is CodeIgniter?
2
If you are someone who knows a little bit about
backend programming languages like PHP, ASP, or
even Ruby on Rails. Then you must have heard of
CodeIgniter. CodeIgniter is a free, open-source
framework of PHP. Fast, minimal code and uses
MySQL, which is easy to set up and its easy to
learn too! All these features make CodeIgniter
ideal for any PHP beginner. Not to mention,
CI(as called by most coders) is also an MVC
(model-view-controller) library. And provides
huge built-in libraries for ease of usage. Of
course, there are many other PHP frameworks like
Laravel, CakePHP, Symfony, etc. But CodeIgniter
is especially amazing for beginners. Check out
this infographic
Infographic explaining the difference between
CodeIgniter and Laravel. And before we move to
the next stage, check out our guide on how to use
API in your projects.
3
How to Install CodeIgniter 4 via Composer? To
build our CodeIgniter 4 login and registration,
the easiest method would be to install CI with
Composer. We have explained why Composer is vital
to any PHP developer in our Gmail SMTP PHPmailer
post. Until CodeIgniter 3, using the composer
was optional. But ever since CodeIgniter 4
update, the composer is vital. Why? Because
without the composer you will have a very hard
time upgrading your future CI application. Doing
an upgrade manually is time-consuming and prone
to error. So lets start to install CodeIgniter
with the composer. Here is the simple CLI command
to install a CI project. You use Powershell,
command prompt, or your favorite code editors
terminal to run this. composer create-project
codeigniter4/appstarter your-project-name
As you can see we chose our project name
myfirstproject. Note If this is the first
time you are installing CI4 then there is a high
chance you will encounter the error of require
ext-intl gt it is missing in your system.
  • CodeIgniter 4 Installation Error
  • Resolving this error is simple. Follow these
    steps
  • Go to your PHP.ini file in XAMMP or Wamp.
  • Then search for extensionintl. If it has a
    semicolon before it. Remove it.
  • And then restart your Apache of Xampp and Wamp.

4
Enable intl extension in php.ini to install
Codeigniter 4 login session properly Congrats!
We have successfully installed CI and you should
see the default greeting message.
CodeIgniter welcome message after successful
install
  • Install CodeIgniter 4 Manually
  • Even though using composer to install CodeIgniter
    is the best, sometimes you might need to install
    CI manually. No worries it is a simple process
  • Navigate to the Offical Website of CodeIgniter.
    Click here to Visit.
  • Choose your CodeIgniter version to download. We
    recommend choosing the latest version. Dont
    sweat, the coding process does not change
    dramatically anyway.

5
3. Download your CodeIgniter zip folder. And
extract in your preferred localhost environment
and just run the project. Now we are ready to
code our Codeigniter user login and registration
system.
CodeIgniter login and registration Before we
start now we will divide our code into 3
sections Views Here we code our UI design with
the help of Bootstrap5. We will use the include()
method to separate the header and footer. See
below all files we created for view
Views files in CodeIgniter
Login and Registration project To make the
navigation bar separate we have put them
in header.php lt!DOCTYPE htmlgt lthtml
lang"en"gt ltheadgt ltmeta charset"UTF-8"gt ltmeta
http-equiv"X-UA-Compatible" content"IEedge"gt ltm
eta name"viewport" content"widthdevice-width,
initial-scale1.0"gt lttitlegtLogin Registration
with CI4lt/titlegt ltlink href"https//cdn.jsdelivr.
net/npm/bootstrap_at_5.2.2/dist/css/bootstrap.min.css
" rel"stylesheet"gt ltlink href"https//fonts.goo
gleapis.com/icon?familyMaterialIcons"
rel"stylesheet"gt lt?php echo link_tag('assets/styl
e.css') ?gt lt/headgt ltbody class"parent_main"gt ltna
v class"navbar navbar-expand-lg navbar-dark
bg-dark"gt ltdiv class"container-fluid"gt lta
class"navbar-brand" href"lt?php echo site_url()
?gt"gtLogin Registerlt/agt
6
ltbutton class"navbar-toggler" type"button"
data-bs-toggle"collapse" data-bs-target"navbarS
upportedContent" aria-controls"navbarSupportedCon
tent" aria-expanded"false" aria-label"Toggle
navigation"gt ltspan class"navbar-toggler-icon"gtlt/s
pangt lt/buttongt ltdiv class"collapse
navbar-collapse" id"navbarSupportedContent"gt ltul
class"navbar-nav me-auto mb-2 mb-lg-0"gt ltli
class"nav-item"gt lta class"nav-link active"
aria-current"page" href"lt?php echo site_url()
?gt"gtLoginlt/agt lt/ligt ltli class"nav-item"gt lta
class"nav-link" href"lt?php echo
site_url('register') ?gt"gtSignUplt/agt lt/ligt lt/ulgt lt
/divgt lt/divgt lt/navgt lt?php if (isset(_SESSION'msg
')) ?gt ltdiv class"alert lt?
_SESSION'msg-class' ?gt alert-dismissible fade
show" role"alert"gtlt? _SESSION'msg'
?gtltbutton type"button" class"btn-close"
data-bs-dismiss"alert" aria-label"Close"gtlt/butto
ngt lt/divgt lt?php endif ?gt Footer file has the
basic code footer.php ltscript
src"https//code.jquery.com/jquery-3.6.0.min.js"gt
lt/scriptgt ltscript src"https//cdn.jsdelivr.net/n
pm/bootstrap_at_5.2.2/dist/js/bootstrap.bundle.min.js
"gtlt/scriptgt lt/bodygt lt/htmlgt In the end, we move
on to the UI part of the login, register, and
admin pages of Codeigniter 4 login and
registration. home.php lt?php
include('includes/header.php') ?gt ltdiv
class"container"gt ltdiv class"card mx-auto p-3
child_sub" style"width 25rem"gt lth3 class"py-2
text-center mb-4"gtLoginlt/h3gt ltdiv
class"card-body"gt ltform action"home/login"
method"post"gt ltdiv class"input-group
mb-4"gt ltspan class"input-group-text"gtlti
class"large material-icons"gtemaillt/igtlt/spangt ltinp
ut type"email" class"form-control"
placeholder"Email Address" name"email"
requiredgt
7
lt/divgt ltdiv class"input-group mb-4"gt ltspan
class"input-group-text"gtlti class"large
material-icons"gtfingerprintlt/igtlt/spangt ltinput
type"password" class"form-control"
placeholder"Password" name"password"
requiredgt lt/divgt ltdiv class"text-center"gt ltbutton
type"submit" class"btn btn-success"
name"submit"gtLoginlt/buttongt lt/divgt lt/formgt lt/divgt
ltdiv class"result text-center"gtlt/divgt lt/divgt lt/d
ivgt lt?php include('includes/footer.php')
?gt register.php lt?php include('includes/header
.php') ?gt ltdiv class"container"gt ltdiv
class"card mx-auto p-3 child_sub" style"width
25rem"gt lth3 class"py-2 text-center
mb-4"gtRegisterlt/h3gt ltdiv class"card-body"gt ltform
action"register/signup" method"post"gt ltdiv
class"input-group mb-4"gt ltspan
class"input-group-text"gtlti class"large
material-icons"gtinsert_emoticonlt/igtlt/spangt ltinput
type"text" class"form-control"
placeholder"Name" name"name"gt lt/divgt ltdiv
class"input-group mb-4"gt ltspan
class"input-group-text"gtlti class"large
material-icons"gtemaillt/igtlt/spangt ltinput
type"email" class"form-control"
placeholder"Email Address" name"email"gt lt/divgt lt
div class"input-group mb-4"gt ltspan
class"input-group-text"gtlti class"large
material-icons"gtfingerprintlt/igtlt/spangt ltinput
type"password" class"form-control"
placeholder"Password" name"password"gt lt/divgt ltdi
v class"text-center"gt ltbutton type"submit"
class"btn btn-success" name"submit"gtRegisterlt/bu
ttongt lt/divgt lt/formgt lt/divgt ltdiv class"result
text-center"gtlt/divgt lt/divgt lt/divgt lt?php
include('includes/footer.php') ?gt And our admin
UI file
8
admin.php lt!DOCTYPE htmlgt lthtml
lang"en"gt ltheadgt ltmeta charset"UTF-8"gt ltmeta
http-equiv"X-UA-Compatible" content"IEedge"gt ltm
eta name"viewport" content"widthdevice-width,
initial-scale1.0"gt lttitlegtAdmin
Panellt/titlegt ltlink href"https//cdn.jsdelivr.net
/npm/bootstrap_at_5.2.2/dist/css/bootstrap.min.css"
rel"stylesheet"gt ltlink href"https//fonts.google
apis.com/icon?familyMaterialIcons"
rel"stylesheet"gt lt?php echo link_tag('assets/styl
e.css') ?gt lt/headgt ltbody class"parent_main"gt ltna
v class"navbar navbar-expand-lg navbar-dark
bg-dark"gt ltdiv class"container-fluid"gt lta
class"navbar-brand" href"lt?php echo
site_url('admin') ?gt"gtAdmin Panellt/agt ltbutton
class"navbar-toggler" type"button"
data-bs-toggle"collapse" data-bs-target"navbarS
upportedContent" aria-controls"navbarSupportedCon
tent" aria-expanded"false" aria-label"Toggle
navigation"gt ltspan class"navbar-toggler-icon"gtlt/s
pangt lt/buttongt ltdiv class"collapse
navbar-collapse" id"navbarSupportedContent"gt ltul
class"navbar-nav me-auto mb-2 mb-lg-0"gt ltli
class"nav-item"gt lta class"nav-link active"
aria-current"page" href"lt?php echo site_url()
?gt"gtLoginlt/agt lt/ligt ltli class"nav-item"gt lta
class"nav-link" href"lt?php echo
site_url('register') ?gt"gtSignUplt/agt lt/ligt lt/ulgt lt
li class"d-flex"gt lta class"btn
btn-outline-danger" href"lt?php echo
site_url('logout') ?gt"gtLogoutlt/agt lt/ligt lt/divgt lt/
divgt lt/navgt lt?php if (isset(_SESSION'msg'))
?gt ltdiv class"alert lt? _SESSION'msg-class'
?gt alert-dismissible fade show" role"alert"gtlt?
_SESSION'msg' ?gtltbutton type"button"
class"btn-close" data-bs-dismiss"alert"
aria-label"Close"gtlt/buttongt lt/divgt lt?php endif
?gt ltdiv class"container"gt ltdiv class"card
mx-auto p-3 child_sub" style"width
25rem"gt ltdiv class"card-body"gt
9
lth4 class"py-2 text-center mb-4"gtWelcome to
Admin, lt?php echo _SESSION'loginname'
?gtlt/h4gt lt/divgt lt/divgt lt/divgt lt?php
include('includes/footer.php') ?gt Controller
Here goes our logic which takes data from the
views HTML form and then processes and sends it
to our MySQL database. Here are our controllers
names that we will use in CodeIgniter 4 login
and Registration
Controllers in Codeigniter user login
and registration project Lets go into the
coding section for each file. BaseController is
created by default by CI so we dont need to
edit it. Home.php lt?php namespace
App\Controllers use App\Models\Registermodal cl
ass Home extends BaseController public
registermodal public function
construct() this-gtregistermodal new
Registermodal() public function
index() return view('home') public function
login() request \Config\Servicesrequest()
email request-gtgetVar('email') password
md5(request-gtgetVar('password')) data
this-gtregistermodal-gtloginData(email) if
(data-gtemail email) if (password
data-gtpassword)
10
_SESSION'loginemail' email _SESSION'login
name' data-gtname this-gtsession-gtsetTempdata(
'msg', 'Login Details Success!',
300) this-gtsession-gtsetTempdata('msg-class',
'alert-success', 300) return redirect()-gtto('/ad
min') else this-gtsession-gtsetTempdata('msg',
'Password is incorrect! Try Again',
300) this-gtsession-gtsetTempdata('msg-class',
'alert-danger', 300) return redirect()-gtto('/')
else this-gtsession-gtsetTempdata('msg',
'You Email is incorrect', 300) this-gtsession-gtse
tTempdata('msg-class', 'alert-danger', 300)
return redirect()-gtto('/') The Home.php
controller handles our login page in the project.
Its linked to our Model Registermodal.php and
processes the code of login. Next up is
Register.php. This controller controls the
logic of the registration process. lt?php namespac
e App\Controllers use App\Models\Registermodal
class Register extends BaseController public
registermodal public function
construct() this-gtregistermodal new
Registermodal() public function
index() return view('register') public
function signup() request \Config\Servicesr
equest() password md5(request-gtgetVar('passwo
rd')) insData 'name' gt request-gtgetVar('na
me'), 'email' gt request-gtgetVar('email'),
'password' gt password if (this-gtregistermoda
l-gtinsertData(insData)) this-gtsession-gtsetTemp
data('msg', 'Registration Successfully', 300)
11
this-gtsession-gtsetTempdata('msg-class',
'alert-success', 300) return redirect()-gtto('/')
else this-gtsession-gtsetTempdata('msg',
'Registration Failed', 300) this-gtsession-gtsetTe
mpdata('msg-class', 'alert-danger', 300) return
redirect()-gtto('/') Lastly, we have our
final controller Admin.php. This simple
controller summarizes code related to the Admin
page. And also it is the code that let us log out
of our application by using the session destroy
method. Admin.php lt?php namespace
App\Controllers class Admin extends
BaseController public function index() return
view('admin') public function
logout() this-gtsession-gtdestroy() return
redirect()-gtto('/') Model This handles
all the coding behind the database data insert.
For this purpose, we have the Registermodal.php
file. Here is its code lt?php namespace
App\Models use CodeIgniter\Model class
Registermodal extends Model public db public
function construct() this-gtdb
\Config\Databaseconnect() public function
insertData(data) builder this-gtdb-gttable('u
sers')
12
result builder-gtinsert(data) return
result public function loginData(email) bu
ilder this-gtdb-gttable('users')-gtwhere('email',
email) query builder-gtget() return
query-gtgetRow() Congrats, we are almost
finished. By now if you are tired then check out
15 isekai anime recommendations for fun. And
here is a live demo of our CodeIgniter Login and
Registration System Demo
Link to Database To keep our login data, we are
using our favorite MySQL database.
Database in Codeigniter user login and
registration You can learn to create a simple
database from this post. But to link your MySQL
database with the CodeIgniter app, we must edit
the file database.php.
13
Link Database to CodeIgniter 4
Conclusion We hope you enjoyed our article on how
to build a CodeIgniter 4 login and registration
system. We have made the login and registration
source code available on GitHub, so check it out
if youre interested in learning more about
CodeIgniter. Download CodeIgniter Login and
Registration Source Code We hope you enjoyed it,
and thanks for reading along! Also check our post
on how to build an Image Converter that works
with PNG, JPG, and GIF formats. Ta-da! And Keep
Coding.
Write a Comment
User Comments (0)
About PowerShow.com