회원 로그인
|
CodeIgniter(코드이그나이트) 사용
CodeIgniter(코드이그나이트) 사용
설치
- https://codeigniter.com에서 다운로드 (2015.12.6 기준 v3.0.3)
- 압축을 풀고 웹서버의 home 디렉토리에 원하는 폴더로 바꾸어 업로드
- mysql 설정
사용
- config/database.php - db 설정
database.php
- application/controllers - POST, GET등 으로 받은 정보를 models의 클래스를 요청하여 인/아웃풋 처리
game.php
- application/models - 기본적인 사용 클래스 정의(join.php, login.php 등 중복이 되는 부분 클래스화)
user.php
접근
- http://주소/설치루트디렉토리/index.php/controllers php명/클래스명
예) http://주소/home/index.php/game/login
3.0 한글 매뉴얼> http://www.ciboard.co.kr/user_guide/kr
설치
- https://codeigniter.com에서 다운로드 (2015.12.6 기준 v3.0.3)
- 압축을 풀고 웹서버의 home 디렉토리에 원하는 폴더로 바꾸어 업로드
- mysql 설정
사용
- config/database.php - db 설정
database.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /* 주석 생략 */ //$active_group = 'default'; $active_group = 'game_db'; $query_builder = TRUE; // $db['default'] = array( $db['game_db'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => '사용자명', 'password' => 'DB패스워드', 'database' => 'DB명', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => FALSE, //(ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
- application/controllers - POST, GET등 으로 받은 정보를 models의 클래스를 요청하여 인/아웃풋 처리
game.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Game extends CI_Controller { /** * Index Page for this controller. * * Maps to the following URL * http://example.com/index.php/welcome * - or - * http://example.com/index.php/welcome/index * - or - * Since this controller is set as the default controller in * config/routes.php, it's displayed at http://example.com/ * * So any other public methods not prefixed with an underscore will * map to /index.php/welcome/<method_name> * @see http://codeigniter.com/user_guide/general/urls.html */ // modles의 user.php 참조하여 결과 출력 function __construct() { parent::__construct(); // 유저 모델 객체 참조 $this->load->model("User", "user_model"); } public function index() { echo "Game Controller!!"; } public function login(){ // $user_id = $_POST["user_id"] // php 문법 // post 데이터 추출 // 코드이그나이트 (보안처리 가능) // $user_id = $this->input->post("user_id"); // $user_pw = $this->input->post("user_pw"); // post 데이터 추출 $user_info = array(); $user_info["user_id"] = $this->input->post("user_id"); $user_info["user_pw"] = $this->input->post("user_pw"); // 응답 딕셔너리 객체 생성 $res_data = array( "RESULT" => "EMPTY_ACCOUNT" ); // 아이디와 비번 중에 빈값이 있다면 회원 가입 실패 if (!($user_info["user_id"] && $user_info["user_pw"])){ echo json_encode($res_data); exit(); } // 유저 모델 객체에게 로그인을 요청함 $result = $this->user_model->login($user_info); // 로그인 정보가 있다면 if ($result != null){ $res_data["RESULT"] = "LOGIN_SUCCESS"; $res_data["INFO"] = $result; echo json_encode($res_data); } else { // 로그인 정보가 없다면 $res_data["RESULT"] = "LOGIN_FAIL"; echo json_encode($res_data); } } public function join(){ echo "게임 가입"; // 모델 객체의 id 체크 메소드 호출 // 모델 객체의 회원 가입 메소드 호출 } // 스코어 업데이트 public function score_update() { $user_info = array(); $user_info["user_id"] = $this->input->post("user_id"); $user_info["best_score"] = $this->input->post("best_score"); // 응답 딕셔너리 객체 생성 $res_data = array( "RESULT" => "EMPTY_ACCOUNT" ); // 유저 모델 객체에게 로그인을 요청함 $result = $this->user_model->score_update($user_info); if ($result) { $res_data["RESULT"] = "SCORE_UPDATE_SUCCESS"; } else { $res_data["RESULT"] = "SCORE_UPDATE_FAIL"; } echo json_encode($res_data); } }
- application/models - 기본적인 사용 클래스 정의(join.php, login.php 등 중복이 되는 부분 클래스화)
user.php
<?php // 유저 모델 클래스 // models에서 db접근 관련 script 작성 defined('BASEPATH') OR exit('No direct script access allowed'); // CI_Model 상속 해야함 class User extends CI_Model { // 게임 데이터베이스 객체 참조 변수 var $game_db; // 생성자 function __construct() { parent::__construct(); // CI_Model의 생성자 호출 // 데이터베이스 연결 $this->game_db = $this->load->database("DB명", true); } // 게임 유저 로그인 // $user_info controllers에서 넘어옴 function login($user_info) { // game.php 컨트롤러에서 넘어온 파라미터값 추출 $user_id = $user_info["user_id"]; $user_pw = $user_info["user_pw"]; // 코드이그나이트로 'sql 인젝션'' 보안 해결 // (바인딩) 쿼리 작성 $sql = "SELECT * FROM game_tb WHERE id=? && pw=?;"; // 쿼리 바인딩 $bind_param = array( $user_id, $user_pw ); // 쿼리 실행 $query_result = $this->game_db->query($sql, $bind_param); // 조회한 레코드의 갯수가 0 이상이면 if ($query_result->num_rows() > 0){ // num_rows() - count 갯수 추출 // 조회 결과 배열에서 행단위로 추출함 // 테이블 => $result->result() // 행 => $row foreach ($query_result->result() as $row){ // C# foreach($row in $query_result->result()) {} return $row; } } return null; } // 스코어 업데이트 function score_update($user_info){ $user_id = $user_info["user_id"]; $best_score = $user_info["best_score"]; $sql = "UPDATE game_tb SET best_score=? WHERE id=?;"; $bind_param = array( $best_score, $user_id ); // 게임 정보 수정 쿼리 실행 $query_result = $this->game_db->query($sql, $bind_param); // 쿼리 실행 결과 갯수가 1개라도 있으면 수정 성공 if ($this->game_db->affected_rows() > 0) { return true; } else { return false; } } // 가입 function join() { } // id체크(중복 등) function idcheck() { } // 수정 function update() { } } ?>
접근
- http://주소/설치루트디렉토리/index.php/controllers php명/클래스명
예) http://주소/home/index.php/game/login
3.0 한글 매뉴얼> http://www.ciboard.co.kr/user_guide/kr
댓글 0개
| 엮인글 0개
285개(5/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
205 | [Python] Anaconda 사용 | 푸딩뱃살 | 3420 | 2017.08.28 17:51 |
204 | [Python] url 인코딩/디코딩 | 푸딩뱃살 | 6325 | 2017.08.12 19:23 |
203 | [Python] Qt5(ui) to py | 푸딩뱃살 | 2628 | 2017.07.04 15:29 |
202 | [VisualStudio] Visual Studio Community 2015 설치 파일 (다운로드) [1+1] | 푸딩뱃살 | 4516 | 2017.06.11 20:11 |
201 | [VisualStudio] Visual Studio Community 2015 삭제 | 푸딩뱃살 | 4287 | 2017.06.11 20:06 |
200 | [Python] PyQt4 to PyQt5 | 푸딩뱃살 | 2974 | 2017.06.02 16:50 |
199 | [Python] NumPy 라이브러리 설치 | 푸딩뱃살 | 3351 | 2017.05.18 15:14 |
198 | [Python] Python Decompiler | 푸딩뱃살 | 3945 | 2017.04.28 14:28 |
197 | [Python] 암호화 ASE | 푸딩뱃살 | 3433 | 2017.01.09 00:10 |
196 | [Python] 모듈 with xbox 360 controller | 푸딩뱃살 | 2105 | 2016.09.18 22:03 |
195 | [PHP] php 메모리 부족 (PHP Fatal error) | 푸딩뱃살 | 4307 | 2016.09.10 21:07 |
194 | [C#] 조건연산자 ?: | 푸딩뱃살 | 2950 | 2016.09.06 13:46 |
193 | [C#] object 형 변환 (with Unity) [1+1] | 푸딩뱃살 | 10550 | 2016.05.04 02:20 |
192 | [C#] 현재 시간 | 푸딩뱃살 | 3032 | 2016.02.28 23:23 |
191 | [C#] 문자열 Format | 푸딩뱃살 | 2473 | 2016.01.21 12:10 |
190 | [XML] XML 파싱 (with Unity) | 푸딩뱃살 | 7035 | 2016.01.20 10:33 |
189 | [PHP] PHP 전송 종류 | 푸딩뱃살 | 2409 | 2016.01.11 10:49 |
188 | [C#] 배열 | 푸딩뱃살 | 1203 | 2016.01.05 14:38 |
187 | [C#] 참조 ref, out (with Unity) | 푸딩뱃살 | 6075 | 2015.12.08 11:22 |
>> | [PHP] CodeIgniter(코드이그나이트) 사용 | 푸딩뱃살 | 11880 | 2015.12.06 17:46 |