How to overpass "Confirm Form Resubmission" in PHP - php

I have 3 pages.
-page1.php sends data through a form with post action
-page2.php recieves data from page1.php. In page2.php there is a link to page3.php
-page3.php does some staff.
When i click to go back from page3 to page2 occures
Confirm Form Resubmission
ERR_CACHE_MISS
I understand the reason of this behaviour. When I take data from page1 to page2 I store some of them in session so i don't need always to use submit. How can i check if submit was done in order to continue normally if no submit was used?
Here is some code of page2:
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
global $dbcnx;
if (isset($_POST['submit']))//if i submit i store in session
{
$_SESSION['team_name'] = $_POST['teams'];
echo $_SESSION['team_name'];
}
else //do nothing
{}
//conitnue code normally
.
.
.
That was my last try. Any response will be helpfull. Thanks in advance!

The usual way to prevent that is to redirect to a page with no post data, click the link to see more info on How to make a redirect in PHP?

The answer is that you should redirect to a specific page using php.that is done with header().
this should be a help:
header("Location: http://www.something.com/any/thing.php") //the page can be anything
Of course, if you're using xampp or wamp or whatever server, use it this way:
header("Location: http://localhost/any/thing.php") //the page can be anything

Related

HTML PHP html php redirect same page with additional message

I am creating a submit contact form. However when the submit button is pressed, i want it to go back to my contact.php page with an additional message at the top of the body page such as.
"We have received your email, our agent will contact you shortly"
i have two files, contact.php for form and send_form_email.php for email process
i have tried this
header("location: contact.php");
however this would only send me back to contact page without any confirmation
Can you please kindly help me ?
Regards,
Lex
The easiest way to do it is a query string.
header("location: contact.php?success=1");
Or something like that.
Then in your contact page, you check for that using $_GET['success'] and if it is set (you can use isset() to just see if its in the URL or check the actual value if you want to do more) display your message.
For something slightly more "complex", see this: PHP passing messages between pages
I suggest using POST in your form. Using GET in this is a bad idea.
http://www.w3schools.com/php/php_forms.asp
Then add
if($_POST["done"] == 1)
echo "We have received your email, our agent will contact you shortly";
You have a couple options.
The simplest would be to just have the form processing logic on the same page and then set your form's action attribute to that page.
You could do something like checking if you have a value posted to tell you if you need to process the page.
if (isset($_POST['my_value_from_form'])) {
// process
}
// body of page itself
That lets you put one pretty easily.
Another way would be to add a GET value by appending it to the URL:
header("location: contact.php?message=1")
And use that with $_GET['message'] to determine what to show. However, that ?message=1 will be in the URL of the page, so it may not be ideal.
The other way would be to set a session value before you direct, then check if that value is there (and also clear it after you display it.
// on send_form_email.php
session_start();
$_SESSION['message'] = 1;
// on contact.php
session_start();
if ($_SESSION['message'] == 1) {
// do something
}
unset($_SESSION['message']); // so it only shows once.
All ways have minor trade-offs, mostly just owing to how you organize your code. If I was going to implement it, I would use the session method.
In file Contact.php add
$done = $_GET['done'];
if ($done == 1){
echo "<div>We have received your email, our agent will contact you shortly</div>";
}
In file send_form_email.php add
header("Location: contact.php?done=1");
change your header to
header("location: contact.php?success");
and then on your contact.php add this at the top of your page
if(isset($_GET['success'])){
echo "We have received your email, our agent will contact you shortly";
}
you can make a session of message like this
$_SESSION['message'] = "your message";
and after that you do redirect insert this session inside message div and then unset it
<?php
session_start();
$_SESSION['message'] = "your message";
header("location: contact.php");
dont forget to start the session with session_start(); on top of your file contact.php
You need to use sessions for that. Example
session_start(); // start session
$_SESSION['message'] = 'We have received your email, our agent will contact you shortly';
header("location: contact.php");
In your contact.php
echo $_SESSION['message'];
unset($_SESSION['message']); // delete message so it doesnt display again

how to assign post globals on a redirected page?

I have a login form which sends 3 post values from username, password and submit button. But my form processor has 3 pages one is validation.php which validates the field second is read.php which checks the posted values against db and third is login.php which is a result of login success. All redirect to each other respectively on success. Problem here is that when I try to access the user posted values from form in read.php (redirected page) not validate.php (action page) I get an error of undefined index.
I really don't see why you are doing all those redirects, but if you want to make the data more persistent you could use a session variable, because the $_POST superglobal is only set for the current request.
firstfile.php
<?php
session_start();
$_SESSION['posted_data'] = $_POST;
other file
<?php
session_start();
var_dump($_SESSION['posted_data']);
However as already stated you may really want to reconsider doing all the requests.
UPDATE
Besides the fact that you will loose your data you are also doing multiple (unneeded) requests to simply sumbit the form. The only redirect that should happen is to the successpage when you have done all you work. See this for more information: http://en.wikipedia.org/wiki/Post/Redirect/Get
If you are look to keep you code clean you could always just include the other files or go for an OOP approach.
You should do one page only that will do all the work. That doesn't seem too complicated of a script, so I would advise putting everthing together on one page.
You did not provide any code so I'll show you a general example. I just typed it without rereading so it's not pure PHP syntax, just the spirit:
<?php
$login=$_POST['login'];
$pwd=$_POST['pwd'];
$dbcheck = mysql_fetch_array(mysql_query("SELECT COUNT(1) FROM table WHERE user =$login and pwd = $pwd"))
if($dbcheck[0] > 0) {
//Login success
//Setup your session variables, cookies, etc
//Then you can do your redirect here
} else {
//Page for wrong login
}

Unset POST values in php

acitvity.php
//Form start
<form action=''>
</form>
//Form End
//Get POST Values
<?php
$_POST[''];
?>
//End
if i refresh the page after form is submitted, all the posted values are resubmitted, reason because all values are in browser so they are resubmitted. When i was searching solution for this, i got info that if the form & post operation done in separate php file then no more issue in posting values on refresh.
Is this the solutions? but now i have to do both in single file & POST values should not be submitted again on refresh.. is there any way to do this???
Learn PRG Pattern so that you can do this properly :)
http://en.wikipedia.org/wiki/Post/Redirect/Get
For example, you are trying to handle a user registration form, so what you do is you get a bunch of POSTed values, and save it into your database.
if(!empty($_POST)) {
// validate and save to db
// get last inserted user_id
}
After you do that, instead of returning the same page with the previously POSTed values, you redirect the new user, for example to his profile page (assuming you have no activation requirement in place)
if(!empty($_POST)) {
// validate and save to db
// get last inserted user_id, say in $user_id
header("Location: /users/$user_id");
}
That way, the browser redirects and you won't have problem with say, double registration, whenever the user hits refresh.
After saving to your database, reload your page:
if ($_POST) {
// Save $_POST to database and other stuffs
// Reload current page to discard $_POST
header('Location: my_page.php');
}
That's called PRG or Post/Reload/Get
You can use unset($var) to unset a variable. However, I think the issue is with the browsers; some of them try to be smart and will remember form data regardless when you refresh the page. If you hit "go" or "enter" on the URL bar it does a "true" refresh though.

Redirect not working with Header(Location ) and session variable

1: i use register.php to sign up the clients,
2: the data collected from the form is send to 1.php, it is saved in database
3: after form data is saved in database, 1.php forwards selected form data (myValue) to register.php?myValue='abc'
in 1.php, i am saving a session variable like this
#session_start();
$_SESSION['color']='blue';
the code of register.php is
if (isset($_SESSION['color'])) {
header('Location: http://mydomain.com/thankyou.php');
}
else {
#session_start();
some other stuff that was initially use for signing up the clients
my logic is to check for session variable and to redirect it to some-other page
when step 1 , step 2 and step 3 are complete, page should be
redirected to thankyou.php
currently, when step 1, step 2, step 3 are done, instead of opening thankyou.php, the following page is being opened
http://mydomain.com/register.php?myValue='abc'
however, if i re-open register.php or go back to step one (opening register.php), thankyou.php is displayed...
can somebody guide me where i am doing the blunder? why redirection is not being successful although session variables are being created?
code Update
i tried the following code at the top of my register.php
#session_start();
if (isset($_SESSION['color'])) {
header('Location:http://mydomain.com/thankyou.php');
exit;
}
else{
remaining stuff
it occasionally do the trick, redirects to the page, while on occasion (greater in number), it fails in redirecting to thankyou.php,, also the code needs to delete complete history and cache to work (after doing so, still miss hits occurs..)
Make sure you use exit(0); right after you do a header redirect otherwise php will still parse and run the rest of your script, sometimes it can cause some funny behaviour.
In your register.php, you can't test for the session variable before you issue the session_start, so your code should be more like:
session_start();
if (isset($_SESSION['color'])) {
header('Location: http://mydomain.com/thankyou.php');
}
else {
// Something else....
EDIT:
Another thing I've found useful when trying to set session variable in conjunction with redirects is to proceed to the redirect only after running a function. Here's how it would work:
$throwAwayVariable = setColor('blue');
if($throwAwayVariable ){ // separated out into a function so it wouldn't redirect before the session variable was saved
session_write_close();
header("Location: http://mydomain.com/thankyou.php");
}
function setColor($color){
#session_start();
$_SESSION['color']='blue';
return true;
}
Since not all your code is posted, you'll have to figure out where this goes, but I've always had my session vars work after this process.
Your session_start() call in register.php needs to be BEFORE you call any $_SESSION variables.
I have the same issue, then I try to add session_start and session_write_close, and it works!
session_start();
$_SESSION['status'] = 'Updated Poem successfully';
session_write_close();
header("location: index.php");

Allow Form to go to page but dis-allow url based access

Good evening Stack members,
I have no experience with that im about to ask so it may be a totally stupid idea.
We have a few different pages that ask for different bits of information
On each form we have the post to the next page , then we have php code to collect the information from the previous page and fill in the next page - we have bits of code on the second page that rely on the first page to be filled in else they will just stay blank.
What we were wondering is .. is that any way for us to deny requests via the web if someone went to page2.php it redirected to page1.php or just said access denied but yet allowed access if our form posted information to page 2
I'm sorry if this is quite messy and i do agree if you rate me down but im just a beginner and trying to figure this out for myself , I understand a lot of you are quite knowledgeable and would be grateful for any information at all
Thanks
So basically to recap
Page 1 > User fils in information > pass > page2.php
User tries to enter page2.php into their browser url window >> denied >> redirect
on page 1 put a hidden value in the form,
<input type="hidden" id="page" name="page" value="1" />
on page 2
if($_POST['page'] !='1'){
header('Location: http://www.example.com/page1.php');
exit();
}
You could also use sessions
At the top of page1.php:
<?php
session_start();
$_SESSION['last_page'] = 1;
// your code
At the top of page2.php:
<?php
session_start();
if(! isset($_SESSION['last_page']) && $_SESSION['last_page'] == 1){
header('Location: http://domain.com/page1.php');
exit(0);
}
// if you have more pages increment the last_page count
$_SESSION['last_page']++;
// your code
You can check the REQUEST_METHOD variable to make sure the user came to the page via a POST request, like so
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
//Your code goes here
}
Or alternatively, check to see if they came by an alternate method (e.g. GET) and acct accordingly
if ($_SERVER['REQUEST_METHOD'] != 'POST')
{
//send the user back to page one
header('Location: page1.php');
//don't allow the script to continue
die('access denied!');
}
//Your code goes here
Yes, it is. You just need to store a variable (probably in the current $session user array, or in a relative database table / file / whatever it happens to be) as a flag for the current $index of that form proccess.

Categories