Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Best way to handle a null $_POST value?

Posted on 2008-11-04
Medium Priority
Last Modified: 2012-06-27
I'm working on PHP page 2 that retrieves a $_POST value from PHP page 1.   Conceivably, the user could browse directly to PHP page 2, resulting in a null (non-existant) $_POST value.  What's the best way to check for this?
Question by:jdana

Accepted Solution

dwaynecharrington earned 252 total points
ID: 22882242
With PHP, there are many different ways you can do this. If you only want to stop people loading the page directly, this will work.

Place the attached code on page 2:

// If data hasn't been posted using POST
    echo 'You did not come here from the previous form!';

Open in new window

LVL 15

Assisted Solution

MMDeveloper earned 248 total points
ID: 22882604
the way I usually do multi-page forms is a single php document that uses sessions to store data and advance page numbers. That script would check a session value and if present, advance to the next page in the page list, if that session value does not exist (someone goes directly to the single processing script), then it defaults to page 1, for example:

....parent file

$_SESSION["pageNum"] = abs(intval($_SESSION["pageNum"]));
if ($_SESSION["pageNum"] > 0) {
      $nextPage = ++$_SESSION["pageNum"];
      $_SESSION["pageNum"] = $nextPage;
else {
      $nextPage = 1;
      $_SESSION["pageNum"] = $nextPage;

include("page_"  + $nextPage + ".php");

that would include page1.php, page2.php, etc..

Assisted Solution

shanikawm earned 252 total points
ID: 22882777
You can use the function "empty". More details,
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 248 total points
ID: 22883037
You can also have HIDDEN field on the page 1 and check it on the POST page whether the hidden values is empty or not... with isset()

ref. http://in2.php.net/isset

$hidden_value = $_POST[''];
if (isset($hidden_value)){
  do something
} else {

Open in new window


Author Closing Comment

ID: 31513358
Thanks for the great solutions!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

564 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