dougai2427 2017-10-19 21:00
浏览 97
已采纳

在php中执行某些操作后正确处理回复消息

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.

展开全部

  • 写回答

2条回答 默认 最新

  • douzhang1926 2017-10-19 21:11
    关注

    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']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部