简体   繁体   English

如何使用CodeIgniter在本地主机上的网站上集成Facebook登录?

[英]How do I integrate facebook login on my website on localhost using CodeIgniter?

I am trying to integrate facebook login on my website on localhost using CodeIgniter. 我正在尝试使用CodeIgniter在本地主机上的网站上集成Facebook登录。 I created a fb_login function in controller user.php where different functions are also defined. 我在控制器user.php中创建了fb_login函数,其中还定义了不同的函数。 The problem is that when I call fb_login function from login_view.php then it doesn't work, where as when fb_login is defined in a separate controller and called, its function then works... 问题是,当我从login_view.php调用fb_login函数时,它不起作用,就像当在单独的控制器中定义fb_login并调用该函数时,它的函数才起作用...

Here's the code: 这是代码:

user.php (controller) user.php(控制器)

public function fb_login(){

        $this->load->library('facebook');                
        $user = $this->facebook->getUser();
        echo $user;
        if ($user) {
            try{
            $user_profile = $this->facebook->api('/me?locale=en_US&fields=name,email');
            }
            catch (FacebookApiException $e){
                $user = null;
            }
        }
        else{
            $this->facebook->destroySession();
        }

        if ($user) {
            $logout_url = site_url('user/logout');
            $user_data =array(
                'user_id' => $user_profile['id'],
                'username' => $user_profile['name'],
                'logged_in' => true
            );

            $this->session->set_userdata($user_data);
            $this->load->view('profile_view');//,$logout_url,$user_profile);
        }
        else{
            $login_url = $this->facebook->getLoginUrl(array(
                'redirect_uri' => site_url('user/login_page'),
                'scope' => 'email'
                ));

            $this->load->view('login_view',$login_url);    
        }

}

but this works fblogin.php (controller) 但这可以工作fblogin.php(控制器)

class Fblogin extends CI_Controller { 

    public function Login(){


        $this->load->library('facebook');

        $user = $this->facebook->getUser();
        echo $user;
        if ($user) {
            try{
            $data['user_profile'] = $this->facebook->api('/me?locale=en_US&fields=name,email');
            }
            catch (FacebookApiException $e){
                $user = null;
            }
        }
        else{
            $this->facebook->destroySession();
        }

        if ($user) {
            $data['logout_url'] = site_url('fblogin/logout');     
        }
        else{
            $data['login_url'] = $this->facebook->getLoginUrl(array(
                'redirect_uri' => site_url('fblogin/login'),
                'scope' => 'email'
                ));

        }
        $this->load->view('login',$data);
    }

    public function logout() {
        $this->load->library('facebook');
        $this->facebook->destroySession();
        redirect('fblogin/login');
    }

}

In Facebook Developer , set the "Site URL" to your local address, " http://localhost/[yourSiteName] ". Facebook Developer中 ,将“站点URL”设置为您的本地地址“ http:// localhost / [yourSiteName] ”。

Facebook does not redirect back to your server. Facebook不会重定向回到您的服务器。 FB JavaScript plugin does and it runs in the context of your browser, which knows where "localhost" points to. FB JavaScript插件可以运行,并且可以在您的浏览器上下文中运行,该浏览器知道“ localhost”指向的位置。

And do not forget to change it back to your domain when your done. 完成后,不要忘记将其更改回您的域。

I would imagine this has something to do with the urls/routes you are using since it works from from one controller and not the other. 我想这与您使用的网址/路由有关,因为它是从一个控制器而不是另一个控制器工作的。 Be certain you are calling the correct url /controller/method and not just calling it like /method thinking that will hit the appropriate controller method. 确保您在调用正确的URL /controller/method ,而不仅仅是像/method这样的调用/method ,它将影响适当的控制器方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM