Jump to content

Recommended Posts

I'm getting a <b>Warning</b>:  Illegal offset type in <b>/home/xtremer/public_html/manager/processes/login.php</b> on line <b>125</b><br /> error and not sure what's wrong with it.

 

$data = array('numberOfLogins' => $numberOfLogins,'dateLast' => $row['dateCurrent'],'dateCurrent' => date('Y-m-d H:i:s', time()),'ip_address' => $_SERVER['REMOTE_ADDR'],'browser' => $_SERVER['HTTP_USER_AGENT'],'os' => getUserOS());

 

 

Link to comment
https://forums.phpfreaks.com/topic/239037-illegal-offset-type/
Share on other sites

while ($row = mysqli_fetch_array($result)) {
                             
                                $userID = $row['userID'];
                                $firstName = $row['firstName'];
                                $lastName = $row['lastName'];
                                $loggedinUserDataArray = array('userID' => $userID, 'name' => $firstName.$lastName);
                                $_SESSION[$loggedinUserDataArray];
                                $data = array('numberOfLogins' => $numberOfLogins,'dateLast' => $row['dateCurrent'],'dateCurrent' => date('Y-m-d H:i:s', time()),'ip_address' => $_SERVER['REMOTE_ADDR'],'browser' => $_SERVER['HTTP_USER_AGENT'],'os' => getUserOS());	
                                $userID = mysqli_real_escape_string($dbc,$userID);
                                $query = "UPDATE manager_users WHRE userID = '".$userID."'";
                                $result = mysqli_query($dbc,$query);
                                $output = array('errorsExist' => false, 'message' => 'You have been logged in, please allow a moment while we load your account data.'.$numberOfLogins);
                                
                            } 

Link to comment
https://forums.phpfreaks.com/topic/239037-illegal-offset-type/#findComment-1228225
Share on other sites

hmm still don't see anything wrong with that code. Illegal offset errors generally mean you are using a non string/integer type for the array keys (IE doing something like:

$object = new GenericObject();
$array = array($object => 'value');

 

In that example, i tried using an object as an array key. This will cause the warning you are having.

 

However, I don't see anywhere in your code where you use anything but a string as a key. Please note, that what is occurring is a warning, and not an error. So your script isn't breaking.

 

I don't want to tell you to post all your code, but to be completely honest, I can't see anything wrong with what you posted, so I don't really know what to tell you.

Link to comment
https://forums.phpfreaks.com/topic/239037-illegal-offset-type/#findComment-1228228
Share on other sites

Here's the full code: I'm also getting this as the response from the sever as soon as the script executes.

 

<br />

<b>Warning</b>:  Illegal offset type in <b>/home/xtremer/public_html/manager/processes/login.php</b> on line <b>125</b><br />

<br />

<b>Warning</b>:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in <b>/home/xtremer/public_html/manager/processes/login.php</b> on line <b>119</b><br />

<br />

<b>Warning</b>:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in <b>/home/xtremer/public_html/manager/processes/login.php</b> on line <b>64</b><br />

{"errorsExist":false,"message":"You have been logged in, please allow a moment while we load your account data.1"}

 

<?php

session_start(); // Start a new session

// Include the database page
require ('../inc/dbconfig.php');
require ('../inc/global_functions.php');

if (isset($_POST['submit'])) {

    if(!isset($_SESSION[$loggedinUserDataArray])) {
    
        $errors = "no";
        
        if ((!isset($_POST['username']))||(empty($_POST['username']))||(trim($_POST['username'])=="")||(!isset($_POST['password']))||(empty($_POST['password']))||(trim($_POST['password'])=="")) {
        
            $errors = "yes";
            $message = "The username and password fields were both left blank!";
        
        } else {
        
            if ((!isset($_POST['username']))||(empty($_POST['username']))||(trim($_POST['username'])=="")) {
            
                $errors = "yes";
                $message = "The username was left blank!";
            
            } else {
            
                if ( preg_match("/^[^a-z]{1}|[^a-z0-9]+/i", $_POST['username']) ) {
                    
                    $errors = "yes";
                    $message = "The username contains invalid characters!";
                    
                } else {
                    
                    $username = strtolower($_POST['username']);
                    
                }
                    
                
            }
            
            if ((!isset($_POST['password']))||(empty($_POST['password']))||(trim($_POST['password'])=="")) {
                
                $errors = "yes";
                $message = "The password was left blank!";
                
            }
        
        }
        
        if($errors == "yes") {
    	   
           $output = array('errorsExist' => true, 'message' => $message);
           
    	} else {
    
            $username = mysqli_real_escape_string($dbc,$_POST['username']);
            $query = "SELECT * FROM manager_users WHERE username = '".$username."'";
            $result = mysqli_query($dbc,$query);
            
            if (mysqli_num_rows($result) > 0) {
            
                while ($row = mysqli_fetch_array($result)) {
                    
                    $err = "no";
                    
                    if ($row['statusID'] == 1) {
                        
                        $err = "yes";
                        $output = array('errorsExist' => true, 'message' => 'Sorry you must verify your email address before logging in. Didn\'t get the verification email? Don\'t worry we can <a href="javascript:void(0);" id="resendVerification">resend it</a>!');
                        
                        if ($row['lockDate'] !== "0000-00-00 00:00:00") {
                            
                            if (($row['statusID'] == 2) AND (strtotime($row['lockDate']) <= time())) {
                                
                                $err = "yes";
                                $output = array('errorsExist' => true, 'message' => 'Your account is currently locked, we appologize for the inconvienence. This is a security messure implimented by to many failed login\'s!');
                            
                            } else {
                            
                                $data = array('lockDate' => '0000-00-00 00:00:00', 'statusID' => 1);
                                $username = mysqli_real_escape_string($row['username']);
                                $query = "UPDATE manager_users SET '".$data."' WHERE username = '".$username."'";			
                            
                            }
                            
                        }
                        
                    }
                        
                    if ($row['statusID'] == 3) {
                        
                        $err = "yes";
                        $output = array('errorsExist' => true, 'message' => 'Your account has been suspended and is pending deletion. If you would like to contest this action <a href="javascript:void(0);" id="contestSuspension">click here</a>!');
                    
                    }
			    
                    if ($row['isactive'] == 4) {
                        
                        $err = "yes";
                        $output = array('errorsExist' => true, 'message' => 'Your account is currently pending deletion, would you like to reactivate it? <a href="javascript:void(0);" id="undeleteAccount">Yes, Reactivate</a>!');
                        
                    }
                    
                    if ($err == "no") {
                        
                        $password = $row['password'];
				    $password2 = $row['password2'];
                        $regenFromPostPW = reGenPassHash($_POST['password'], $password2);
                        
                        if ($password == $regenFromPostPW) {
                            
                            $numberOfLogins = $row['numberOfLogins']+1;
                            $userID = mysqli_real_escape_string($dbc,$row['userID'] );
                            $query = "SELECT * FROM manager_users WHERE userID = '".$userID."'";
                            $result = mysqli_query($dbc,$query);
                            
                            while ($row = mysqli_fetch_array($result)) {
                             
                                $userID = $row['userID'];
                                $firstName = $row['firstName'];
                                $lastName = $row['lastName'];
                                $loggedinUserDataArray = array('userID' => $userID, 'name' => $firstName.$lastName);
                                $_SESSION[$loggedinUserDataArray];
                                $data = array('numberOfLogins' => $numberOfLogins,'dateLast' => $row['dateCurrent'],'dateCurrent' => date('Y-m-d H:i:s', time()),'ip_address' => $_SERVER['REMOTE_ADDR'],'browser' => $_SERVER['HTTP_USER_AGENT'],'os' => getUserOS());	
                                $userID = mysqli_real_escape_string($dbc,$userID);
                                $query = "UPDATE manager_users WHRE userID = '".$userID."'";
                                $result = mysqli_query($dbc,$query);
                                $output = array('errorsExist' => false, 'message' => 'You have been logged in, please allow a moment while we load your account data.'.$numberOfLogins);
                                
                            } 
                            
                        } else {
                                
                                $output = array('errorsExist' => true, 'message' => 'Invalid Username and Password combination!');
                            
                        }
                    
                    }
                        
                
                }
            
            } else {
                
                $output = array('errorsExist' => true, 'message' => 'Sorry we can\'t seem to find you in our system, please check your username and try again!');
    		
            }
    
        }
    
    } else {

        $output = array('errorsExist' => true, 'message' => 'Already logged in!');
        
}
        
}

//Output the result
$output = json_encode($output);
echo $output;

?>

Link to comment
https://forums.phpfreaks.com/topic/239037-illegal-offset-type/#findComment-1228232
Share on other sites

Okay cool, however, the code was originally written by a friend of mine and I was trying to turn it into procedural style for my benefit and the same line on his was written as $this->session->set_userdata($loggedinUserDataArray);

 

and that's why I tried changing it into

 

$_SESSION[$loggedinUserDataArray]; 

 

What is the correct way of changing that oop line into what I need to use?

Link to comment
https://forums.phpfreaks.com/topic/239037-illegal-offset-type/#findComment-1228237
Share on other sites

Well I notice that you use a while loop in with those queries producing the errors. When you are pulling a single record from your database (IE querying based on a unique row, like ID, or username, etc.) you dont need to do use a while loop. Using one results in the second run of the while loop producing the warning you get. Ill give you an example

 

$result = mysql_query("SELECT something FROM somewhere WHERE id=some id");//query that will grab 1 row always
while ($row = mysql_fetch_array($result)){
//do some stuff
}

Now lets examine what happens with this while loop. Remember, that a while loop will always try to execute what is in its conditional statement, so what happens is

first iteration:
we set $row to the first result in the $result's result set. This also happens to be the only row.
because of this, $result is now boolean false,
second iteration:
remember that the result set only had 1 row
so $result is a boolean. EVen though its boolean, we still call the fetch_array function
using $result as a parameter. Obviously when you do mysql_fetch_array(false) it returns a value of false
this produces an error as fetch_array expects a mysql result resource, and you have passed a boolean
however, $row is set to false because mysql_fetch_array returned false, so the while loop exits without a 2nd iteration of the code block

 

fixing this is easy though. Just get rid of the while loop. So if we take our example above, to fix it we would do

$result = mysql_query("SELECT something FROM somewhere WHERE id=some id");//query that will grab 1 row always
$row = mysql_fetch_array($result);
//do stuff with row

//continue with rest of code

 

EDIT: ahh seems PF caught your original error. Can't believe I missed that. As for your latest question, without seeing your friends object, its kind of difficult to guess what he means to use. However, it seems that he is setting some sort of user data, so you can use 'user_data' as the key. In your friends example, $loggedinUserDataArray would be the value at that key. so

$_SESSION['user_data'] = $loggedinUserDataArray;

Link to comment
https://forums.phpfreaks.com/topic/239037-illegal-offset-type/#findComment-1228238
Share on other sites

His:

 

$loggedinUserDataArray = array(
												  'fmr' => $x0set,
												  'uid' => $mID,
												  'nme' => $firstName
												  );
					$this->session->set_userdata($loggedinUserDataArray);
					$data = array(
						'loginCount' => $countedLogins,
						'dateLast' => $row['dateCurrent'],
						'dateCurrent' => date('Y-m-d H:i:s', time()),
						'ip_address' => $this->gfunc->getRealIpAddr(),
						'browser' => $_SERVER['HTTP_USER_AGENT'],
						'os' => $this->gfunc->getUserOS()
					);		

Link to comment
https://forums.phpfreaks.com/topic/239037-illegal-offset-type/#findComment-1228239
Share on other sites

what you posted isn't the object itself, and doesn't really give much more context than the original snippet you posted. However, given the context, it seems what I posted is a pretty safe bet for what he is trying to accomplish translated to a procedural style.

Link to comment
https://forums.phpfreaks.com/topic/239037-illegal-offset-type/#findComment-1228240
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.