Solved

PHP back button

Posted on 2010-09-18
11
733 Views
Last Modified: 2013-12-12
Hi Experts,

I'd like to use PHP instead of Java to script a 'back' button in a page.  The previous page is a form so I'd like any of the fields to still have the data in them when the user clicks 'back'.

Any idea how to do this?

Thanks,

HNM
0
Comment
Question by:HelpNearMe
  • 3
  • 2
  • 2
  • +3
11 Comments
 

Accepted Solution

by:
Terry174 earned 84 total points
ID: 33709867
if (isset($HTTP_REFERER)) {
echo "<a href='$HTTP_REFERER'>back</a>";
} else {
echo "<a href='javascript:history.back()'>back</a>";
}
0
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 83 total points
ID: 33709872
Use session to remember the data entered, it is done in two fold first on your form page you need to check if the session are set if not you show the blank fields, when the user submit the form you set the session data so if the user goes back than the fields are already populated.
0
 

Expert Comment

by:Terry174
ID: 33709873
This should probably work better.

<?php
  $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
  echo "<a href='$url'>back</a>";
?>

Although they should both work.
0
 
LVL 2

Assisted Solution

by:WilliamStam
WilliamStam earned 83 total points
ID: 33711361
create a loop of all the fields submitted with hidden fields..

foreach in post thing


then your back button basicaly submits the hidden fields back to your form.. your form inputs

if(isset($_POST['fld'])){
$v = $_POST['fld'];
} else $v = "";

<input type="text" value="<?php echo $v; ?>
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 83 total points
ID: 33711532
That's an interesting question.  Usually developers are more interested in disabling the back button to prevent accidental re-submission of the data.

But if you want to prepopulate the form input fields, you can usually do it without too much trouble.  Often the information comes out of a data base, but it could come from $_SESSION, too.

Install this and run it to see the moving parts.

Regards, ~Ray
<?php // RAY_prepopulate_forms.php

// SET ERROR REPORTING SO WE CAN IGNORE UNDEFINED VARIABLE NOTICES
error_reporting(E_ALL ^ E_NOTICE);

// ALWAYS START THE SESSION UNCONDITIONALLY AT THE TOP OF THE PAGE
session_start();

// IF ANYTHING HAS BEEN POSTED
if (!empty($_POST))
{
    // COPY THE POSTED INFORMATION TO THE SESSION
    $_SESSION['POST'] = $_POST;
    
    // DISPLAY THE CONTENTS OF THE ARRAYS TO SIMULATE PROCESSING THE DATA
    echo "<pre>" . PHP_EOL;
    echo "POST: ";
    var_dump($_POST);
    echo "SESSION: ";
    var_dump($_SESSION);
    
    // ALL DONE
    die("HIT THE BACK KEY NOW");
}

// IF NOTHING HAS BEEN POSTED, DROP INTO HTML AND PUT UP THE FORM
?>
<form method="post">
YOUR NAME: <input name="name" value="<?php echo $_SESSION["POST"]["name"]; ?>" />
<input type="submit" />
</form>

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Assisted Solution

by:phpmycoder
phpmycoder earned 167 total points
ID: 33712031
This may considered semi-vulnerable, but if your form allows for fields to be filled through GET variables (say, form.php?name=Me fills in the name field when the form displays) you could always iterate through all of the given form information and create invisible fields for each. Then the back button simply will submit all this information back to the form. Try something like this:

 
<?php //PhpMyCoder - formprocess.php
$keys = array('name', 'email', 'zip');  //Array of post fields to send back
?>
<form action="form.php" method="get">
<?php foreach($keys as $key): ?>
<input type="hidden" name="<?= $key ?>" value="<?= $_POST[$key] ?>" />
<?php endforeach; ?>
<input type="submit" name="s" value="Back" />
</form>

<?php //PhpMyCoder - form.php ?>
<form action="formprocess.php" action="post">
<input type="text" name="name" value="<?= htmlentities($_GET['name']) ?>" />
<input type="text" name="email" value="<?= htmlentities($_GET['email']) ?>" />
<input type="text" name="zip" value="<?= htmlentities($_GET['zip']) ?>" />
</form>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33712472
"This may considered semi-vulnerable,..."

It is all vulnerable if it depends on unfiltered external data, and it is safe if the script follows the standard, "Accept only known good values."

Every month or so, I like to read all the links on the first two pages here:
http://lmgtfy.com?q=PHP+Security

Best to all, ~Ray
0
 
LVL 1

Assisted Solution

by:phpmycoder
phpmycoder earned 167 total points
ID: 33712570
Hence the htmlentities(), but then again there's lots of other hacks that could apply here and circumvent the htmlentities() function.
My strategy when it comes to vulnerabilities is: learn the attack, practice it on a local machine, and then find a way to prevent it. Usually, though, the last step is really unnecessary since a simple Googling can find a solution.

I can't agree with you more on that quote. Newer programmers never consider the evils of the internet and the dangers of using user generated content.

Also Ray, I can't help but noticing: you really like "let me Google that for you" don't you?
0
 

Author Comment

by:HelpNearMe
ID: 33760880
I'll have some feed back shortly, haven't touched this yet.

Thanks,

HNM
0
 

Author Comment

by:HelpNearMe
ID: 33863386
May be another two days before I can update this question thanks for waiting.

HNM
0
 

Author Closing Comment

by:HelpNearMe
ID: 34026229
I won't be able to test the solutions so I'm just sharing the points and thank you for the effort.

HNM
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now