Created
April 4, 2017 06:47
-
-
Save VaLeXaR/119f8776fa05b2656320fd5dc06a5729 to your computer and use it in GitHub Desktop.
Реєстрація через Фейсбук
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a class="fb_login_btn" onclick="fb_login();return false;"><img src="/image/fb_login.png" border="0" alt=""></a> | |
<script> | |
function handle_fb_data(response){ | |
FB.api('/me', function(response) { | |
console.log('Successful login for: ' + response.name); | |
console.log('Прилитело из ФБ: '+JSON.stringify(response)); | |
// alert('Прилитело из ФБ: '+JSON.stringify(response)); | |
$.ajax({ | |
type: 'post', | |
url: '/do/reg/fb', | |
data: response, | |
success: function(msg) { | |
console.log(msg); | |
if ((msg=='зарегались')||(msg=='залогинились')){window.location.reload();} | |
}, | |
error: function(){} | |
}) | |
}); | |
} | |
function fb_login(){ | |
FB.getLoginStatus(function(response) { | |
if (response.authResponse) { | |
console.log('Welcome! Fetching your information.... '); | |
handle_fb_data(response); | |
} else { | |
console.log('Юзер был не залогинен в самом ФБ, запускаем окно логинизирования'); | |
FB.login(function(response){ | |
if (response.authResponse) { | |
console.log('Welcome! Fetching your information.... '); | |
handle_fb_data(response); | |
} else { | |
console.log('Походу пользователь передумал логиниться через ФБ'); | |
} | |
}); | |
} | |
}, { | |
scope: 'email,id' | |
}); | |
} | |
window.fbAsyncInit = function() { | |
FB.init({ | |
appId : '{ТУТ ВСТАВЬТЕ ID ПРИЛОЖЕНИЯ}', | |
cookie : true, // enable cookies to allow the server to access | |
// the session | |
xfbml : true, // parse social plugins on this page | |
version : 'v2.8' // use graph api version 2.8 | |
}); | |
}; | |
// Load the SDK asynchronously | |
(function(d, s, id) { | |
var js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) return; | |
js = d.createElement(s); js.id = id; | |
js.src = "//connect.facebook.net/en_US/sdk.js"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'facebook-jssdk')); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function fb(){ | |
if (($this->session->userdata('logged_in') != true) ){ | |
// 1. Не делаем ничего, если пользователь УЖЕ залогинен. | |
// 2. Если нет, то Проверим, есть ли у нас в БД юзер с таким FB_ID | |
// если есть, то залогинем его | |
// 3. Если пользователя в базе нет, то сначала его зарегаем и сразу залогинем | |
// Итак, вытаскиваем из базы всё что есть по данному пользователю и заносим всё в его сессию, т.е. "логинем" | |
$query = $this->db->get_where('users', array('fb_id' => $_POST['id'])); | |
if ($query->num_rows() > 0) { | |
foreach ($query->row_array() as $key => $value) { | |
$userdata[$key] = $value; | |
} | |
$userdata["logged_in"] = true; | |
//проверяем, есть ли у пользователя в сессии информация по какой-либо его сделке, если есть, добавляем пользователя в сделку | |
if ($this->session->userdata('bet_code_last') != false) { | |
$data = array( | |
'start_user_id' => $userdata['id'] | |
); | |
$this->db->where('bet_code', $this->session->userdata('bet_code_last')); | |
$this->db->update('bet', $data); | |
$this->session->sess_destroy(); | |
} | |
$this->session->set_userdata($userdata); | |
echo 'залогинились'; | |
} else { | |
$data = array( | |
'fb_id' => $_POST['id'], | |
'lang' => 'en', | |
'name' => $_POST['name'], | |
); | |
// предполагаем, что если имя пользователя содежит москальские символы, то русский язык интерфейса ему будет более удобен | |
if( preg_match('/[ёа-я]/i', $_POST['name']) ){$data['lang']='ru';} | |
$this->db->insert('users', $data); | |
// строчкой выше зарегали его в базе, а стройкой ниже залогинили пользователя ;) | |
$data["logged_in"] = true; | |
$this->session->set_userdata($data); | |
echo 'зарегались'; | |
} | |
}else{echo 'уже был залогинен';} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment