Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danlito's avatar

Add Session value with ajax post seems not very accurate.

What i am doing is grabbing a value from an input field , passing it through ajax post which is pointed to a function. This function checks if session is empty or not then if false adds this session.

Then i have a simple place where i check this session and do some maths. Everything happens in same page without refreshing anything.

The trouble i am having happens on the result, sometimes it fires sometimes not, maybe this is a bug or i am missing something here. I have tried to keep my code as simple as possible to see what is causing this issue.

Here is me code.

This is my jquery code


$j(document).ready(function(){
$j('#codebtn').click(function(e){
    var code = 'TESTME';
    var codec = $j('#codec').val();
    if(codec != code){
        alert('This is not a valid');
    }else{
        $j.post("addcode", {cname: code, _token: _token});
    }
});
});

This is my function that adds the session


public function addCode(){

        if($_POST['cname']){

            if(!Session::has('__cp')){

                Session::put('__cp', $_POST['cname']);
                Session::save();
            }
        }
    }

As i said before, i get 15 successful out of 20. I don't understand why.

Please advice. Thanks

0 likes
13 replies
bashy's avatar

So have you debugged to see what it fails?

Have you tried returning data on success/fail?

danlito's avatar

Yes bashy i have try lets at my function return 'Yes' and it seems to work most of the time, but in some cases not. This is really weird to me.

bashy's avatar

What part returns failed? Is it the JavaScript part or the PHP part where it checks for the session?

danlito's avatar

What i am doing now is this

$j(document).ready(function(){
$j('#codebtn').click(function(e){
    var code = 'TESTME';
    var codec = $j('#codec').val();
    if(codec != code){
        alert('This is not a valid');
    }else{
        $j.post("addcode", {cname: code, _token: _token}, function(data){
        if(data == 'no'){
        $j.post("addcode", {cname: code, _token: _token});
      }
});
    }
});
});
public function addCode(){

        if($_POST['cname']){

            if(!Session::has('__cp')){

                Session::put('__cp', $_POST['cname']);
                Session::save();
             return 'no';
            }else{
         return 'yes';
}
        }
    }

so basically i am forcing to try it again and again. so at the second try it works. It should not be like this.

bashy's avatar

Why would the first call return yes anyway? You have

if( !
danlito's avatar

I can explain it, i know it's weird but is the only way i can make it work. When i do the first call the (if) statement checks if there is a session or not then try to push the session, i know that sometimes it will fail so for the first attempt i turn a "no" to tell to ajax try it again. Next time when ajax tries again the (if) checks if there is session or not if yes it turns an yes if not it tries again. :) i know is ridiculous but i have spend more then 7 hours to fix this and it looks like the only way is this. Most of the time i end up with two ajax calls because the first attempt fails.

Actually now that i am thinking the yes is wrong there need to be the the opposite and then the opposite at ajax as well. But you get the idea.

bashy's avatar

I get you. I guess it doesn't save the session for some reason, not sure if there's a Session::save() anyway? What session driver are you using?

danlito's avatar

The session drive is:

    'driver' => getenv('SESSION_DRIVER') ?: 'file',
bashy's avatar

So you're using a custom one or the default "file" driver?

danlito's avatar

It's the default one, i haven't touch anything on that session.php file

bashy's avatar

Have you checked the session folder for created sessions?

danlito's avatar

Yes i always check it , when it fails it does not extend the session with the new values, the all sudden it happens.

bashy's avatar

Not sure. Try session table in database?

Please or to participate in this conversation.