Results 1 to 5 of 5

Thread: session lost problem

  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default session lost problem

    hello everyone....i need your help

    i face session lost problem so many times and every times i just find out other logic to implement the code in different ways. but i don't get a perfect solution.Please help me to resolve this............why it was happened.....
    i searched google for it.........this is the common problem but i dont get any proper solution..............

    recently i write a code to fetch tweets from twitter..but when returning back from .i lost my session value.........

    supp this is my page page.php
    when user click on button........i just check the form submitted like this
    session_start();
    $_SESSION['id']=$id; //supp $id=10
    if($_POST['submit'])
    {
    header("Location:tweet.php ");
    }
    i already included session_start() at the top of every page.

    in tweet.php i just generate a token and send user to twitter for authorization.

    in tweet.php i checked session started
    i check it with
    Print_r($_SESSION);
    in this page i used session like this
    session_start();
    if($_SESSION['id'])
    {
    header("Location:$url") //$url will be authoritarian url generated by twitter oauth..
    }else
    {
    header("Location:index.php")
    }

    after returning from twitter some times session variable are not lost.but sometimes i lost my session.this really irritates me....if logic is wrong then every time session destroyed...............

    i check my php.ini file configuration..

    session_save.path=/tmp; //by default

    register global are off.................

    any help is appreciated................

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Maybe try using:

    PHP Code:
    <?php
    phpinfo
    ();
    ?>
    And check the "session" section - see what value you have for "session.gc_maxlifetime".

    Is it fairly random when your sessions are losing data? Or are you leaving the page for an extended period of time?

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by san2629 View Post
    ...
    after returning from twitter some times session variable are not lost.but sometimes i lost my session...
    just to clarify, are you sending the user away from your site (to twitter.com) and then returning?

    If that is the case, it may be browser settings that are ending the session. Most browsers are configured to end sessions when they close, some after a certain time limit... I don't know of any that end the session when leaving the domain, but it's possible.

  4. #4
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default session lost problem

    thanks schmoopy and traq for you reply.......

    I already check my php.ini file.in this session.gc_maxlifetime is set to 1440
    and i am not leaving the site for long time.

    the main problem is that i loose session only for one time.when first time i try to
    authenticate user through twitter...next time i didn't face this problem.i get this problem in all browser......not every time
    this is my session setting in php.ini file

    Session Support enabled
    Registered save handlers files user sqlite
    Registered serializer handlers php php_binary wddx

    Directive Local Value Master Value
    session.auto_start Off Off
    session.bug_compat_42 Off Off
    session.bug_compat_warn On On
    session.cache_expire 180 180
    session.cache_limiter nocache nocache
    session.cookie_domain no value no value
    session.cookie_httponly Off Off
    session.cookie_lifetime 0 0
    session.cookie_path / /
    session.cookie_secure Off Off
    session.entropy_file no value no value
    session.entropy_length 0 0
    session.gc_divisor 100 100
    session.gc_maxlifetime 1440 1440
    session.gc_probability 1 1
    session.hash_bits_per_character 5 5
    session.hash_function 0 0
    session.name PHPSESSID PHPSESSID
    session.referer_check no value no value
    session.save_handler files files
    session.save_path /tmp /tmp
    session.serialize_handler php php
    session.use_cookies On On
    session.use_only_cookies Off Off
    session.use_trans_sid 0 0

  5. #5
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    this is my code look like

    this is my first page when user request for twitter// sucess.php

    <?php session_start();
    $token=$_SESSION['logintoken']; //login token contain email of the user
    include("config.php");
    require('twitteroauth/twitteroauth.php');
    if($token)
    {
    $sql=mysql_query("SELECT id from f_login where email='$token'");
    $rowloginfetch=mysql_fetch_array($sql);
    $id1=$rowloginfetch['id'];
    $_SESSION['id']=$id1;
    $currentuserid=$_SESSION['id'];
    $decodecurrent=base64_encode($currentuserid);
    $packet=base64_encode($id1);

    $sql1=mysql_query("SELECT * from f_general where gid='$id10'"); //to get name
    $row1=mysql_num_rows($sql1);
    while($row = mysql_fetch_array($sql1))
    {
    $fname=$row['fname1'];
    $lname=$row['lname1'];
    $_SESSION['name']=$fname."&nbsp;".$lname;
    }



    if($_POST['submit'])
    {
    header("Location:tweet.php");
    }




    ?>
    <form method="post" action="<?php echo $PHP_SELF; ?>" name="add">
    <input type="submit" id="Mybutton" style="background-color:#E2E1E0; " value="Add Application" name="submit" onmouseover="fun();" onmouseout="fun1();"/>
    </form>

    //-----------------------------this is my page after redirect

    <?php session_start();
    require('twitteroauth/twitteroauth.php');
    include('config.php');

    $registered=$_SESSION['id'];

    $consumer_key="--XXXXXXXXXXXXXXXXXXXXX----";
    $consumer_secret="--XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    //creating a object and define consumer key and consumer token
    $twitteroauth=new TwitterOAuth($consumer_key,$consumer_secret);
    $request_token = $twitteroauth->getRequestToken('http://abc.com/tweetback.php');
    // Saving them into the session
    $token=$request_token['oauth_token'];
    $token1=$request_token['oauth_token_secret'];
    //now we are calling the url and send above data in it
    if($twitteroauth->http_code==200)
    {
    // Let's generate the URL and redirect
    $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
    if($url)
    {
    $query=mysql_query("select * from f_twitter where loginiduser='$registered'");
    $result=mysql_fetch_array($query);
    if(empty($result))
    {//first time when user does not have any record in database
    $query=mysql_query("insert into f_twitter(token,token_secret,url,loginiduser)values('$token','$token1','$url','$registered')");
    header('Location: '. $url);
    }else
    {//if record exist then update the staus in database
    $query=mysql_query("update f_twitter set token='$token',token_secret='$token1',url='$url' where loginiduser='$registered'");
    session_write_close();
    header('Location:'.$url);
    }
    }else{
    // It's a bad idea to kill the script, but we've got to know when there's an error.
    die('Something wrong happened.');
    }
    }

    ?>

    //this is my page after returning from twitter

    <?php session_start();
    include('config.php');
    require('twitteroauth/twitteroauth.php');

    if($_SESSION['id'])
    {
    $idloginuser=$_SESSION['id'];
    $sqlquery1=mysql_query("select token,token_secret,url from f_twitter where loginiduser='$idloginuser'");
    $rowquery1=mysql_fetch_array($sqlquery1);
    $token=$rowquery1['token'];
    $token_secret=$rowquery1['token_secret'];
    $url=$rowquery1['url'];
    if(!empty($_GET['oauth_verifier']) && !empty($token) && !empty($token_secret))
    {
    $consumer_key="--XXXXXXXXXXXXXXXXXXXXXxxxx--";
    $consumer_secret="--XXXXXXXXXXXXXXXXXXXX----";
    $newobject=new TwitterOAuth($consumer_key,$consumer_secret,$token,$token_secret);
    $tokencall=$newobject->getAccessToken($_GET['oauth_verifier']);
    $_SESSION['access_token'] = $tokencall;
    $user_info = $newobject->get("account/verify_credentials"); //you get userid
    if(isset($user_info->error)) //if generate error send to authorise url of twitter
    {

    header("Location:$url");
    }else
    {
    $query12=mysql_query("select * from users where oauth_provider = 'twitter' AND oauth_uid = ". $user_info->id);
    $result=mysql_fetch_array($query12);
    if(empty($result))
    {
    $verifier=$_GET['oauth_verifier'];
    $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username, oauth_token, oauth_secret,loginid,oauth_verifier) VALUES ('twitter', {$user_info->id}, '{$user_info->screen_name}', '$token', '$token_secret','$idloginuser','$verifier')");
    $query1 = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
    $result = mysql_fetch_array($query1);
    }else
    {
    // Update the tokens
    $query = mysql_query("UPDATE users SET oauth_token = '$token', oauth_secret = '$token_secret', oauth_verifier='$verifier' WHERE oauth_provider = 'twitter' AND oauth_uid = {$user_info->id}");
    }
    $_SESSION['primarykey']=$result['id'];
    $_SESSION['username'] =$result['username'];
    $_SESSION['oauth_uid'] = $result['oauth_uid'];
    $_SESSION['oauth_provider'] =$result['oauth_provider'];
    $_SESSION['oauth_token1'] =$result['oauth_token'];
    $_SESSION['oauth_secret'] =$result['oauth_secret'];
    $home_timeline = $newobject->get('http://api.twitter.com/1/statuses/home_timeline.json?count=30');
    $_SESSION['timeline']=$home_timeline;
    if($texttopost)
    {
    $newobject->post('statuses/update', array('status' => $texttopost));
    if($newobject)
    {
    $_SESSION['sucess']="Your tweet has been posted successfully";
    unset($_SESSION['datatosend']);
    header("Location:tweet.php");
    }else
    {
    $_SESSION['failure']="There is some problem occur when connecting to twitter.Please try again";
    }
    }elseif(!$texttopost && $_SESSION['oauth_token1'] && $_SESSION['oauth_secret'])
    {
    header("Location:success.php?data=true");
    }
    }//end of else--------------------------------------------

    } //end of if

    }else
    {
    header("Location:index.php?data");
    }

    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •