$_GET value or eregi error?

144 Views Asked by At

I'm not sure where is my problem. I've a script called with parameters (GET) like :

http://www.xxx.com?isms_restemsg=STOP&value=1

Here is my code :

$keyword_allowed = array("STOP", "");

$found_keyword = "N";
$keyword_cf = "";
for($i=0; $i < 2; $i++)
{
    if (eregi($keyword_allowed[$i], $_GET["isms_restemsg"]))
    {
        $found_keyword = "Y";
        $keyword_cf = $keyword_allowed[$i];
    }
}

QUESTION : what happend when the url invoked is :

http://www.xxx.com?isms_restemsg=&value=1

In this case, what happend at the eregi instruction.

I'm asking this question because the $found_keyword="N", it should be "Y" Or is there an error ?

If yes can you help me ?

4

There are 4 best solutions below

1
On BEST ANSWER

You shouldn't use eregi(), since it's deprecated.

It doesn't even look as if you need regular expressions after all.

Give it a try with stristr() or even simple compare syntax:

if ($keyword_allowed[$i] == $_GET["isms_restemsg"])

(If you write this yourself you probably have control over the GET values as well.)


You can help yourself in such cases by echoing some debug output:

print_r($_GET);
for($i=0; $i < 2; $i++)
{
    echo eregi($keyword_allowed[$i], $_GET["isms_restemsg"]));
}
1
On

eregi is depreciated as of php 5.

Use stristr instead

 if (stristr($keyword_allowed[$i], $_GET["isms_restemsg"]))

Or better yet, array_search

$keyword_allowed = array("STOP", "");
$found_keyword = "N";

if(($keyword_c = array_search($_GET["isms_restemsg"], $keyword_allowed)) !== false) {
    $found_keyword = "Y";
}
0
On

When the URL is like this

http://www.xxx.com?isms_restemsg=&value=1

Your $_GET["isms_restemsg"] is blank

hence if (eregi($keyword_allowed[$i], $_GET["isms_restemsg"]))

second argument is passed as null in eregi function

eregi is deprecated stop using it replace with preg_match

0
On

I'm not quite sure yet, but there are probably other ways to achieve what you are trying to do.

First you could use a more specific regular expression to get rid of the loop. This checks for two alternatives, STOP and the empty string (this is probably what failed with your eregi test).

if (preg_match('/^(STOP | )$/ix', $_GET["isms_restemsg"], $r))
{
    $found_keyword = "Y";
    $keyword_cf = $r[0];
}
else {
    $found_keyword = "Y";
}

Or since you only need to check against two values:

if (in_array(strtoupper($_GET["isms_restemsg"]), array("STOP", "")) {
   $found_keyword = "Y";
}