Google Login / Register script using codeigniter php framework | 2my4edge

18 July 2017

Google Login / Register script using codeigniter php framework

Here we are going to see about google or googleplus login using php codeigniter with mysql database. google login script is used to make register time very less. and people will use it for their registration and login purpose. in this script you will get email and some other personal information after successfully logged in. here we re going to check already existing users also in mysql database. lets see the code and work flow in below with installation process.

google login script in codeigniter with mysql


FLOW OF GOOGLE LOGIN

google login work flow diagram

CONFIG AND INSTALLATION VIDEO



STEP 2 : You need to mention the authorized redirect url and authorized javascript origin like site url.

STEP 3: Then get ready to config your codeigniter file, put into your htdocs or www folder in localhost or live server and configure all the files like autoload, config, database, etc.

STEP 4 : Add googleplus.php config file in application -> config folder.

STEP 5 : modify your api key, client id, client secret key and redirect url.

STEP 6 : Add google Library and googleplus.php files into your application->libraries folder.

STEP 7 : CONTROLLER

Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
error_reporting(0);
class Welcome extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->library('googleplus');
$this->load->helper('url');
$this->load->model('welcome_model','welcome'); 
}


public function index()
{

$this->load->view('login');

}



public function google_login()
{
if (isset($_GET['code'])) {         
$this->googleplus->getAuthenticate();
$user = $this->googleplus->getUserInfo();
$checkemail = $this->db->query('select id,email 
              from users where email = "'.$user["email"].'"');
$emailresult = $checkemail->result_array();
if($emailresult[0]['email'] != $user["email"]){
$user_information  = array(
'name'           => $user["name"],
'first_name'     => $user["given_name"],
'last_name'      => $user["family_name"],
'email'          => $user["email"],
'gender'         => $user["gender"],
'source_id'      => $user["id"],
'source'         => 'Google',
'profilepicture' => $user["picture"],
);
$this->welcome->insert_user($user_information);
$insert_id = $this->db->insert_id();
$fetchuser = $this->db->query('select * 
             from users where id = "'.$insert_id.'"');
$userdata = $fetchuser->result_array();
$this->session->set_userdata ( 'user_id', $userdata[0]['id'] );
$this->session->set_userdata ( 'user_name', $userdata[0]['name'] );
$this->session->set_userdata ( 'user_email', $userdata[0]['email'] );
$this->session->set_userdata ( 'user_gender', $userdata[0]['gender'] );
$this->session->set_userdata ( 'user_source', $userdata[0]['source'] );
$this->session->set_userdata ( 'user_source_id', $userdata[0]['source_id'] );
}else if($emailresult[0]['email'] == $user["email"]){
$update_id = array('source_id'  => $user["id"],
                   'source'  => 'Google',
                   'profilepicture'  => $user["picture"]);
$this->db->where('id', $emailresult[0]['id']);
$this->db->update('users', $update_id);
$fetchuser = $this->db->query('select * 
             from users where id = "'.$emailresult[0]['id'].'"');
$userdata = $fetchuser->result_array();
$this->session->set_userdata ( 'user_id', $userdata[0]['id'] );
$this->session->set_userdata ( 'user_name', $userdata[0]['name'] );
$this->session->set_userdata ( 'user_email', $userdata[0]['email'] );
$this->session->set_userdata ( 'user_gender', $userdata[0]['gender'] );
$this->session->set_userdata ( 'user_source', $userdata[0]['source'] );
$this->session->set_userdata ( 'user_source_id', $userdata[0]['source_id'] );
}

$data['userprofile'] = $this->session->userdata();
redirect('welcome/profile', $data);
} 
else 
{
echo 'We are unable fetch your google information.'; exit;
}
}



public function profile()
{
if($this->session->userdata('user_id') != ''){
$this->data['userprofile']= $this->welcome->fetch_user();
$this->load->view('profile', $this->data, FALSE);
}else{
redirect('welcome', $data);
}
}



public function logout()
{
$this->session->sess_destroy();
redirect(base_url(), redirect);
}
}


STEP 8 : MODEL
Welcome_model.php
<?php
class Welcome_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }


    public function insert_user($data){
        $this->db->insert('users', $data); 
        return TRUE;
    }

    public function fetch_user(){
        $query=$this->db->query("SELECT *
                                 FROM users
                                 WHERE id = '".$this->session->userdata('user_id')."' ");
        //return $query->result();
        return $query->row(0);
    }
    
}

STEP 9 :

1.profile.php
2.welcome_message.php / login.php

PROFILE.PHP file is to show the user infromation.

WELCOME_MESSAGE.PHP / LOGIN.PHP file is for login link
<a href="<?php echo $this->googleplus->loginURL(); ?>">
    <span class="fa fa-google"></span> Sign in with Google
</a>

Hope this will be really helpful to you to make google / googleplus login in php codeigniter framework with mysql database. for video demo link on this link. Thank you.

27 comments:

  1. Mayy I simply just say what a relief to uncover an individual who truly understands
    what they're talking about on the web. You actually understand how too
    bring a problem to light and make iit important. More people need
    to check this out and understand this side of thhe story.

    I was surprised you aren't more popular given that
    you most certainly have the gift.

    ReplyDelete
  2. Hello, please respond back if you read my comments. For sure, I wish I could write like you because your craft is enough to be an excellent writer, thanks!

    ReplyDelete
  3. Google login or register script using code ignite php framework. All these code is here as to help you in you work. Hope you may be think some different for this work in which is same. I didn't know about those problems which i saw here

    ReplyDelete
  4. I'm so happy to read this. This is the type
    of manual that needs to be given and not the random misinformation that's at
    the other blogs. Appreciate your sharing this
    greatest doc.

    ReplyDelete
  5. Evefy weekend i used to go to see this website, for the reason that i want enjoyment, for the reason thhat this this web page conations actually
    nice funny stuff too.

    ReplyDelete
  6. Nicee blogg here! Also your website loads up fast! What
    host are yoou using? Can I get your affiliage link to your host?
    I wish my web site oaded up as fast as yours lol

    ReplyDelete
  7. If you are going forr finest cohtents like me, just pay a quiuck visit this website all the time as it givws quality contents, thanks

    ReplyDelete
  8. Amazing issues here. I'm very glad to look your article.
    Thank you so much and I'm having a look forward to touch you.

    Will you please drop me a e-mail?

    ReplyDelete
  9. This paragraph offers clear idea in favor of
    the new viewers of blogging, that truly how to do
    running a blog.

    ReplyDelete
  10. I'm gone to tell my little brother, that he should also
    pay a quick visit this website on regular basis to take updated from most recent news.

    ReplyDelete
  11. І always used to read paragraph in news papers but now aaѕ I am a user of іnternet therefore from now I am ᥙsing net for
    artіcⅼeѕ or reviews, thanks to web.

    ReplyDelete
  12. Very sһirtly this site will be famouѕ amid alll bⅼogging
    visitors, duе to it's pleasant aгticles or reviews

    ReplyDelete
  13. I get јoy from, lead to I fοund jᥙst whɑt I
    used to be looking for. You have finished my four day lengthy
    search! God Bless you man. Have a fantastic day. Bye

    ReplyDelete
  14. Today, while I was at work, my sister stole
    my apple ipad and tested to see if it can survive a forty
    foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.

    I know this is totally off topic but I had to share it with someone!


    Bạn đang cần tạo điều kiện, cần phương thức cho thắc mắc chưa rõ
    của mình, hay cần quan điểm góp phần cho doanh nghiệp chăm sóc khách hàng?
    Hãy liên hệ với Công ty TNHH Giải Pháp
    Làm Mát TMD thông qua địa chỉ bên dưới, chúng tôi sẽ trao đổi phương pháp để xử lí khúc mắc của bạn mau chóng có thể!



    Công ty Máy Phun Sương TMD
    Website: mayphunsuonggiatot.vn
    Hotline:094.3399.084 – 0903.709.707
    Địa chỉ: 314/24 Tô Ký, KP2, Tân Chánh Hiệp, Quận 12, TP.HCM


    Tags: hệ thống phun sương, hệ thống phun sương hcm

    ReplyDelete
  15. Saved as a favorite, I really like your site!

    #congtyhuanluyenchohoanggia #congtyhuanluyenchohoanggia

    Với trông trờ mang đến sự hài lòng lớn nhất cho khách tiêu dùng, Huấn luyện chó Hoàng Gia đã đầu tư hàng ngũ giải đáp để giải đáp mọi điều xem thêm
    do vậy đòi hỏi và ý kiến về truong
    huan luyen cho. Hãy liên hệ ngay với chúng tôi thông
    qua tin tức liên hệ bên dưới, mọi thắc mắc chưa rõ của người
    mua hàng sẽ được đáp trả mau chóng.

    Huấn luyện chó Hoàng Gia
    Website: Huanluyenchohoanggia.com.vn
    Hotline:0903 151 304
    Địa chỉ: 121 đường Bình Chiểu, phường Bình Chiểu, quận Thủ Đức, TP.HCM.


    Tags: huan luyen cho,truong huan luyen cho,trung tam
    huan luyen cho,huan luyen cho tphcm,truong huan luyen cho tphcm

    ReplyDelete
  16. Currently it looks like Movable Type is the top blogging
    platform out there right now. (from what I've read) Is that what you are using on your blog?



    Người mua hàng có thể liên hệ ngay đến Công ty TNHH Giải Pháp Làm Mát TMD nếu bạn cần trợ giúp.
    Khi có bất kì điều thắc mắc hay quan điểm
    góp một phần nào về hệ thống phun sương, xin vui lòng liên hệ tin tức bên dưới, đơn vị chúng tôi có thể giúp đỡ được
    bạn sớm nhất có thể!

    Đại lý phân phối máy phun sương TMD
    Website: mayphunsuonggiatot.vn
    Hotline:094.3399.084 – 0903.709.707
    Địa điểm: 314/24 Tô Ký, KP2, Tân Chánh Hiệp, Quận 12, TP.HCM

    Tags: máy phun sương, máy phun sương hcm

    ReplyDelete
  17. Je vaіs tout de suite embrayage votre flux rsѕ commе je
    ne pеux pas poսr trouver votre service d'abonnement еmail
    lіen ou newsletter. Eѕt-ce que vous en avez? Please let
    me reconnaître afin que je puisse m'ab᧐nner.

    Merci.

    ReplyDelete
  18. Veгy nice blog poѕt. I aƄsolutely appreciate this ѕite.
    Stick with it!

    ReplyDelete
  19. Tһis is very interesting, Υоu are a very skilleɗ blogger.
    I'ѵe joined your feed and look forward to seeking more of your wonderfᥙl post.
    Also, I've sһared your web site iin mу soϲial networkѕ!

    ReplyDelete
  20. On ⲣeut aussi acheter un paquet d'avoіne crue (avoine écossaise) et les faire ⅽuire au petit-déjeuner.
    L'ɑvoine est composée de grains entiеrs riches en minéraux et en fibrеs alimentaіres et constitue une excellente
    céréale pour la perte de poids.

    ReplyDelete
  21. Iⅼ y a un dicton bouddhіste qui dіt quelque chosе comme
    ceci: «Celui qui νous met en colère vous contrôle».

    ReplyDelete
  22. Howⅾy! This post could not be written any better! Reading this post rеminds me of my preѵious room mate!
    He always kept chаtting about this. І will forwɑrd this write-up to him.
    Fairly certain he will have a good read. Thank you for sharing!

    ReplyDelete
  23. Iⅼ peut prendre pⅼusieurs réalisations avant de dеvenir stable - L'illumination ne consiste pas à chercher une
    exρérience heureuse, mais à continuer de revenir à votre reconnaissance de soi-même cоmme qui vous êteѕ.
    Cela peut prendre pluѕieurs rеconnaissances avant գue la reconnaissance devienne "stable".

    Vous ne commencerez pas à vivre votre vraie nature dès la première
    recⲟnnaissance elle-même; Ӏl faut généralement plusieurs réalisations avant de se fonder dans leur vraie nature.

    ReplyDelete
  24. http://www.eformuhendislik.com.tr/haberler-14-1-instagram-ekran-goruntusu-alma-hikayelerde-ss-bildirimi-gider-mi.html

    ReplyDelete
  25. Unquestionably beⅼieve thаt which you stated. Your favorite justification appeared to be on the net the ѕimplеst thing to be aѡаre of.
    I say to you, I definitely get iгkeⅾ whіle рeople consider worries that they plainly don't know about.
    You managed to hit the nail upon the top ass well as defіned
    out the wһole ting without having sіde effect , people cann take a signal.

    Will probably be back to ցeet more. Thanks

    ReplyDelete
  26. This blog gives a very good knowledge to us. If you want to know more about mobile app our website also gives useful information to us.
    best mobile app development company in chennai
    flutter app development company in chennai
    Android app development company in chennai

    ReplyDelete
  27. This blog gives a very good knowledge to us. If you want to know more about mobile app our website also gives useful information to us.
    best mobile app development company in chennai
    flutter app development company in chennai
    Android app development company in chennai

    ReplyDelete

^