-1

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.

Deepak Rai
  • 2,163
  • 3
  • 21
  • 36
Novice
  • 363
  • 10
  • 32
  • 1
    Is it the same msg, sent 3 times, or 3 different msgs? Maybe the SMS provider retries bcs sending failed, but didn't really fail? Add a unique code to the msg to see. Do the msgs all arrive at the same time, or spread out over seconds/minutes/..? – Don't Panic Jan 20 '21 at 07:39
  • 1, 2 or 3 same msgs with 2-3 sec delay. 10 times i got 1 msg and 11 try give me two same msgs. – Novice Jan 20 '21 at 07:43
  • 1
    OK so you need to find out if your code is running 3 times, or if the SMS provider/network is resending ... add some logging? – Don't Panic Jan 20 '21 at 07:47
  • how can log function what request sending? – Novice Jan 20 '21 at 07:48
  • 1
    If you add logging in your code, with timestamps, you will see if there is 3 log records, and 3 times, or one. That will tell you if your code is running 3 times, or if the problem is related to the provider somehow resending. – Don't Panic Jan 20 '21 at 07:54
  • mamy thanks, is there any tutorial on web how add logging to laravel what exactly controller do? – Novice Jan 20 '21 at 08:04
  • 1
    [`Log::debug('An informational message.');`](https://laravel.com/docs/8.x/logging#writing-log-messages) – Don't Panic Jan 20 '21 at 08:08
  • I guess this might be something to do with Chrome pre-fetching the page: https://stackoverflow.com/questions/31215875/chrome-loading-script-twice-when-manually-typing-url-to-move-to-next-page, https://stackoverflow.com/questions/11598171/kohana-model-saved-twice This is probably also a good reason to stick with [standard conventions](https://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them) and use a POST for this, rather than GET? – Don't Panic Jan 21 '21 at 08:15

2 Answers2

4

As this script runs on server-side the browser shouldn't be an issue. Based on your code provided, there is no clear answer to give here.

Please try the following in order to debug your problem:

  1. Log messages at each stage of the script in order to see which part was called how often. That will help you to locate the problem. You can use \Log::error("Message") to do that.

  2. Once you know where the problem might be, try to log "decision" making / mission critical variables to logile as well. E.g. \Log::error($session) so that you can understand why that problem might occur. One reason could be that you have a bad configured session caching or your cookies might be messed up. At some point there is probably a piece of data not the way you expect it to be.

philippzentner
  • 396
  • 2
  • 11
1

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.

JBA
  • 2,889
  • 1
  • 21
  • 38
  • ````'driver' => env('SESSION_DRIVER', 'file')```` so sessions are stored in ````storage/sessions/````. do you thing this can be problem for chrome ? – Novice Jan 24 '21 at 17:00
  • @ JBA Is it worth investing time and setting up a seassion relational database? – Novice Jan 24 '21 at 17:03
  • If that's already file-based there is no need to test the database option IMHO, although this issue beeing browser dependent is really strange. Did you also double-check that your .env file does not override this setting? the SESSION_DRIVER in the .env could override that config default value… – JBA Jan 24 '21 at 17:44
  • my .env file dont has SESSION_DRIVER in :/ – Novice Jan 24 '21 at 17:46
  • If that's on your server and you're note 100% sure what the setting value is in the end, you might just push a test debug page where you would ask laravel to echo the value of your config('session.driver') value… – JBA Jan 24 '21 at 17:46
  • If you're confident you're not on cookies, then it's Chrome that is messing up Laravel's Sessions system… A workaround I would try then would be to use the native php session without the Laravel Session layer, just to eliminate any unknown behavior. – JBA Jan 24 '21 at 17:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227769/discussion-between-jba-and-novice). – JBA Jan 24 '21 at 17:51