Best way to handle a null $_POST value?

Posted on 2008-11-04
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
    LVL 6

    Accepted Solution

    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

    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..
    LVL 8

    Assisted Solution

    You can use the function "empty". More details,
    LVL 36

    Assisted Solution

    by:Loganathan Natarajan
    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


    Author Closing Comment

    Thanks for the great solutions!

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    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 …

    754 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

    19 Experts available now in Live!

    Get 1:1 Help Now