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.
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']);
}