I'm trying to only display some content if values were posted to my page. I'm doing this using the following code:
if(isset($_POST)){
echo "it is in here";
}
The value in the echo is appearing even when the page loads, why would that be?
$_POST are always set, try empty function instead. Try this:
if(!empty($_POST)){
echo "it is in here";
}
$_POST is one of the superglobals in PHP Superglobals - PHP Manual
isset() will return TRUE on any of them, since they are
built-in variables that are always available in all scopes
according to the official manual.
You should either check
isset($_POST['a specific post parameter name'])
Or, check:
$_SERVER['REQUEST_METHOD'] === 'POST'
to determine.
$_POST isset even if it's empty
You can use !empty (not empty), i.e.:
if(!empty($_POST)){
echo "it is in here";
}
Although, you should normally check if a specific $_POST parameter isset, i.e.:
if(isset($_POST["something"])){
echo "something is in here";
}
Resources:
function.empty
reserved.variables.post
function.isset
Related
I was trying to pass an error message to another page using session variable. On the handler page, the session was initialized in the manner:
function sess() {
$index = $_POST['name'];
$_SESSION['error'] = ($index == '') ? 'name cannot be empty' : $index . " is a good name";
header("Location: http://website-name.com/form");
}; sess();
and on the other page is something like:
if(isset($_SESSION['error'])) {
echo $_SESSION['error']; unset($_SESSION['error']);
};
Now if the form is submitted with an empty field, the error name cannot be empty becomes the value of the session. After that, if the name is then set, the value of the session doesn't change but still returns name cannot be empty.
I've checked to see if it has something to do with the fact that the session was called within a function. got no idea! but found online a way to unset all variable using session_unset. But it's actually one session variable that i want to unset and i found something like session_unset($_SESSION['error']). However, using it did not unset all session but the $_SESSION['error'] doesn't get set anymore within the function unless i use the unset($_SESSION['error']) which doesn't terminate the first session that is set.
So what is actually going wrong? how does unset_session() works differently form unset()? what does it has to do with being inside a function? As far as i can recall, $_SESSION[] is a global variable just like $GLOBALS[] which can be access in any scope.
I'm sure this is a rookie question but I get undefined index in the $_POST password parameter.
if ($_SESSION['Password'] == $_POST['Password']){
echo 'Hi 2 '.$_SESSION['Password'].'<br>';
}
If I do the following with either with $_SESSION or $_POST, I get the correct echo line.
if (!empty($_SESSION['Password'])){
echo 'Hi 2 '.$_SESSION['Password'].'<br>';
}
Can it be that same parameters cannot be used for both variables ?
First you should understand what is SESSION and POST variables.
SESSION is used to store a session variable that can be accessed throughout the session from any PHP document. POST is used to get any variable that send through the post method.
In the first code snippet you are checking weather the
SESSION['Password'] == POST['Password']
and you are not providing any value through post method and the post variable 'Password' is not defined in your program to that's why you are getting an undefined error.
So to over come this one the best way is to check weather both of the variable has defined or not by using isset function.
if(isset($_SESSION['Password']) && isset($_POST['Password'])){
if ($_SESSION['Password'] == $_POST['Password']){
echo 'Hi 2 '.$_SESSION['Password'].'<br>';
}
}
Provide the value for POST['Password'] through a correct statement then the code will work.
I think you know how to set values for SESSION variable because you are comparing a SESSION variable with a POST variable.
I am wondering why: without clicking the submit button on a form on my PHP page, the _POST variable is already set.
For example, I have this piece of code on the web page:
if (isset($_POST)){
echo "XXXXXXX";
}
It turns out the XXXXXX is echoed just when the page loads the very first time -- at this point I have of course not submitted any data to the server using POST. Why would this be the case?
As specified on PHP.net, it is automatically created.
This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.
To address your code, it is created, but it's empty.
To better test if the user has made a POST request, simply test for an index on $_POST, like isset($_POST['someFieldName'])
Your code should test if it's empty or not:
if(!empty($_POST)){
echo "a";
}
or
if(isset($_POST['someFieldName'])){
echo "a";
}
$_POST is a superglobal array and it is always set. If you do not pass any value to it using post method it will be empty. So set a value to this array and check whether that value is available or not like this:
if(isset($_POST['submit'])){
//Do this...
}
The same is true for the $_GET superglobal. These are always set regardless of HTTP Method. You can check if a request was a POST, GET, PUT, etc by checking the REQUEST_METHOD
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// this was an HTTP POST request
}
Check if the variable is empty or not. $_POST will always be set.
So something like the following should work:
if(!empty($_POST['textFieldName'])){
echo "XXXXXXX";
}
$_POST is superglobal and it's always set as an empty array.
Try this, just to understand better:
if(!is_null($_POST))
{
print_r($_POST);
}
Why is this going to help you to understand? - Because isset checking if a variable is set and is not NULL.
When my form has been submitted i always check if the form is isset and then check if all fields are also isset
This is my php code :
if(isset($_POST)) {
if(isset($_POST['username'], $_POST['password'])) {
// process
}
}
My question is, does my first check of isset is if the form itself is submitted and then check again for each input if it isset or if the moment i use
if(isset($_POST)) { }
it will actually check of the fields inside that form?
i hope you get my point thanks in advance.
Well, $_POST is always set, so first statement is not necessary.. Check only if specific fields are set.
EDIT: As noted in comment, isset() give you only information, if some variable is set, not if it has some value. empty() can tell you, if you have something in it.
EDIT 2: Just to be sure if empty() will or will not notice you on undefined POST field, try this:
error_reporting(E_ALL);
var_dump(empty($_POST['undefined']));
You will see that empty() works with undefined indexes too.
If you want to check if a post index has been sent, reagardless if it contains an empty string or some data use array_key_exists():
if(array_key_exists($_POST['foo']) && array_key_exists($_POST['bar'])) ..
wtf??
BETTER use $_SERVER['REQUEST_METHOD']
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// process
}
Is it possible to use a session variable, then unset it directly after?
Example:
//====
//Process Form
if ($_POST['Submit']) {
$update = $userSettings->update($_POST);
//If there are form errors
if (!$update) {
//Load the errors into an array
$errors = $update[1];
} else {
//Set the session
$_SESSION['showUpdated'] = true;
//Redirect to this page
header("Location: http://www.mysite.com/settings");
}
}
//==================
if ($_SESSION['showUpdated']) {
echo "Settings Updated";
unset($_SESSION['showUpdated'];
}
So after the form is submitted, if there are no errors:
Set a session to say the form submission was okay
Reload the page (to prevent re-submitted POST data)
If the 'showUpdated' session variable is set, display the "Updated" message
Unset the session variable (so we don't see the message on next reload)
Currently the problem is, if you unset the session variable straight after; It is as if you have un-set it before the "if exists" part.
Any solutions? Is this even the best way to do it?
Many thanks!
I noticed a small error in the original example that might cause other problems.
unset($_SESSION['showUpdated'];
needs to be
unset($_SESSION['showUpdated']);
Not including that end ) in the unset will cause an error.
That looks like it should work. Make sure you call session_start() before trying to use the session, and always exit() or die() after a redirect header.
I accomplish what you're doing a little differently. I keep a 'message' element in the session. I'll stick text in like 'Your data was saved', error messages, etc. Then, on each page (actually in a page template class), I check to see if the $_SESSION['message'] is set and not empty. If there's something there, I display the message and set the value to an empty string or null.
I do this from time to time. I never have any problems with it. But what I would add to yours is an exit() function call after the header redirect.
EDIT: The reason for the exit() is that it will prevent it from processing any further code and will eliminate the possibility of unset before you wanted to check after the redirect.
The header call without an exit after will continue running the page.
header("Location: http://www.mysite.com/settings");
exit;
Using that instead, should kill the page and not unset the session variable on the same page call.
Just check to see if it exists. This is safe to do before it has been defined and will tell you your answer after it has been defined.
if(!empty($_SESSION['showUpdated'])) {
Or you can just set it to false.
if ($_SESSION['showUpdated']) {
echo "Settings Updated";
$_SESSION['showUpdated'] = false;
}
And it looks like you use smaller version of PHP than 5.3, because in 5.3 you'll get notice when you use uninitialized value. So you should use isset function:
if (isset($_SESSION['showUpdated']) && $_SESSION['showUpdated']) {
echo "Settings Updated";
$_SESSION['showUpdated'] = false;
}