472,122 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Problem with $_POST variable

Hi,

I'm learning PHP and prepared a simple login form. validate.php does
the validation and it was behaving erratically. So, I did var_dump of
$_POST variable and it's NULL. Did I miss anything here regarding the
configuration or code? Code for validate.php is given below.

<?php
var_dump($_POST);

$user=$_POST['login'];
$password=$_POST['password'];

// .... Rest of the validation ...
?>
Any kind of help would be highly beneficial.

Jul 17 '05 #1
10 14576
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable ar**************@gmail.com's handwriting:
I'm learning PHP and prepared a simple login form. validate.php does
the validation and it was behaving erratically. So, I did var_dump of
$_POST variable and it's NULL. Did I miss anything here regarding the
configuration or code? Code for validate.php is given below.

<?php
var_dump($_POST);
Well, if you DUMP the $_POST var here...
$user=$_POST['login'];
$password=$_POST['password'];


.... then obviously you won't get anything here. $_POST is dumped, so
there is no such thing as $_POST[whatever]. Don't dump. :)

Besides, you should check whether there is anything in those vars:
if ( (isset($_POST['login'])) && (isset($_POST['password'])) ) then
{
// Validation here
}
else
{
// Error message ("No username/password supplied" for example) here
}

Cheers
Mike
Jul 17 '05 #2
My original code is
<?php
$user=$_POST['login'];
$password=$_POST['password'];

// .... Rest of the validation ...
?>

login and password fields were not empty when the form was submitted.
But still, $_POST['login'] and $_POST['password'] were not set (I've
confirmed it using isset() functions).

Can someone help?

Jul 17 '05 #3
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable ar**************@gmail.com's handwriting:
My original code is
<?php
$user=$_POST['login'];
$password=$_POST['password'];

// .... Rest of the validation ...
?>

login and password fields were not empty when the form was submitted.
But still, $_POST['login'] and $_POST['password'] were not set (I've
confirmed it using isset() functions).

Can someone help?


Could you also include:
1. the form's HTML code
2. The filenames of both files (the form's file and the validating file
Jul 17 '05 #4
arun.kumar.va...@gmail.com wrote:
login and password fields were not empty when the form was submitted.
But still, $_POST['login'] and $_POST['password'] were not set (I've
confirmed it using isset() functions).


Sure your login and password fields in the form are named correctly? =\

Jul 17 '05 #5
I'm really sorry guys. PHP version on my server is 4.0.6. Found it out
with phpinfo()

Came to know that $HTTP_POST_VARS is the variable to use for the
versions earlier than 4.1.0

Jul 17 '05 #6

Michal Wozniak wrote:
<?php
var_dump($_POST);


Well, if you DUMP the $_POST var here...
$user=$_POST['login'];
$password=$_POST['password'];


... then obviously you won't get anything here. $_POST is dumped, so
there is no such thing as $_POST[whatever]. Don't dump. :)

This is absolutely false.

The "dump" in var_dump should be thought of as "dumping output to
screen". It is analagous to print or echo. It does not unset the
variable, or change it in any way.

It is very useful to use var_dump in the way the original poster used
it, because "print" would not work ( $_POST is an array ) and "print_r"
would produce incomplete results ( because $_POST might contain nested
arrays ). var_dump will echo all the contents of $_POST which is
useful for testing purposes.

Jul 17 '05 #7
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable Ramius's handwriting:
se var_dump in the way the original poster used
it, because "print" would not work ( $_POST is an array ) and "print_r"
would produce incomplete results ( because $_POST might contain nested
arrays ).**var_dump*will*echo*all*the*contents*of*$_POST* which*is
useful for testing purposes.


Oooops... Sorry, my mistake. :/
I stand corrected.

Regards
Mike
Jul 17 '05 #8
My "workaround" for this problem is something like:

put this code in any function that needs $_POST, $_GET, etc...
if ( !isset($_SERVER) )
{
global $HTTP_POST_VARS;
$_POST = &$HTTP_POST_VARS;
// do this for any other superglobal $_whatevers you need
}

Jul 17 '05 #9
ar**************@gmail.com wrote:
Hi,

I'm learning PHP and prepared a simple login form. validate.php does
the validation and it was behaving erratically. So, I did var_dump of
$_POST variable and it's NULL. Did I miss anything here regarding the
configuration or code? Code for validate.php is given below.

<?php
var_dump($_POST);

$user=$_POST['login'];
$password=$_POST['password'];

// .... Rest of the validation ...
?>
Any kind of help would be highly beneficial.

Odds are your problem is with your html. Double-check your variable
names in the html form as well as in your php source. Also, check your
form tag in your html. You may inadvertantly be using GET instead of
POST, in which case $_POST will contain nothing. Also, update to the
latest stable release of PHP. See below for an example.

test.html:
<form action="login.php" method="POST">
Username: <input type="text" name="user"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Submit">
</form>

login.php:
<?php
$user = isset($_POST['user']) ? $_POST['user'] : '';
$pass = isset($_POST['password']) ? $_POST['password'] : '';

echo 'Username: '.$user.'<br>Password: '.$pass;
?>
Jul 17 '05 #10
Ramius wrote:
"print_r"
would produce incomplete results ( because $_POST might contain nested
arrays ). var_dump will echo all the contents of $_POST which is
useful for testing purposes.


print_r() prints nested arrays.

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Thomas Hoheneder | last post: by
3 posts views Thread by J˙Gius³ vs ::NRG::ius | last post: by
4 posts views Thread by Lee David | last post: by
1 post views Thread by unknown; | last post: by
5 posts views Thread by pavloutefkros | last post: by
reply views Thread by Terry Reedy | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.