KSM
KSM

Reputation: 262

Undefined offset And Array Comparison PHP

Hey everyone ive done my research and i find im still stuck many people said to put @ sign before the variable but it does not seem to be working, so my code gives me this error

                 Notice: Undefined index: 2 in login.php on line 20

my code is

          if( isset($_REQUEST['email']) || isset($_REQUEST['pwd']) || $_REQUEST['email'] != "" || $_REQUEST['pwd'] != "" )
                    {

                          $inputFile = fopen("members.txt", "r");  
                          $found = false;
                          $i =0;
                          //read the read.txt until the end of file
                          while(!feof($inputFile) && $found == false)  
                          {

                            $line = fgets($inputFile);  
                            // replace the special charater within the lines by there proper entity code
                            $lineArray = preg_split("/\,/", (string)$line);


                            if ($_REQUEST['email'] === $lineArray['2'] && $_REQUEST['pwd'] === $lineArray['4']) 
                            {
                                        session_start();
                                        $found = true;
                                        $useremail=$_REQUEST['email'];
                                        $password= $_REQUEST['pwd'];
                                        //time to set sessions and stuff
                                        $_SESSION['useremail'] = $useremail;
                                        $_SESSION['password'] = $password;
                                        //send the redirect header
                                        header('Location: index.php');
                                        exit();
                            }
                          }
                          fclose($inputFile);

                    }

so the line its referring to is

                            if ($_REQUEST['email'] === $lineArray['2'] && $_REQUEST['pwd'] === $lineArray['4']) 

ive tried many other variation such as removing single quotes adding @ in front of the $lineArray and doing both, can anyone help me out the values are there when i was printing them out but when it get to this if statement it doesn't turn to equal and it give me this error.

if also tried

           if ($_REQUEST['email'] === $lineArray[2] && $_REQUEST['pwd'] === $lineArray[4]) 

and

        if ($_REQUEST['email'] === @$lineArray[2] && $_REQUEST['pwd'] === @$lineArray[4]) 

Upvotes: 0

Views: 388

Answers (3)

user849137
user849137

Reputation:

The error means there is a undefined var being used in your code. In your case, it's talking about $lineArray['2']. This isn't a serious error, so you can be lazy and alter your error settings to get rid of it:

error_reporting(E_ERROR | E_WARNING | E_PARSE);

But you really should just fix it instead.

As Devnate suggested, you need to use a int to specify the index key of the array, instead of a string (so this $lineArray[2] instead of this $lineArray['2']). Why? Because the key you was using before ('2') was never set, which resulted in the error.

You say the comparing fails when you attempt the above. I cannot help you with that until I see the result of print_r($lineArray);.

This is the code from your previous question. It's a shame you didn't take my advice and go with my code. You wouldn't be having this issue if you did. But that's a different matter. Post the print_r($lineArray); so I can see what the problem is with the comparing.

Upvotes: 1

UltraInstinct
UltraInstinct

Reputation: 44444

You need $lineArray[2]. Indices of an array are integers, not strings. And also ensure if that same array has atleast 3 elements.


The line is the problem:

$lineArray = preg_split("/\,/", (string)$line);

It should be (since you seem to be splitting on ,):

$lineArray = preg_split("/,/", (string)$line);

PS: Consider using a simpler $array = explode(",",$yourString)

Upvotes: 2

devnate
devnate

Reputation: 764

You'll want to use numbers (no quotes) for your array keys, but you'll also need to check that those array values exist with isset() before comparing them.

if (isset($lineArray[2]) && $_REQUEST['email'] === $lineArray[2] ...

Upvotes: 1

Related Questions