-1

I am developing an application on corePHP, whenever I am handling add, edit & delete operations I am redirecting with "header" method along with query strings appending in the URL like "http://mywebsite.com/my_file.php?added=1" and I am checking if that "added" is there or not in the URL with global variables like "isset($_GET['added'])", messages are showing fine but what is my problem here is "whenever I just refresh the page the same page the message is showing again & again as the parameter 'added' is present in the URL. I know there must be a proper way of handling this. So, Can anyone please suggest a proper way of handling this. Thanks.

My code is below: Browser URL

http://mywebsite.com/my_website_page.php?added=1

my_website_page.php:

if(isset($_POST)){
  $is_success = my_method_todo_some_task($_POST);
  if($is_success ){
      header('Location: my_website_page.php?id='.$my_var.'&added=1');
      exit;
  }else{
      header('Location: my_website_page.php?id='.$my_var.'&added=0');
      exit;
  }
}

my_website_page.php :

if(isset($_GET['added'])) { 
 if($_GET['added'] == 1){
    echo '<div class="" style="color:green;font-size:18px;text-align:center">1 Record added successfully</div>';    
 }else{
    echo '<div class="" style="color:red;font-size:18px;text-align:center">Some problem occurred, please try again.</div>';
 } 
}

I know this can be posible with sessions but if we use session then again the same problem will come. So any idea apart from using sessions. Thanks.

Prasad Patel
  • 707
  • 3
  • 16
  • 53

2 Answers2

1

As a quick fix , i would reccomend to set a success or failure message in session See:

       if($is_success ){
    $_SESSION['msg']="Record added successfully";
$_SESSION['successful']=1;
          header('Location: my_website_page.php?id='.$my_var);
          exit;
      }else{
$_SESSION['successful']=0;
    $_SESSION['msg']="Some problem occurred, please try again.";
          header('Location: my_website_page.php?id='.$my_var);
          exit;
      }

and in website.php:

   if(isset($_SESSION['successful']) && $_SESSION['successful'] ) { 

    echo '<div class="" style="color:green;font-size:18px;text-align:center">'. $_SESSION["msg"].'</div>';    
 }
if(isset($_SESSION['successful']) && !$_SESSION['successful'] ) {
    echo '<div class="" style="color:red;font-size:18px;text-align:center">'. $_SESSION["msg"].'</div>';
 }
unset($_SESSION['successfull']);
unset($_SESSION['msg']); 
}

Dont forget add session_start() at top of each page.

Note: There are several other methods to do it like ajax submission.

Blesson Christy
  • 380
  • 3
  • 13
0

There's when session comes in handy

session_start();
if(isset($_POST)){ 
  $is_success = my_method_todo_some_task($_POST);
  if($is_success ){
      $_SESSION['added'] = 1;
      header('Location: my_website_page.php?id='.$my_var.'&added=1');
      exit;
  }else{
      $_SESSION['added'] = 0;
      header('Location: my_website_page.php?id='.$my_var.'&added=0');
      exit;
  }
}


if(!empty($_SESSION['added'])) {
  echo 'Your message here';
  unset($_SESSION['added']);
}
Basheer Kharoti
  • 4,202
  • 5
  • 24
  • 50
  • Thanks Basheer BHAI.. session solved my problem. But what I feel is Blesson Christy's answer is the most accurate for this problem So, I I voted +1 for him and the right answer is for you. – Prasad Patel Oct 20 '17 at 09:37