dougai2427 2017-10-20 05: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-20 05: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条)

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面