Best way to handle a null $_POST value?

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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..
You can use the function "empty". More details,
Loganathan NatarajanLAMP DeveloperCommented:
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()


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

Open in new window

jdanaAuthor Commented:
Thanks for the great solutions!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.