Skip to content
Advertisement

Laravel – Problem Of How To Tune Strange Behavior With Sending Text Message

I have a very special problem and I don’t know how to deal with it.

I have web App in Laravel, when i open index page, I receive text message to my mobile phone. Problem is, sometimes I receive 2 messages or 3, sometimes 1. Is there a tool how to debug this strange behavior which is not always the same?

A few words about my code: user opens the page, and because its first visit Session doesn’t have attribute message_sent and SendTextMessage::SendMessage($phoneNumber, $id_message, $smsCode, $newDateFormat); is executed. After that Session has message_sent and can’t be sent again, for example if I refresh the page.

SendTextMessage::SendMessage() is Class in Laravel Helpers.

controller code:

public function index($url_attribute, $id_message, Request $request)
{      
    if(!Session::has('message_sent'))
    {
        $user = User::where('id_message', $id_message)->first()->toArray();
        $phoneNumber = $user['mobile_phone'];
        $smsCode = $user['sms_code'];
        $newDateFormat = date("d.m.yy", strtotime($smsExpirationTime));

        $request->session()->flash('message', 'Text message sended.' );

        SendTextMessage::SendMessage($phoneNumber,$id_message, $smsCode, $newDateFormat);
        Session::put('message_sent', true);

    }

    return view('login');
    
}

SendTextMessage Class:

class SendTextMessage 
{
    public static function SendMessage($phoneNumber, $id_message, $smsCode, $newDateFormat)
    {
        $sms = new Connect();
        $sms->Create("user","pass",Connect::AUTH_PLAIN);
        $sms->Send_SMS($phoneNumber,"Message");
        $sms->Logout();
    }
}

Many thanks for any tip or help.

UPDATE:

problem is only in Chrome. Edge and internet explorer are fine.

Advertisement

Answer

You should maybe try to change the way you use Laravel Session. You indicated that it was working fine on some browsers, that means your server-side code is correct so far, but there is someting messing with Chrome… From there, if you take a quick look at the Laravel Session doc, you’ll see that Session can be stored in cookies, and I bet that this is your actual setup (check in your .env file the SESSION_DRIVER constant, or in your config/session.php file).

If so, to confirm that this cookies-based session setting is the culprit, you might want to change the Session config to make it browser-independent: any other option than cookies will work, the database or file options might be the easier to setup… And if it works I would strongly encourage you to keep using this no-cookie setting to make your code browser-safe.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement