简体   繁体   中英

Codeigniter loses session after redirect

I am using Abraham Williams twitter api in order to log in the user. During step one I store the temporary oauth_token and oauth_token _secret in the session. After the user is redirected to my page after the sign in process the session data stored previously are lost. How can I solve this ?

function oauth()
{
    //Build TwitterOAuth object with client credentials
    $connection = new TwitterOAuth($this->consumer_key, $this->consumer_secret);

    //Get temporary credentials
    $request_token = $connection->getRequestToken($this->callback);

    //Save temporary credentials to session

    $session_data = array(
        'oauth_token'       => $request_token['oauth_token'],
        'oauth_token_secret'=> $request_token['oauth_token_secret'],
    );

    $this->session->set_userdata($session_data);


    //If last connection failed don't display authorization link.
    switch ($connection->http_code)
    {
        case 200:
            $url = $connection->getAuthorizeURL($request_token['oauth_token'], TRUE);
            header('Location: ' . $url);
            break;

        default:
            echo 'Could not connect to Twitter. Refresh the page or try again later.';
    }

}

function callback()//callback after user signs in with twitter
{

    $connection = new TwitterOAuth($this->consumer_key, 
                                   $this->consumer_secret,
                                   $this->session->userdata("oauth_token"),
                                   $this->session->userdata("oauth_token_secret"));

    $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);

    $this->session->set_userdata('access_token', $access_token);

    //Remove no longer needed request tokens
    $this->session->unset_userdata('oauth_token');
    $this->session->unset_userdata('oauth_token_secret');

    //If HTTP response is 200 continue otherwise send to connect page to retry
    if (200 == $connection->http_code)
    {
        $this->session->set_userdata('twitter_log_in', TRUE);
        redirect('/main/', 'refresh');
    }

}

Of course you verified you config file in Session section, but:

  1. Already you tried it in other PC?
  2. Tried DB Sessions or the inverse?
  3. Made you simple tests excluding you case (twitter oauth class)
  4. In somehow, maybe the code are replacing the vars, try log the informations in many parts of the code, to see if it as overwrote in the life of the process

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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