- Windows10 기반 Autoset9 (PHP 5.6.0, MySQL 5.6.20) : 정상
- Windows10 기반 Autoset10(Apache 2.4.17, PHP 7.0, MariaDB 10.1.9 ) : 정상
리눅스 기반 PHP 5.6.30, MySQL 5.6.30 에서 테스트를 하다보니 일부 함수가 문제가 발생하는 걸 알았다.
mysqlnd 가 설치되지 않아서였더라.
phpize로 추가 설치하는 방법을 검색해봤는데 아직 해결책을 못찾았다.
정 안되면 APM(Apache + PHP + MySQL) 소스 설치를 다시 해볼 생각이다.
- SHA1 패스워드를 적용한 알고리즘이 좋더라. 그래서 이걸 수정/추가해서 코드를 보완
- 아이폰/안드로이드폰에서 접속시 폰의 고유장치번호를 인식하여 최초 등록 단말이 아닌 경우 로그인 불가처리
- 테이블명을 변경하고 칼럼을 추가하고 user 권한을 설정하는 걸 추가
테이블 구조
- phpMyAdmin SQL에서 아래코드를 붙여넣기 하면 자동으로 DB명과 테이블을 생성한다.
- DB명과 user 권한을 수정해서 이용하면 된다.
create database android default character set utf8; use android; CREATE TABLE IF NOT EXISTS members( idx int(11) primary key auto_increment, userID varchar(60) not null unique, userNM varchar(50) not null, email varchar(60) default null, passwd varchar(80) not null, salt varchar(10) not null, telNO varchar(16) default null, mobileNO varchar(34) default null, phoneSE varchar(50) default null, OStype tinyint(2) NOT NULL DEFAULT '0', created_at datetime, regdate char(8) default null );
use mysql; create user android@localhost; grant all privileges on android.* to android@localhost identified by '!android@'; flush privileges;
DB_Functions 클래스 및 DB 접속정보는 첨부파일 참조
- DB_Functions 클래스가 핵심 내용이다.
<?php
class DB_Functions { private $conn;
// 생성자 function __construct() { require_once 'dbconnect.php'; // DB 연결 $db = new Db_Connect(); $this->conn = $db->connect(); }
// 소멸자(destructor) function __destruct() {
}
// 회원 정보 신규 입력 public function storeUser($userID, $name, $email, $password, $telNO, $mobileNO) { $hash = $this->hashSSHA($password); $encrypted_password = $hash['encrypted']; // encrypted password $salt = $hash['salt']; // salt
// check for successful store if ($result) { $stmt = $this->conn->prepare("SELECT * FROM members WHERE userID = ?"); $stmt->bind_param("s", $userID); $stmt->execute(); $user = $stmt->get_result()->fetch_assoc(); $stmt->close();
return $user; } else { return false; } }
// 로그인 체크 public function getUser($userID, $password) { $stmt = $this->conn->prepare("SELECT * FROM members WHERE userID = ?"); $stmt->bind_param("s", $userID);
if ($stmt->execute()) { $user = $stmt->get_result()->fetch_assoc(); $stmt->close();
// verifying user password $salt = $user['salt']; $encrypted_password = $user['passwd']; $hash = $this->checkhashSSHA($salt, $password); // check for password equality if ($encrypted_password == $hash) { // user authentication details are correct return $user; } } else { return NULL; } }
if ($result->num_rows > 0) { // user existed $stmt->free_result(); $stmt->close(); return true; } else { // user not existed $stmt->free_result(); $stmt->close(); return false; } }
// 회원 정보 삭제 public function deleteUser($userID){ $stmt = $this->conn->prepare("delete FROM members WHERE userID = ?"); $stmt->bind_param("s", $userID); $stmt->execute(); $stmt->close(); }
public function checkhashSSHA($salt, $password) { $hash = base64_encode(sha1($password . $salt, true) . $salt); return $hash; }
}
?>
- user 권한 및 패스워드 설정을 다르게 한 경우에는 해당 파일을 수정해야 한다.
register.php
- 안드로이드에서 전송된 데이터라고 가정하고 값을 직접 입력하여 DB에 잘 저장되는지 테스트했다.
<?php // 안드로이드에서 넘어온 데이터라고 가정하고 직접 DB에 데이터 저장 테스트 // 제대로 저장되는지 확인했으면 아래 4줄은 주석처리 또는 삭제해야 함 $_POST['userID'] = 'jsk005@gmail.com'; $_POST['name'] = '홍길동'; $_POST['email'] = 'jsk005@gmail.com'; $_POST['password'] = 'sk1234!';
extract($_POST);
require_once 'config/db_functions.php'; $db = new DB_Functions();
if (isset($_POST['userID']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
// POST 배열로 받은 데이터 ==> extract($_POST); 이 한줄로 아래 코드 불필요 $userID = $_POST['userID']; $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; $telNO = '02-1234-4567'; $mobileNO = '010-1234-2580';
// 동일한 userID 등록되어 있는지 체크 if ($db->isUserExisted($userID)) { // E-Mail 이 key value // user already existed $response["error"] = TRUE; $response["error_msg"] = "User already existed with " . $userID; echo json_encode($response); } else { // 사용자 등록 $user = $db->storeUser($userID, $name, $email, $password, $telNO, $mobileNO); if ($user) { // 사용자 등록 성공 $response['error'] = FALSE; $response['user']['userID'] = $user['userID']; $response['user']['name'] = $user['userNM']; $response['user']['email'] = $user['email']; $response['user']['created_at'] = $user['created_at']; $response['user']['updated_at'] = $user['regdate']; echo json_encode($response); } else { // 사용자 등록 실패 $response['error'] = TRUE; $response['error_msg'] = "Unknown error occurred in registration!"; echo json_encode($response); } } } else { // 입력받은 데이터에 문제가 있을 경우 $response['error'] = TRUE; $response['error_msg'] = "Required parameters (name, email or password) is missing!"; echo json_encode($response); } ?>
json 으로 넘길 데이터 형태는 개발자가 변경하고 싶은데로 변경하여 사용하면 된다.
login.php
<?php // 안드로이드에서 넘어온 데이터라고 가정하고 직접 DB에 데이터 저장 테스트 // 제대로 저장되는지 확인했으면 아래 3줄은 주석처리 또는 삭제해야 함 $_POST['userID'] = 'jsk005@gmail.com'; $_POST['password'] = 'sk1234!'; $_POST['deviceID'] ='355500071663786';
extract($_POST); require_once 'config/db_functions.php'; $db = new DB_Functions();