Jump to content

empty worked well on wamp but fails to work properly in host

- - - - -

  • Please log in to reply
7 replies to this topic

#1
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
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?

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Can you post some code

#3
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
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
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
First you should always test if the variable exist like so:
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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.

#6
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts

Alexander said:

A silly trick, however isset($_POST['foo'][0]) can check for both existence, an non-emptiness in one call.
I did not know about the fact that isset can test multiple level at the same time
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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

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.

#8
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
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