Warning: strpos(): Empty needle in ......wordpress Plugin [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I get this error :
Warning: strpos(): Empty needle in ......popularity-contest.php on
line 2574
function akpc_is_searcher() {
global $akpc;
$referrer = parse_url($_SERVER['HTTP_REFERER']);
$searchers = explode(' ', preg_replace("\n|\r|\r\n|\n\r", ' ', $akpc->searcher_names));
foreach ($searchers as $searcher) {
if (strpos($referrer['host'], $searcher) !== false) {
return true;
}
}
return false;
}
Can someone please help me to fix this problem?

A bunch of PHP search functions use the terms "needle" and "haystack" as their parameter names, indicating what is sought and where to seek it.
The strpos function is such a function. "Empty needle" means that you have passed in a null or empty value as the needle to look for. This is like saying "search for nothing" which doesn't make sense to the function.
To fix this, check that the variable that you're passing in as the needle has an actual value. The empty function is a good choice for that.

The warning should go away if you set WP_DEBUG to false in wp_config.php. If you want to fix it, try the following:
function akpc_is_searcher() {
global $akpc;
$referrer = parse_url($_SERVER['HTTP_REFERER']);
$searchers = explode(' ', preg_replace("\n|\r|\r\n|\n\r", ' ', $akpc->searcher_names));
foreach ($searchers as $searcher) {
if ( ! empty($searcher) && strpos($referrer['host'], $searcher) !== false) {
return true;
}
}
return false;
}

Related

Using '&&' inside foreach? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am looking for smart solution, maybe you could help me out. Currently I am doing an own Authentication (Separate Class) system for my Webshop project. My problem is, that I need conditional statement inside foreach loop, to return the code (see below). Any suggestions?
My code currently look like this
public function regiAuth($email, $password, $firstname, $lastname)
{
$authContainer = [$email, $password, $firstname, $lastname];
foreach ($authContainer as $a) {
return !empty($_POST[$a]);
}
}
And I want to result this (With &&)
return !empty($_POST[$email]) && !empty($_POST[$password]) &&
!empty($_POST[$firstname]) && !empty($_POST[$lastname])
I believe you could do simply by do
foreach ($authContainer as $a) {
if (empty($_POST[$a])
return false;
}
return true;
instead of checking if all of them are full, you look if there is at least one empty.
it is a good practice to stop iteration if you find one element that is not as expected, imagine if you had an array of hundreads of assertions to do.
making an full if statement would look like this, here it checks if the $_POST are not empty. && means that both have to true or false
foreach ($authContainer as $a) {
if((!$_POST[$email]) && (!$_POST[$password])){
return false;
}
}
return true;

Compare values in a single array and return the key in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an array like this:
$age=array("Peter"=>43,"Ben"=>67); .
The array contains only two key value pairs. First i need to check if the values of these two keys are same. If same then it returns the key of these two value, otherwise return false. So here value 43 and 67 are not same so it should return false. If the two values would be same like this :
$age=array("Peter"=>43,"Ben"=>43); .
It should return the key "Peter" and key "Ben" and maybe store the keys in another array The reason is to find if two people are of same age if same age then i would like to do several other things. I will appreciate the help.
Just get the unique values and see if there is only 1:
if(count(array_unique($age)) === 1) {
return array_keys($age);
} else {
return false;
}
Because I was bored, here are two others.
Assuming only 2 elements:
if(($v = array_values($age)) && $v[0] === $v[1]) {
return array_keys($age);
} else {
return false;
}
Also, should work with multiples:
if((array_sum($age) % count($age)) === 0) {
return array_keys($age);
} else {
return false;
}

my defined php function not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want my page to display an error message "lorem ipsum" if the input for $a (from the $_POST super global) is greater than a defined maximum value ($max1 and $max2). So this is what I did:
$max1 = 2.7432;
$max2 = 274.32;
$a = $_REQUEST['a'];
function maximum(){
if ($a>$max1 || $a>$max2){
echo "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
return maximum();
}
}
then I call the function here like so:
if(($fred=="fred") && ($george=="george") {
//check validity of input first by calling maximum;
maximum();
}
This is not working! What am I doing wrong?
This is a scope issue. $a, $max1, and $max2 is not in scope inside of your function. You need to pass it as a parameter for it to be available (i.e. in scope) within your function.
Also, your function doesn't work. In a nutshell you need to return the string from your function and then capture or echo it out when calling the function.
$max1 = 2.7432;
$max2 = 274.32;
$a = $_REQUEST['a'];
function maximum($a, $max1, $max2){
if ($a>$max1 || $a>$max2){
return "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
}
}
if(($fred=="fred") && ($george=="george")) {
//check validity of input first by calling maximum;
echo maximum($a, $max1, $max2);
}
Please change your function like bellow and check the comments what mistake you did
function maximum()
{
global $max1,$max2,$a;//make $a,$max1,$max2 available inside this function
if ($a > $max1 || $a > $max2) {
echo "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
//return maximum(); remove this line. Otherwise itwill go infinte loop
}
}
http://php.net/manual/en/language.variables.scope.php

Error on line that doesnt exist [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to define a link based of a $_GET variable, but it's saying there's an error on a line that doesn't exist...
<?php
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
?>
<?php
if ($ref != "") {
$link = "http://site.com/page.php?ref=$ref";
} else {
$link = "http://site.com/page.php";
}
?>
Anyone see what's up? I was pretty sure it was fine.
I've tried it multiple different ways, with isset etc... same result.
You are missing a closing }:
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
}
By the way, this code is quite redundant. empty() will also check whether the variable is set, so you don't need isset().
You can also use the ternary operator, which is for cases like this:
$ref = empty($_GET['ref']) ? null : $_GET['ref'];
And later check with:
if (!is_null($ref)) {
//whatever
}
Otherwise, in your code, when execution reaches if ($ref != "") {, the variable $ref might not even exist - this will throw an E_NOTICE, which you might not even see, depending on your settings.

Find a value in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I am doing wrong with the following code? I want to compare if the element $my_id is present within the array $arr. If it is present return TRUE else return FALSE.
for($i=0;$i<$cnt;$i++)
{
if($arr[$i] == $my_id)
{
return TRUE;
}
else
{
return FALSE;
}
}
You could replace that with...
return in_array($my_id, $arr);
...assuming you don't really want to return FALSE if the first element does not match.
If that is actually what you wanted, you could use...
return $arr[0] == $my_id;
If you want to leave your code mostly intact, just move the return FALSE to outside of the loop body.
The issue you are having is that you aren't looping entirely through the array. You are returning true/false after the first item in the array, irrespective of subsequent array entries after [0]
Well, I believe you should remove the else statement, unless you always just have one element in the array. I mean -- from the example you're showing, you're exiting the loop with this. I doubt this is what you want.
If you just need to know, if a value is within a given array
in_array($value, $array);
Maybe you want get the index of (the first occurence of) the value too
$index = array_search($value, $array);
if ($index === false) {
// Not in array
} else {
echo $array[$index];
}
The error in Your code is to return false on $arr[$i] != $my_id. The algorithm should look something like this:
for($i=0;$i<$cnt;$i++)
{
if($arr[$i] == $my_id)
{
return TRUE;
}
}
return FALSE;
P.S. This isn't the best solution for this problem in PHP language. You should use one from alex.

Categories