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

x
?
Solved

Best way to handle a null $_POST value?

Posted on 2008-11-04
5
Medium Priority
?
459 Views
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?
0
Comment
Question by:jdana
5 Comments
 
LVL 6

Accepted Solution

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

<?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
 
LVL 15

Assisted Solution

by:MMDeveloper
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..
0
 
LVL 8

Assisted Solution

by:shanikawm
shanikawm earned 252 total points
ID: 22882777
You can use the function "empty". More details,
http://www.php.net/empty
0
 
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 {
 warning
}

Open in new window

0
 

Author Closing Comment

by:jdana
ID: 31513358
Thanks for the great solutions!
0

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