0

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]) 
4
  • do not use $_request better go for $_get or $_post Commented Aug 4, 2012 at 17:11
  • echo contents of $line and tell us what it gives Commented Aug 4, 2012 at 17:12
  • another wired thing is that if i place $lineArray[2] = "try"; $lineArray[4] = "try"; before the if and enter try and try within the text boxes it works so i just dont get why its doing this ? Commented Aug 4, 2012 at 17:23
  • @mithunsatheesh --------LineArray------Array ( [0] => kunal [1] => kunal [2] => kunal [3] => kunal [4] => 123456789 ) --------Line ------kunal,kunal,kunal,kunal,123456789 --------LineArray------Array ( [0] => kunalM [1] => KunalM [2] => kunalM [3] => kunalM [4] => kunalkunal ) --------Line ------kunalM,KunalM,kunalM,kunalM,kunalkunal --------LineArray------Array ( [0] => ) --------Line ------ Commented Aug 4, 2012 at 18:05

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Hey ive tried that like i mention up above i originally had it as so, but the it doesn't seem to be equal even when the data is the same, also i get the undefined error still.
This did not work either when i originally print my $line i get First Line give me : kay,kay,kay,kay,123456789 Second Line gives me : kay,kay,kay,kay,passw
Please update your question with the output of print_r($lineArray) and print_r($line)
--------LineArray------Array ( [0] => kunal [1] => kunal [2] => kunal [3] => kunal [4] => 123456789 ) --------Line ------kunal,kunal,kunal,kunal,123456789 --------LineArray------Array ( [0] => kunalM [1] => KunalM [2] => kunalM [3] => kunalM [4] => kunalkunal ) --------Line ------kunalM,KunalM,kunalM,kunalM,kunalkunal --------LineArray------Array ( [0] => ) --------Line ------
1

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.

3 Comments

hey the lines are like noted above to be --------LineArray------Array ( [0] => kunal [1] => kunal [2] => kunal [3] => kunal [4] => 123456789 ) --------Line ------kunal,kunal,kunal,kunal,123456789 --------LineArray------Array ( [0] => kunalM [1] => KunalM [2] => kunalM [3] => kunalM [4] => kunalkunal ) --------Line ------kunalM,KunalM,kunalM,kunalM,kunalkunal --------LineArray------Array ( [0] => ) --------Line ------
Right. So when you say the comparing doesn't work, have you tried echoing $_REQUEST['email'] to see why there is no match?
hey i found out it was cause i was entering a \n which added a space in the end you were right!!
1

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] ...

1 Comment

Hey i changed my code to this, it did stop giving me the error but it does not match my values when its comparing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.