Arzozeus
Arzozeus

Reputation: 103

Read session when click back button instead of $_POST value

I have 3 page, and pass value using $_POST method. The reason not using $_GET method is I don't want user go to the url and change the value and able to pass value. From the beginning for t2 and t3 page:

  1. t2 and t3 will check if(isset($_POST)) then save to the session.
  2. When t3.php user click on back button, t3 not passing $_POST data and read session

It is able to work when passing data from t1 to t2 and t2 to t3, but when click on back button from t3 to t2, the browser show "Confirm Form Resubmission". How can I make the page read the session data instead of the $_POST value when user hit the back button?

t1.php

<html>
<body>
    <form id="b">
        value1: <input type="text" name="value1" id="value1">
        value2: <input type="text" name="value2" id="value2">
        <input type="button" onclick ="test()" value="test">
    </form>
</body>
</html>

<script type="text/javascript">
function test(){
    document.getElementById("b").method = "post";
    document.getElementById("b").action = "t2.php";
    document.getElementById("value1").value = "asd";
    document.getElementById("value2").value = "zxc";
    document.getElementById('b').submit();      
}
</script>

t2.php

<?php 
session_start();

if(isset($_POST)){
    unset($_SESSION['t2']);
    $_SESSION['t2']=$_POST;
}else if(isset($_SESSION['t2'])){

}else{
    header("Location: http://localhost/my_project/t1.php");
}

?>

<html>
<body>
    value1<?php echo $_SESSION['t2']["value1"]; ?>!<br>
    value2 <?php echo $_SESSION['t2']["value2"]; ?>.

    <form id="b">
        value1: <input type="text" name="value1" id="value1">
        value2: <input type="text" name="value2" id="value12">
        <input type="button" onclick ="test()" value="test">
    </form>

</body>
</html>

<script type="text/javascript">
function test(){
    document.getElementById("b").method = "post";
    document.getElementById("b").action = "t3.php";
    document.getElementById("value1").value = "qwe";
    document.getElementById("value2").value = "rty";
    document.getElementById('b').submit();      
}
</script>

t3.php

<?php 
session_start();

if(isset($_POST)){
    unset($_SESSION['t3']);
    $_SESSION['t3']=$_POST;
}else if(isset($_SESSION['t3'])){

}else{
    header("Location: http://localhost/my_project/t1.php");
}

?>

<html>
<body>
    value1<?php echo $_SESSION['t3']["value1"]; ?>!<br>
    value2 <?php echo $_SESSION['t3']["value2"]; ?>.

</body>
</html> 

Upvotes: 3

Views: 2780

Answers (2)

Mohamed Amine Ayachi
Mohamed Amine Ayachi

Reputation: 170

if youre losing data when you press navigater back button and as result the page is not loaded you can simply add this session_cache_limiter('private_no_expire'); before the session_start(); this will keep the page loaded as it is even if you press back button and as another way you can use get method and not post for passing parameters because the first method session_cache_limiter('private_no_expire'); will create some problems

Have a nice day

Upvotes: 1

Fuzzzzel
Fuzzzzel

Reputation: 1773

If you hit the "back" button in your browser, your browser tries to recreate the situation/status that was present when first calling that page.

That means, you do the following

1) t1 --> t2
2) t2 --> t3
3) Hit the "back" button in your browser

Now your browser tries to recreate situation 1) to display page t2 and by this wants to re-post the data you sent when you first called t2.

One way around this is to always load existing data from your session and to post your form via ajax to decouple the request of the next page and sending of data. If sending of data only happens when you execute that function (e.g. clicking the submit button which is bound to that function) from the browsers perspective there's no data needed to call the page.

If you follow this way, your page is alsways loaded with data that was stored in your session (and by this is the latest data). Only if you submit data via ajax your session data is updated.

Some options are explained in multiple answers to this question: jQuery AJAX submit form

I want to add some code snippet (jQuery/AJAX - not tested). It works this way:

  • Clicking the submit button you send the form data to t2.php.
  • In t2.php you check if form data was sent. If so, process it.
  • After the call was handled and a response is sent, the first call is finished, without loading the actual page.
  • Then (here "success: ...") t2.php is loaded to the browser without sending form data. Last part is what the browser executes when it opens t2.php and which will be the action performed when clicking the back-button.

Code Snippet:

// You can also reference the jQuery library differently than this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">

// Will bind the function, once the page is loaded
$(document).ready(funtion(){

// Gets a reference to your form
var $form = $('#yourFormId');

// Gets the target URL of your form
var formURL = form.attr("action");

// Will be execured when you click the submit button
$form.submit(funtion(e){
    // Does stop the form from submitting data itself
    e.preventDefault();
    // Call to send data
    $.ajax({
        type: "POST",
        url: formURL,
        data: $(this).serialize(), 
        cache: false,
        success: function() { window.location.href = formURL; }
    });
  });
});
</script>

Upvotes: 1

Related Questions