I am writing an if else statement in OOP;
The conditions are:
1. Get all requests from BSC from 789789 or 987897
2. If RNB is starting 17 and are 15 characters long
3. Those values with cost as 15 send to URL 1
4. Else send to URL 2
If it matches the statement it is sent to a specific URL, if it does not it sent to another URL. I have rewritten the conditional statement below:
if($request->BSC = 789789 or 987897 && $request->RNB = 17 && $request->BRN strlen = 15 && $request->Cost = 11){
send to URL 1
} else{
..Send to URL 2
}
My question is conditional statement correct?
Your question is a little unclear,
I try to fix your if statement just to show you the correct syntax!:
if( ($request->BSC == 789789 || $request->BSC == 987897) && strpos($request->RNB,'7') == 0 && $request->BRN == 15&& $request->Cost == 11){
//send to URL 1
} else{
//send to URL 2
}
This line :
strpos($request->RNB,'7') == 0
Means: RNB should start with 7, for example : 765433 or 789998 or 712342 . ..
Related
I would like to set the disabled state of a form field based on the combination of 4 variables: processed, process started, process ended, user id
If it is not going to be processed, the form field should be disabled
If the process has started OR ended, it should be also disabled, except if the user id == 1. So User 1 can still fill the form field, even if the process has started OR ended. And it should be also disabled for User 1 also if it is not going to be processed.
I was trying this way, but doesn't work as I expect, so there must be a flaw in my logic or understanding how PHP works:
'disabled' => !$proc || (($proc_started || $proc_ended) && !$user_id == 1)
This way other users see the form field also enabled, which I don't want. Is it the hierarchy of the Logical Operators ?
Can you please point me to the right direction? Thanks.
!$user_id == 1 is (!$user_id) == 1
$foo = 42;
!$foo == false;
You want to write !($user_id == 1) or $user_id != 1
Should work.
if($user_id === 1) {
if($state != "processed") {
$state = "Enabled" // or anything else of your choice
}
} else {
$state = "Disabled";
}
I'm doing a search engine for eBay-API, though I can't figure out what I'm doing wrong here, I get the error of:
Notice: Trying to get property of non-object in D:\xamp\htdocs\webapp\queries.php on line 431
Line 431 is:
if ($darkarteli->Item->SellingStatus->CurrentPrice <= 10 && $darkarteli->Item->SellingStatus->CurrentPrice >= 25 && $darkarteli->ListingType == 'FixedPriceItem')
Full code snippet:
foreach ($temparrrayforproducts as $darkartelis){
global $itemidsfromgetsellerlist;
$nd=[];
array_push($nd, $darkartelis->ItemArray);
foreach($nd as $darkartelis->Item){
if ($darkarteli->Item->SellingStatus->CurrentPrice <= 10 && $darkarteli->Item->SellingStatus->CurrentPrice >= 25 && $darkarteli->ListingType == 'FixedPriceItem'){
array_push($itemidsfromgetsellerlist, $darkarteli->Item->ItemID);
}
}
}
var_dump ($itemidsfromgetsellerlist);
Beautified var_dump() for $darkartelis: https://pastebin.com/xi34Vzn7
EDIT: I tried this code and it doesn't give out any errors, but it doesn't pass through the if statement even though there are 10-25 priced items which are fixedpriceitems also
foreach($temparrrayforproducts as $ndd){//pro kiekviena getsellerlist
global $itemidsfromgetsellerlist;
$nd=[];
array_push($nd, $ndd->ItemArray->Item); // tada per get seller list item arr
foreach($nd as $k=>$nds){ // per kiekviena itema
if (is_object($nds)&&!empty($nds)){
if ($nds->SellingStatus->CurrentPrice <= 10.00 && $nds->SellingStatus->CurrentPrice >= 25.00 && $nds->ListingType == 'FixedPriceItem'){
echo "Hallelujah";
array_push($itemidsfromgetsellerlist, $nds->ItemID);
}
}
}
}
I have 4 fields from a previous page using an html form in the post method. Everything works fine except I am trying to check to make sure the strlens of each field are below 200. The following code always says that they are too long even though I am making sure they are way below 200.
PHP:
$hsname = trim($_POST['hsname']);
$hsstate = trim($_POST['hsstate']);
$hsemail = trim($_POST['hsemail']);
$hspassword = trim($_POST['hspassword']);
if (strlen($hsname) || strlen($hsstate) || strlen($hsemail) || strlen($hspassword) > 200){
die("Each field can only contain 200 characters"); //this is always returned even though the fields are below 200.
}
else {
echo "fields have good characters";
}
I will translate Your if condition:
if (strlen($hsname) || strlen($hsstate) || strlen($hsemail) || strlen($hspassword) > 200)
It`s like:
if(strlen($hasname)!='' || strlen($hsstate)!='' || strlen($hsemail)!='' || strlen($hspassword) > 200)
So You should know why it`s wrong now ;)
Compare every strlen or try that:
$maximumValue = max(strlen($hsname), strlen($hsstate), strlen($hsemail), strlen($hspassword));
// You got maximum value now, so:
if($maximumValue > 200){}
The reason your condition always returns true is because you're only checking if the last string has a length of greater than 200. The other strings are checked for whether they have a length of greater than 0, so your condition reads "if any of the first 4 strings are greater than 0 or the last string is greater than 200". Try this instead
foreach(array($hsname,$hsstate,$hsemail,$hspassword) as $string) {
if(strlen($string) > 200) {
die("Each field can only contain 200 characters");
}
}
You need to check that the fields are indeed there, and then check their length.
One way of doing it:
$fields = array('hsname', 'hsstate', 'hsemail', 'hspassword');
foreach ($fields as $field) {
if (!array_key_exists($field, $_POST)) {
die("Field {$field} is missing");
}
// Declare a variable whose name is in $field.
${$field} = trim($_POST[$field]);
if (strlen(${$field}) > 200) {
die("Field {$field} must be less or equal to 200 characters");
}
}
If you have international character support, you may also want to verify effective string length against database field length; a single UTF8 "character" may be more than one database field "character". The mb_* functions may help you, as well as utf8 encoding and decoding functions.
That wont work, when you evaluate
strlen($hsname)
It will satisfy the if condition when it has more than 0 characters
This is the same for the first 3 strings you check against
You need the ' > 200 ' for each of the strings you want to compare [assuming that you are passing the actual values in the post]:
$hsname = 'test';
$hsstate = 'test';
$hsemail = 'test';
$hspassword = 'test';
if (strlen($hsname) > 200 || strlen($hsstate) > 200 || strlen($hsemail) > 200 || strlen($hspassword) > 200){
die("Each field can only contain 200 characters"); //this is always returned even though the fields are below 200.
} else {
echo "fields have good characters";
}
I'm using wp-polls and need to limit the number of votes per IP. Although this is usually set at 1 by default, I need to limit the number of votes per IP to 2.
I've been reading documentation, playing with the plugin code, and looking around on google and SO and can't seem to find the proper method.
I've considered using cookies, but these are harder to catch since I could just as well empty my cookies and voilĂ , it's done.
I need a limit because I don't want people to vote endlessly
Looking at the database, I imagine this has something to do with the pollip_ip field (from the (prefix)_pollsip) table, but not being familiar with editing WP Plugins, this is as far I as I got to go.
As a reference, here is some failed code
wp-polls.php, line 1323
// original code
// if($check_voted == 0) {
// proposed by #birgire
if( $check_voted == 0 || ( is_array( $check_voted ) && 2 >= count( $check_voted ) ) ) {
wp-polls.php, line 1323
// original code
// if($check_voted == 0) {
// alternative of code proposed by #birgire
if( ($check_voted == 0) || count($check_voted) <= 2)
I couldn't see any available filters for this, when I skimmed through the plugin source.
I wonder if it would work, if you replace line #1323 of the wp-polls.php file:
if($check_voted == 0) {
with:
if( $check_voted == 0
|| ( is_array( $check_voted ) && 2 >= count( $check_voted ) ) ) {
to limit the number of votes per IP to 2.
Additionally:
Replace line #140 with:
if( !is_array($check_voted) && intval($check_voted) > 0
|| (is_array($check_voted) && sizeof($check_voted) > 1)
|| ($poll_active == 0 && $poll_close == 1)) {
But I don't recommend in general to modify plugin files, since it will be restored in the next plugin update.
Sidenote: The plugin is not using the recommended $wpdb->prepare() when preparing the SQL for $wpdb->query().
I am trying to write an if statement that basically checks if the users referrer is in a list of allowed referrers and if not fails.
I have two variables controlling this $this->allowAllReferer and $this->allowEmptyReferer, which as per their name decide whether every referrer should be allowed access and whether empty referrers are allowed, respectively. As well as $this->allowedReferers which is an array of allowed referrers.
I have this function below which I am pretty sure isn't working properly but I have been staring at and tweaking it for half an hour and i've got to the point where I can't tell if it's working or not.
//If the referee is empty and allow empty referrer is false
//or
//If it is not in the allowed list and allow all referer is false
if(!(empty($_SERVER['HTTP_REFERER']) && $this->allowEmptyReferer)
&&
!(!$this->allowAllReferer && in_array(
strtolower(empty($_SERVER['HTTP_REFERER']) ? null : $_SERVER['HTTP_REFERER']), //Silly php access null variable
$this->allowedReferers)
)) {
throw new sfException("Internal server error. Please contact system administrator. File download disabled.");
}
Do you know the correct or a better way to do this/can you confirm the above works?
Cases, hope this makes it more clear
empty_referrer | allowEmpty | in_array | allReferer | result
----------------------------------------------------------------
true | true | false | false | false - no error - empty allowed
false | true | false | false | true - error - not in array
false | true | false | true | false - no error - not in array but allowed
false | false | false | false | true - error - empty and now allowed
If you would like to keep the logic within one huge if block, then try the following:
if (
// throw an error if it's empty and it's not allowed to be
(empty($_SERVER['HTTP_REFERER']) && !$this->allowEmptyReferer)
|| (
// don't bother throwing an error if all are allowed or empty is allowed
(!empty($_SERVER['HTTP_REFERER']) && !$this->allowAllReferer)
// throw an error if it's not in the array
&& !in_array((empty($_SERVER['HTTP_REFERER']) ? null : strtolower($_SERVER['HTTP_REFERER'])), $this->allowedReferers)
)
)
{
throw new sfException("Internal server error. Please contact system administrator. File download disabled.");
}
The second check for empty will now skip the in_array if it's empty.
How about this:
$ref = &$_SERVER['HTTP_REFERER'];
if($allowAll) {
// allowed
} else if($allowEmpty && empty($ref)) {
// allowed
} else if(!empty($ref) && in_array($ref, $allowedReferers)) {
// allowed
} else {
// fail
}
If you want to have all checks in a single if, you can simply chain together the conditions using or/||. Short-circuit evaluation ensures proper variable values and immediate termination of the condition check:
$ref = &$_SERVER['HTTP_REFERER'];
if($allowAll
|| ($allowEmpty && empty($ref))
|| (!empty($ref) && in_array($ref, $allowedReferers))) {
// allowed
} else {
// fail
}
If I've understood your requirements correctly, then this is most in keeping with your original code
if( (!$this->allowEmptyReferer && empty($_SERVER['HTTP_REFERER'])
|| (!$this->allowAllReferer && !in_array(
strtolower(empty($_SERVER['HTTP_REFERER']) ? null : $_SERVER['HTTP_REFERER']),
$this->allowedReferers)
) { // throw your exception }
I would simplify your logic, something like this:
if (!$this->allowAllReferer)
{
if (empty($_SERVER['HTTP_REFERER']) && !$this->allowEmptyReferer)
{
// emtpy referer - not allowed. handle as you wish (throw exception?)
}
else if (!empty($_SERVER['HTTP_REFERER']) &&
!in_array(strtolower($_SERVER['HTTP_REFERER'])), $this->allowedReferers)
{
// referer supplied is not approved/allowed. - handle appropriately.
}
else
{
// referer should be ok if we get here.
}
}
ie. First of all if you are allowing all referers, then you don't need to do any processing - just skip this (if (!this->allowAllReferer)).
Second, break up your logic checks into management chunks it makes it easier to write, read and maintain.
if(isset($_SERVER['HTTP_REFERER'])) {
echo $_SERVER['HTTP_REFERER'];
}