Novice
Novice

Reputation: 393

Laravel protect function against "refresh" page

When i open the web page, laravel via my custom class and function send me to text message on my mobile, but its possible to block sending when i press F5 or refresh web page ?

controller

public function index()
{
    $phoneNumber = $user['mobile_phone'];
    $smsMessage= $user['smsMessage'];
    SendTextMessage::SendMessage($phoneNumber, $smsMessage);

    return view('welcome');
}

Upvotes: 1

Views: 594

Answers (2)

Sandra
Sandra

Reputation: 21

Try:

public function index()
{
   if(url()->previous()!=url()->current()){
      SendTextMessage::SendMessage($phoneNumber, $smsMessage);
   }

   
    return view('welcome');
}

URL history is stored in session, so it can be a solution for page refresh.

Upvotes: 0

Harpal Singh
Harpal Singh

Reputation: 702

If you are sending message only when user visit welcome page, then you can set a flag in session and check if session value is not exists only then send message.

use Session;
public function index()
{
    if(!Session::has('message_sent')){
       SendTextMessage::SendMessage($phoneNumber, $smsMessage);
       Session::put('message_sent', true);
     }
    
    return view('welcome');
}

Upvotes: 4

Related Questions