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?
jdanaAsked:
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.

dwaynecharringtonCommented:
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:

<?php
// If data hasn't been posted using POST
if(!$_POST)
{
    echo 'You did not come here from the previous form!';
}
?>

Open in new window

0

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
MMDeveloperCommented:
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..
0
shanikawmCommented:
You can use the function "empty". More details,
http://www.php.net/empty
0
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()

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

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

Open in new window

0
jdanaAuthor Commented:
Thanks for the great solutions!
0
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
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.