projemizde kullanıcıların, üye girişi, kayıt ve üyelik işlemleri için bir sınıf yazmamız gerekli. ben bu sınıfın projede kullanılacak ozelleklerini oluşturdum. daha sonra bu sınıfımızı geliştireceğiz. şu an için kullanılacak özelliklere bakalım.
TÜM DOSYALARI İNDİRMEK VE AUTH SINIFININ KULLANIMI İÇİN TIKLAYIN
Sınıfımızın methodları
Auth : sınıfımızın constructoru checklogin fonksiyonunu cagirir.
checkLogin : siteye giriş yapılıp yapılmadığını kontrol eder ve setDefine fonksyionunu çağırır.
setDefine : üye bilgilerini sabit değerlere atar
setCookie : cookie olusturur.
getID : id’ye göre veritabanından üye bilgilerini çeker
getEMAIL : email’e göre veritabanından üye bilgilerini çeker
auth_redirect : işlem sonucunda başka sayfaya yönlendirme işlemini yapar
login : validateLogin methodunu çağırır ve bilgiler doğru ise veritabanından üye bilgilerini çekip formdan gelen bilgilerle karşılaştırma yapar.
validateLogin : giriş formuna yazılan bilgilerin doğruluğunu kontrol eder.
logout : cookie ve sessiondaki bilgilerin silinmesini sağlar.
validateUser : kayıt formundan gelen bilgilerin doğruluğunu kontrol eder.
insertUser : validateUser methodundan gelen bilgiler doğru ise veritanınına yeni kullanıcı ekler.
sendMail : üyelik ile ilgili maillerin gönderilmesini sağlar.
generateKey : aktivasyon kodu oluşturmak için kullanılır
setActivation : aktivasyon tipine göre yapılacak işlemleri uygular
setActivationCode : aktivasyon kodunu veritabanına gonderir.
evet bu kadar ön bilgiden sonra sistemin çalışması için yapacağımız değişikliklere bakalım.
project sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | -- phpMyAdmin SQL Dump -- version 2.10.3 -- http://www.phpmyadmin.net -- -- Anamakine: localhost -- Üretim Zamanı: 12 Temmuz 2009 saat 23:46:09 -- Sunucu sürümü: 5.0.51 -- PHP Sürümü: 5.2.6 -- -- Veritabanı: `project` -- -- -------------------------------------------------------- -- -- Tablo yapısı: `user` -- CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL auto_increment, `username` varchar(16) NOT NULL, `password` varchar(32) NOT NULL, `email` varchar(25) NOT NULL, `usergroup` tinyint(1) unsigned NOT NULL, `active` tinyint(1) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -- Tablo döküm verisi `user` -- -- -------------------------------------------------------- -- -- Tablo yapısı: `user_activation` -- CREATE TABLE `user_activation` ( `id` int(11) unsigned NOT NULL auto_increment, `email` varchar(25) NOT NULL, `code` varchar(16) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -- Tablo döküm verisi `user_activation` -- |
/project/cache/captcha/
adında yeni klasör oluşturuyoruz.
/project/uploads/fonts/
adında yeni klasör oluşturuyoruz ve içine tahoma.ttf yazıtipini yüklüyoruz
/project/system/application/plugins/captcha_pi.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function standart_captcha() { $vals = array( 'img_path' => './cache/captcha/', 'img_url' => base_url().'/cache/captcha/', 'font_path' => './uploads/fonts/tahoma.ttf', 'img_width' => '150', 'img_height' => 30, 'expiration' => 7200 ); $cap = create_captcha($vals); return $cap; } |
fonksiyonunu ekliyoruz. create_captcha fonksiyonunu silmiyoruz!!!
/project/system/application/config/database.php
dosyasından veritabanı ayarlarımızı yapıyoruz
1 2 3 4 5 | $db['default']['hostname'] = "localhost"; $db['default']['username'] = "root"; $db['default']['password'] = "xxxx"; $db['default']['database'] = "project"; $db['default']['dbdriver'] = "mysql"; |
/project/system/application/config/config.php
dosyasına aşağıdaki kısımları ekliyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $config['redirect_time']= 5; /* | ------------------------------------------------------------------- | Auth Enable | ------------------------------------------------------------------- | Change FALSE two values for close auth. */ $config['auth_cookie_enable'] = TRUE; $config['auth_session_enable'] = TRUE; /* | ------------------------------------------------------------------- | User Activation | ------------------------------------------------------------------- | 1 - No Activation Needed | 2 - Administrator Actication Needed | 3 - Email Activation Needed | */ $config['auth_activation_type'] = 3; $config['website_mail'] = 'info@internet.com.tr'; $config['website_name'] = 'internet.com.tr'; |
/project/system/application/config/autoload.php
1 2 | $autoload['libraries'] = array('database','session','theme','auth'); $autoload['helper'] = array('url','cookie'); |
kısımlarını değiştiriyoruz.
/project/system/application/controllers/redirect.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php class Redirect extends Controller { function Redirect() { parent::Controller(); log_message('debug', 'Redirect Page Initialized.'); $this->lang->load('auth'); } function index() { $url = preg_replace("/[^a-zA-Z0-9_]/", "", $this->uri->segment(3)); $url = explode('-',$url); $redir_url = $this->session->userdata('redir_url'); $redir = 'redirect_'.$url[0]; $redir_id = (!empty($url[1])) ? '/' . $url[1] : ''; $array['redirect_error'] = $this->lang->line($redir); $array['redirect_url'] = site_url().'/'.$redir_url.$redir_id; $array['redirect_time'] = $this->config->item('redirect_time'); $array['redirect_message'] = $this->lang->line('redirect_redirect'); $this->theme->assign('arr', $array); $this->session->set_userdata('redir_url', ''); $this->theme->assign('content', $this->theme->fetch('redirect/redirect.tpl')); $this->theme->output('noheader.tpl'); } } ?> |
/project/system/application/language/english/auth_lang.php
dosyasını oluşturuyoruz ve içerik olarak aşağıdaki kodları ekliyoruz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <?php $lang['redirect_nologin'] = 'Bu sayfaya giriş için yetkiniz yetersiz, lütfen üye girişi yapınız.'; $lang['redirect_alreadylogin'] = 'Daha önceden giril yapılmış!'; $lang['redirect_nocredit'] = 'Bu sayfaya giriş için yetkiniz yetersiz.'; $lang['redirect_redirect'] = 'Eğer tarayıcınız otomatik olarak yönlendirmiyorsa lütfen burayı tıklayın.'; $lang['redirect_password_resend_ok'] = 'Şifre başarıyla gönderildi. Lütfen email adresinizi kontrol ediniz.'; $lang['redirect_password_resend_no'] = 'Bu email adresi sisteme kayıtlı değildir. Email adresinizi kontrol edil tekrar deneyin.'; $lang['redirect_confirm_password_ok'] = 'Şifreniz başarıyla güncellendi. Artık sisteme giriş yapabilirsiniz.'; $lang['redirect_confirm_password_no'] = 'Email ve/veya şifrenizi yanlış girdiniz. Lütfen kontrol ederek yeniden deneyiniz.'; $lang['redirect_password_ok'] = 'Şifre başarıyla gönderildi. Lütfen email adresinizi kontrol ediniz.'; $lang['redirect_password_changed'] = 'Şifre başarıyla değiştirildi'; $lang['redirect_register_succesfuly'] = 'Üye kaydınız başarıyla geçekleştirildi.'; $lang['redirect_logoutsuccesfuly'] = 'Başarıyla çıkış yapıldı.'; $lang['redirect_loginsuccesfuly'] = 'Başarıyla giriş yapıldı.'; $lang['redirect_activation_resend_ok'] = 'Aktivasyon kodu başarıyla gönderildi. Lütfen email adresinizi kontrol ediniz.'; $lang['redirect_activation_resend_no'] = 'Bu email adresi sisteme kayıtlı değildir. Email adresinizi kontrol edil tekrar deneyin.'; $lang['redirect_activation_confirm_ok'] = 'Aktivasyon kodu kabul edildi. Artık sisteme giriş yapabilirsiniz.'; $lang['redirect_activation_confirm_no'] = 'Aktivsyon kodu sisteme kayıtlı değildir. Bilgileri kontrol ettikten sonra tekrar deneyin.'; $lang['redirect_activation_confirm_noemail'] = 'Bu email adresi sisteme kayıtlı değildir. Email adresinizi kontrol edil tekrar deneyin.'; $lang['redirect_email_activation'] = 'Üye kaydınız tamamlanmıştır. Aktivasyon yapmanız için gerekli bilgiler email adresinize gönderilmiştir. Lütfen email adresinizi kontrol ediniz.'; $lang['redirect_admin_activation'] = 'Üye kaydınız tamamlanmıştır. Site yönetimi tarafından onaylandıktan sonra sisteme giriş yapabilirsiniz.'; $lang['general_security_code']= 'Güvenlik Kodu'; $lang['general_name']= 'İsim'; $lang['general_email'] = 'Email'; $lang['general_username'] = 'Kullanıcı Adı'; $lang['general_password'] = 'Şifre'; $lang['email_header'] = 'Merhaba,'; $lang['email_footer'] = 'internet.com.tr'; $lang['auth_noactive'] = "Auth modeli aktif değil"; $lang['auth_activation_e_subject'] = 'Üyelik Kaydı'; $lang['auth_activation_e_message'] = 'email aktivasyon mesajı aktivasyon kodu : %s '; $lang['auth_activation_a_subject'] = 'Üyelik Kaydı'; $lang['auth_activation_a_message'] = 'admin aktivasyon mesajı'; $lang['auth_activation_r_subject'] = 'Üyelik Kaydı'; $lang['auth_activation_r_message'] = 'üyelik mesajı'; $lang['auth_activation_resend_subject'] = 'Akitivasyon Kodu'; $lang['auth_activation_resend_message'] = 'aktivasyon mesajı'; $lang['auth_lost_password_subject'] = 'Şifre Talebi'; $lang['auth_lost_password_message'] = 'şifre talep mesajı'; $lang['auth_re_password'] = 'Şifre Tekrar'; $lang['auth_login_info'] = 'Giriş Bilgileri'; $lang['auth_reg_form'] = 'Üyelik Formu'; $lang['auth_username_registered'] = '%s başka bir üye tarafından kullanılmaktadır.'; $lang['auth_email_registered'] = '%s başka bir üye tarafından kullanılmaktadır.'; $lang['auth_login_form'] = 'Üye Giriş Formu'; $lang['auth_lost_password'] = 'Şifremi Unuttum'; $lang['auth_oldpassword'] = 'Mevcut Şifre'; $lang['auth_newpassword'] = 'Yeni Şifre'; $lang['auth_re_newpassword'] = 'Yeni Şifre'; $lang['auth_password_nomatch'] = 'Şifrenizi yanlış girdiniz.'; $lang['auth_change_passtitle'] = 'Şifre Değiştir'; $lang['auth_lost_passtitle'] = 'Şifre Hatırlat'; $lang['auth_password_no'] = 'Şifreniz kayıtlı değil. Lütfen şifrenizi kontrol ediniz.'; $lang['auth_email_no'] = 'Email adresiniz kayıtlı değil. Lütfen email adresinizi kontrol ediniz.'; $lang['auth_user_no'] = 'Kullanıcı adı ve/veya şifre geçersiz.'; $lang['auth_confirm_no'] = 'Email ve/veya şifre geçersiz.'; $lang['auth_security_fail'] = 'Güvenlik kodunu yanlış girdiniz.'; ?> |
/project/system/application/models/auth_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Auth_Model extends Model { function Auth_Model() { parent::Model(); $this->_table = 'user'; $this->_activation_table = 'user_activation'; } /* | ------------------------------------------------------------------- | Register / Login | ------------------------------------------------------------------- | */ function getLogin($username, $password) { $this->db->select('id,username,password,usergroup'); $this->db->where('username', $username); $this->db->where('password', $password); $this->db->where('active', '1'); return $this->db->get($this->_table); } function insertUser($data) { $this->db->insert($this->_table, $data); } function getID($id, $select=false) { if($select) $this->db->select($select); $this->db->where('id', $id); return $this->db->get($this->_table); } function isRegistered($key, $value) { $this->db->select('id'); $this->db->where($key, $value); return $this->db->get($this->_table); } function getEMAIL($email, $select=false) { if($select) $this->db->select($select); $this->db->where('email', $email); return $this->db->get($this->_table); } function setActivation($data) { $this->db->insert($this->_activation_table, $data); } } ?> |
/project/system/application/libraries/MY_Validation.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php if (!defined('BASEPATH')) exit('No direct script access permitted.'); Class MY_Validation extends CI_Validation { function MY_Validation() { parent::CI_Validation(); log_message('debug', 'MY Validation Library Initialized.'); $this->CI->load->model('auth_model', 'auth_model'); } function check_username($username) { $query = $this->CI->auth_model->isRegistered('username', $username); if($query->num_rows() > 0) { $this->set_message('check_username', $this->CI->lang->line('auth_username_registered')); return false; } return true; } function check_email($email) { $query = $this->CI->auth_model->isRegistered('email', $email); if($query->num_rows() > 0) { $this->set_message('check_email', $this->CI->lang->line('auth_email_registered')); return false; } return true; } function check_code($code) { $session_code = $this->CI->session->userdata('comment_code'); if(empty($session_code) OR empty($code) OR ($session_code != $code)) { $this->set_message('check_code', $this->CI->lang->line('auth_security_fail')); return false; } return true; } } ?> |
/project/system/application/libraries/Auth.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Class Auth { var $s_e; var $c_e; var $CI; var $cookie_name; var $cookie_path; var $user_info; var $userid; var $define = FALSE; function Auth() { $this->CI=& get_instance(); log_message('debug', 'Auth Library Initialized.'); $this->c_e = $this->CI->config->item('auth_cookie_enable'); $this->s_e = $this->CI->config->item('auth_session_enable'); if(!$this->c_e && !$this->s_e) exit($this->CI->lang->line('auth_noactive')); $this->CI->load->library('validation'); $this->CI->load->model('auth_model', 'auth_model'); $this->cookie_name = 'ci'; $this->cookie_path = '/'; $this->checkLogin(); } /* | ------------------------------------------------------------------- | General | ------------------------------------------------------------------- | */ function checkLogin() { if($this->s_e) { $this->userid = $this->CI->session->userdata('id'); if(!empty($this->userid)){ $this->setDefine($this->userid); return; } } if ($this->c_e) { if (get_cookie($this->cookie_name)) { $cookie_parts = explode(chr(31), get_cookie($this->cookie_name)); $this->userid = $cookie_parts[0]; $username = $cookie_parts[1]; $password = base64_decode($cookie_parts[2]); $query = $this->CI->auth_model->getLogin($username, $password); if ($query->num_rows() == 1) { if($this->s_e) { $this->CI->session->set_userdata('id', $this->userid); } $this->setDefine($this->userid); return; } } } if(empty($this->userid)){ $this->setDefine(); } } function _encode($password) { $_password = $this->CI->config->item('encryption_key').$password; $majorsalt= ""; for ($i = 0; $i < strlen($_password); $i++) { $majorsalt .= md5($_password[$i]); } return md5($majorsalt); } function setCookie($row, $savelogin = FALSE) { $expire = empty($savelogin) ? time() - 3600 : time() + 2592000; $cookie_str = $row->id.chr(31).$row->username.chr(31).base64_encode($row->password); set_cookie($this->cookie_name, $cookie_str, $expire, $this->cookie_path); } function setDefine($id = FALSE) { if(!$this->define) { if(!$id) { define('LOGIN', 0); define('U_USERNAME', "Ziyaretçi"); define('U_ID', 0); define('U_EMAIL', 0); } else { $row = $this->getID($id); define('LOGIN', 1); define('U_USERNAME', $row->username); define('U_ID', $row->id); define('U_EMAIL', $row->email); } $this->define = TRUE; } } function getID($id) { if(empty($this->user_info)) { $query = $this->CI->auth_model->getID($id); if($query->num_rows() > 0) { $this->user_info = $query->row(); } } return $this->user_info; } function getEMAIL($email) { if(empty($this->user_info)) { $query = $this->CI->auth_model->getEMAIL($email); if($query->num_rows() > 0) { $this->user_info = $query->row(); } } return $this->user_info; } function auth_redirect($data, $url) { $this->CI->session->set_userdata('redir_url', $url); header("Location: ".site_url()."/redirect/index/".$data); exit(); } /* | ------------------------------------------------------------------- | Login / Logout | ------------------------------------------------------------------- | */ function validateLogin() { $rules = array( 'username' => 'trim|htmlspecialchars|required|min_length[4]|max_length[12]|alpha_numeric', 'password' => 'trim|htmlspecialchars|required' ); $fields = array( 'username' => $this->CI->lang->line('general_username'), 'password' => $this->CI->lang->line('general_password') ); $this->CI->validation->set_rules($rules); $this->CI->validation->set_fields($fields); return $this->CI->validation->run(); } function login() { if ($this->validateLogin()) { $username = $this->CI->validation->username; $password = $this->_encode($this->CI->validation->password); $query = $this->CI->auth_model->getLogin($username, $password); if ($query->num_rows() == 1) { $row = $query->row(); if($this->s_e) { $this->CI->session->set_userdata('id', $row->id); } if($this->c_e) { $this->setCookie($row, FALSE); } $this->auth_redirect('loginsuccesfuly', 'user/profile'); } else { return true; } } else { return false; } } function logout() { if($this->s_e) { $this->CI->session->set_userdata('id', FALSE); } if($this->c_e) { set_cookie($this->cookie_name, "", time()-3600, $this->cookie_path); } $this->auth_redirect('logoutsuccesfuly', ''); } /* | ------------------------------------------------------------------- | Registration | ------------------------------------------------------------------- | */ function validateUser() { $rules = array( 'username' => 'trim|htmlspecialchars|required|min_length[4]|max_length[12]|alpha_numeric|check_username', 'password' => 'trim|required|matches[re_password]', 're_password' => 'trim|required', 'email' => 'trim|htmlspecialchars|required|valid_email|check_email', 'securitycode' => 'trim|required|check_code' ); $fields = array( 'username' => $this->CI->lang->line('general_username'), 'password' => $this->CI->lang->line('general_password'), 're_password' => $this->CI->lang->line('auth_re_password'), 'email' => $this->CI->lang->line('general_email'), 'securitycode' => $this->CI->lang->line('general_security_code') ); $this->CI->validation->set_rules($rules); $this->CI->validation->set_fields($fields); return $this->CI->validation->run(); } function insertUser() { $activation = $this->CI->config->item('auth_activation_type'); $user = array( 'username' => $this->CI->validation->username, 'password' => $this->_encode($this->CI->validation->password), 'email' => $this->CI->validation->email, 'active' => $activation, 'usergroup' => '1' ); $this->CI->auth_model->insertUser($user); $user_id = $this->CI->db->insert_id(); $this->setActivation($activation, $user_id); } /* | ------------------------------------------------------------------- | Activation | ------------------------------------------------------------------- | */ function sendMail($user, $subject, $message) { $sender_email = $this->CI->config->item('website_mail'); $sender_name = $this->CI->config->item('website_name'); $content = $this->CI->lang->line('email_header'); $content .= $message; $content .= $this->CI->lang->line('email_footer'); $this->CI->load->library('email'); $this->CI->email->clear(); $this->CI->email->from($sender_email, $sender_name); $this->CI->email->to($user->email); $this->CI->email->subject($subject); $this->CI->email->message($content); $this->CI->email->send(); } function generateKey($length) { $pattern = "1234567890abcdefghijklmnopqrstuvwxyz"; $key = $pattern{rand(0,35)}; for($i=1;$i<$length;$i++) { $key .= $pattern{rand(0,35)}; } return $key; } function setActivation($activation, $id) { if($activation == '3') { $user = $this->getID($id); $code = $this->setActivationCode($user); $message = sprintf($this->CI->lang->line('auth_activation_e_message'), $code); $subject = $this->CI->lang->line('auth_activation_e_subject'); $activation_message = 'email_activation'; $url = 'user/activation'; } elseif ($activation == '2') { $message = $this->CI->lang->line('auth_activation_a_message'); $subject = $this->CI->lang->line('auth_activation_a_subject'); $activation_message = 'admin_activation'; $url = ''; } else { $message = $this->CI->lang->line('auth_activation_r_message'); $subject = $this->CI->lang->line('auth_activation_r_subject'); $activation_message = 'register_succesfuly'; $url = ''; } @$this->sendMail($user, $subject, $message); $this->auth_redirect($activation_message, $url); } function setActivationCode($user) { $code = $this->generateKey(6); $activation['email'] = $user->email; $activation['code'] = $code; $this->CI->auth_model->setActivation($activation); return $code; } } ?> |
TÜM DOSYALARI İNDİRMEK VE AUTH SINIFININ KULLANIMI İÇİN TIKLAYIN
benzer konular:














(2 votes, average: 4.50 out of 5)
design patterns