Azad Chouhan Azad Chouhan - 6 months ago 15
PHP Question

$_SESSION is giving error in codeigniter's controller

I am using Facebook login authentication in codeigniter. I have placed the facebook sdk folder in the root of website and using that in the view like this:

<?php
session_start();
include_once("fblogin/config.php");
include_once("fblogin/includes/functions.php");


if(!$fbuser){
$fbuser = null;
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));

}
else{
$user_profile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture');
$user = new Users();
$user_data = $user->checkUser('facebook',$user_profile['id'],$user_profile['first_name'],$user_profile['last_name'],$user_profile['email'],$user_profile['gender'],$user_profile['locale'],$user_profile['picture']['data']['url']);

$_SESSION['email'] = $user_profile['email'];
header('Location:/music/profile');

}

?>


If you see the code carefully, I have added the user email in a session variable. Now I want to access this session variable in my controller. I try two way for this first is this:

My controller constructor

function __construct()
{

parent::__construct();

$this->load->helper('url');
$this->load->helper('form');
$this->load->library("session");
$this->load->model("Profile_model");
}

public function index()
{
session_start();
$name['name'] = $this->session->userdata("name");
$name['email'] = $this->session->userdata($_SESSION['email']);
$this->load->view('Profile_view', $name);

}


This gives me error that a session had already been started:


ignoring session_start(); and Undefined index: email


Second way I used is this:

public function index()
{
session_start();
$name['name'] = $this->session->userdata("name");
$name['email'] = $_SESSION['email'];
$this->load->view('Profile_view', $name);

}


This also give me same error as above.

Answer

You can simply call the user data like this

$name['email'] = $this->session->userdata('email');

as you have already stored it. And in facebook.php you have started a session so why you want to again start a session.

Comments