Welcome Guest, Not a member yet? Register   Sign In
Session creates new session id on every page load
#51

[eluser]timpiele[/eluser]
So this is bizarre:

I have a view (settings.php) that passes a few values to a controller via jQuery $.post and the controller stuffs those values into session variables, like this:

The View
Code:
$.post('/settings/save_session', { one:one, two:two, three:three, four:four },

     function(data){
          // do stuff
     }
);


The Controller
Code:
// code to sanitize the post data goes here

// push it into the session
$newdata = array(
     'one'  => $one,
     'two'  => $two,
     'three'  => $three,
     'four'  => $four
);

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

So if I start out my browsing of the website on the "/settings" view, it will create a new session row in the database and when I browse to another page it will create a second one, and keep adding a new row for each subsequent page I visit. But if I start on any other page first, say the site index or the contact us page, and then go to the "/settings" page, sessions work as expected.

So I ran across this post: http://ellislab.com/forums/viewthread/172415/

I added the define() and created the library and autoloaded it from my /config/autoload.php file and still no luck. If I start off on a view that doesn't have any AJAX requests then everything works as expected.

Is the MY_Session library not seeing the $.post code as AJAX thus not firing? $.post (I believe) is just shorthand for $.ajax() in jQuery.
#52

[eluser]CroNiX[/eluser]
That define() is a bit outdated (although it should work) and CI has a native command for it now:
Code:
$this->input->is_ajax_request()

Not sure about your last question as you aren't really sending "ajax", you are sending post variables via an ajax request. How are you retrieving the post variables? By the way you are sending them it looks like it would be
Code:
$one = $this->input->post('one');
$two = $this->input->post('two');
etc.

Are you checking in that controller that receives those to see if they are being sent and received correctly?
#53

[eluser]timpiele[/eluser]
Okay sweet, I'll try $this->input->is_ajax_request().

Yes I am gathering them with $this->input->post('one') etc...

Yes they are showing up in the controller unmolested, I can echo them in the controller which sends them back to the "data" variable in the view, and use alert(data) to see the values... in fact I know the values are making it into the session and database because technically it works...

If I start out on the index of the website, and then go to "/settings" and run the $.post code by clicking on the trigger element, the post data (one, two, three, four) show up in the ci_sessions->user_data column in the database and when I go back to the session page they are displayed in the HTML as they should be.

It's just that if I start out by typing "https://mydomain.com/settings" in the browser, then I get a new session row in the database for each subsequent page load, including refreshes.

I'm going to try changing $.post to $.ajax to test that and also put a similar $.post/$.ajax on different views to see if they cause the same problem, then report back.

Any other ideas in the meantime?
#54

[eluser]Vadorequest[/eluser]
hanks ! It's because config cookie, my session_id change again and again.

$config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
It's better as that ^^

But I've always a problem... My BDD is updated with local session php but my vars in user_data as been destroyed >< user_data also.

Find ... find...
#55

[eluser]cehennem[/eluser]
I have the same issue and solved it now.

Just make sure that server time is correct according to your localtime. If this not works, just set session_expiration to 0 (zero)

$config['sess_expiration'] = 0;

#56

[eluser]Vadorequest[/eluser]
Already.

I'm passed to phpsession and not CI session, so bugged.

phpsession is for CodeIgniter but not use session CI.
#57

[eluser]InsiteFX[/eluser]
Here is the new MY_Session Class
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by phpDesigner 8.
* Date : 5/6/2012
* Time : 11:19:06 PM
* The Learn CodeIgniter Development Team.
* ------------------------------------------------------------------------
*
* Class MY_Session
*
* @package  Package  CodeIgniter
* @subpackage Subpackage session
* @category category session
* @author  Raymond L King Sr.
* @link  http://example.com
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

/**
* ------------------------------------------------------------------------
* CI Session Class Extension for AJAX and Xajax method calls.
* ------------------------------------------------------------------------
*
* ====- Save as ./application/libraries/MY_Session.php -====
*/

class MY_Session extends CI_Session {

// --------------------------------------------------------------------

/**
  * sess_update()
  *
     * Do not update an existing session on ajax or xajax calls
     *
     * @access    public
     * @return    void
     */
    public function sess_update()
{
  $_ci = get_instance();

  if ( ! $_ci->input->is_ajax_request())
  {
   parent::sess_update();
  }
    }

// --------------------------------------------------------------------

/**
  * sess_destroy()
  *
     * Clear's out the user_data array on sess::destroy.
     *
     * @access    public
     * @return    void
     */
public function sess_destroy()
{
  $this->userdata = array();

  parent::sess_destroy();
}

} // End of Class.

/* ------------------------------------------------------------------------
* End of file MY_Session.php
* Location: ./application/libraries/MY_Session.php
* ------------------------------------------------------------------------
*/

1) Make sure you are using the newest CodeIgniter Session database table.
2) Make sure that your server time is set correct.
3) Make sure that your computer system OS time is set correct.
4) Session cookies only hold 4kb of data.

#58

[eluser]xylude[/eluser]
I didn't read all 5 pages of this thread, but here's what fixed it for me. My situation is that I'm running multiple sites with a very similar build of CI and I believe there was some naming collisions with my cookies.

I changed the value for $config['cookie_prefix'] and now everything is working fine.

Hope this helps someone Smile
#59

[eluser]Unknown[/eluser]
I had the same problem as the original question... session was creating a new id on every page load so a new row was being added to the ci_sessions table every time.

Turns out I had an incorrect value in
Code:
$config['cookie_domain']
. I changed it to a correct value, in the form:

Code:
$config['cookie_domain'] = ".your-domain.com";

and that fixed the problem.

If you don't need this setting, just leave it blank and see if that takes care of it. Hope it helps someone!
#60

[eluser]alexwenzel[/eluser]
I have had the same problem. So i stumbled about this thread.

In order to prevent you all of hours of anger (like i experienced):

$config['sess_cookie_name'] = 'mydomain.com';

This config setting results in a new session id every request.

!!! The problem was the dot (.) within the session cookie name.

By removing it all works as expected again.




Theme © iAndrew 2016 - Forum software by © MyBB