CodeIgniter3
10. [CodeIgniter3] 로그인 폼 만들기 (login form)
drizzle0925
2021. 12. 31. 08:58
728x90
간단한 로그인 폼을 만들어보겠습니다.
1. 학생의 이메일과 비밀번호가 유효한지 확인한 일치 하면 대시보드로 리다이렉션 합니다.
2. 이메일과 비밀번호가 일치하지 않으면 보기 페이지에 오류 메시지를 표시합니다.
student 테이블 구조
(이전 게시글에서 넘어오신 분들은 새로 생성하지 않아도 됩니다.)
create table student(
student_id int primary key auto_increment,
name varchar(50),
email varchar(50),
password varchar(100),
mobile bigint,
course varchar(100)
);
Contoller
application/controllers/User.php 파일에 아래 코드를 입력합니다.
<?php
class User extends CI_Controller
{
public function __construct()
{
// CodeIgniter의 기본 생성자 호출
parent::__construct();
// 데이터베이스 라이브러리르 수동으로 로드
$this->load->database();
// url 헬퍼 호출
$this->load->helper('url');
}
public function index()
{
// name값이 register인 input 태그의 value값을 가져옴
if($this->input->post('register'))
{
// post값을 호출함
$n = $this->input->post('name');
$e = $this->input->post('email');
$p = $this->input->post('pass');
$m = $this->input->post('mobile');
$c = $this->input->post('course');
// Model을 따로 만들지 않고 Controller에서 데이터베이스 쿼리를 처리
$query = $this->db->query("select * from student where email='".$e."'");
$row = $query->num_rows();
if($row)
{
$data['error'] = '<h3 style="color:red">This user already exists</h3>';
}
else
{
$query = $this->db->query("insert into student values('','$n','$e','$p','$m','$c')");
$data['error'] = '<h3 style="color:blue">Your account created successfully</h3>';
}
}
// student_registration view파일에 $data 값을 전달해서 호출
$this->load->view('student_registration', @$data);
}
public function login()
{
// login value값이 있는 경우
if($this->input->post('login'))
{
$e = $this->input->post('email');
$p = $this->input->post('pass');
// contollrer에서 query실행합니다.
$query = $this->db->query("select * from student where email='$e' and password='$p'");
// 쿼리 결과열의 개수를 리턴합니다.
$row = $query->num_rows();
if($row > 0)
{
// 이메일과 비밀번호하고 일치하는 데이터가 있으면 dashboard로 리다이렉트 합니다.
redirect('User/dashboard');
}
else
{
// 일치하지 않으면 에러 메시지를 생성합니다.
$data['error'] = '<h3 style="color:red">Invlid login details</h3>';
}
}
// 로그인 정보가 일치하지 않으면 error 메시지를 login view에 전달해서 호출합니다.
$this->load->view('login', @$data);
}
public function dashboard()
{
// dashboard view 파일을 호출합니다.
$this->load->view('dashboard');
}
}
?>
View
application/views/login.php 파일을 만들고 아래 코드를 입력합니다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login form</title>
</head>
<body>
<form method="post">
<table width="600" align="center" border="1" cellspacing="5" cellpadding="5">
<tr>
<td colspan="2"><?php echo @$error; ?></td>
</tr>
<tr>
<td>Enter Your Email</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Enter Your Password</td>
<td><input type="password" name="pass" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login" name="login" /></td>
</tr>
</table>
</form>
</body>
</html>
아래 주소로 이동해서 로그인 테스트를 합니다.
http://localhost/index.php/user/login
일부로 로그인에 실패해서 오류메시지가 잘 나오는지 확인합니다.
로그인 성공시 이동할 Dashboard View 파일을 만듭니다.
application/views/dashboard.php 파일을 만들고 아래 코드를 입력합니다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Dashboard</title>
</head>
<body>
<h1>Welcome to your dashboard...</h1>
</body>
</html>
정상적으로 로그인되는지 확인합니다.
Github : https://github.com/jun0925/study/commit/07ac999a270f2b3c74dbb9efc68fb772cb4563cd
728x90