Hi, this line if (empty($_POST['userName'])) worked well on wamp 2.1, but strange things started to happen when i uploaded my code to host. Now this line sometimes returns true and sometimes false no matter that i always pass unempty string from form. Even at times when empty returns true, line echo $_POST['userName'] prints the value I typed in which means that $_POST['userName'] should have returned false . Any help?
empty worked well on wamp but fails to work properly in host
Started by thatsme, Sep 09 2011 07:47 AM
7 replies to this topic
#1
Posted 09 September 2011 - 07:47 AM
|
|
|
#2
Posted 09 September 2011 - 07:48 AM
Can you post some code
#3
Posted 09 September 2011 - 09:55 AM
The code is here:
<?php
require_once("stdlib.php");
require_once("sqlInjectionPreventer.php");
$con = connectToDatabase();
mysql_select_db("wlv_warfarehistory", $con);
if (empty($_POST['userName']))
printErrorMessage("Username is not filled in");
$username = htmlentities($username);
$username = SqlInjectionPreventer::cleanQuery($username);...
I can post further code if needed
#4
Posted 09 September 2011 - 10:03 AM
First you should always test if the variable exist like so:
Second, if you wish to test if their anything enter in this variable you should use
From php.net manual:
Returns FALSE if var has a non-empty and non-zero value.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
So empty do more than just looking for a empty string
if (isset($_POST['userName'])) ...
Second, if you wish to test if their anything enter in this variable you should use
strlen(trim($_POST['userName']))insted of empty.
From php.net manual:
Returns FALSE if var has a non-empty and non-zero value.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
So empty do more than just looking for a empty string
#5
Posted 09 September 2011 - 03:04 PM
A silly trick, however isset($_POST['foo'][0]) can check for both existence, an non-emptiness in one call.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#6
Posted 09 September 2011 - 05:23 PM
Alexander said:
A silly trick, however isset($_POST['foo'][0]) can check for both existence, an non-emptiness in one call.
I was certain that I needed to test if($_POST['foo']) and after if($_POST['foo'][0])
Is this a new thing or it always been like that?
I actually create my own method to test this with a huge while to test every level of the array
#7
Posted 09 September 2011 - 09:18 PM
Quote
I did not know about the fact that isset can test multiple level at the same time
In C, strings are simply arrays of individual characters - and PHP extends this trait in to itself.
$name = "Alexander"; echo $name[0] . $name[strlen($name) - 1];
Would print "Ar".
And
$array = array('abc', '123');
echo $array[0][0];
Would print 'a'.
This could be of course used in the example to ensure there is at least one character (0th index of string.)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#8
Posted 12 September 2011 - 11:36 AM
Thanks. Now i use isset and strlen(trim($_POST($field))) == 0 to check if the value was filled in in the field. it works fine
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









