Jump to content

string offset error


Texan78
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello, I have this script that I have been running a long time that is included in my site with a php include. It reads data from a cached file in an array. I noticed last night something has happened and it won't load in my site anymore and causes my site not to load. I determined this through various troubleshooting. Through the various troubleshooting I was able to get an error "Fatal error: Cannot use string offset as an array in /home4/mesquiu0/public_html/nws_alerts_scroller.php on line 44". This is strange because nothing has changed on my server or with this script. Also if I run it by itself it runs fine. http://www.mesquiteweather.net/nws_alerts_scroller.php but, when I include it in my site it won't load and I get the error. I am not sure what is causing this. I have done some research and it appears to be cause by a key as though it were an array of arrays. I just don't see it though. 

 

Here is the portion of the code it is referring to. 

// IF there are alerts
if(!empty($atomAlerts)) {                                                                     // IF there are alerts
  usort($atomAlerts, 'a_sort');                                                               // sort locations
  foreach($atomAlerts as $aak => $aav) {                                                      // FOR EACH alert, get the data from the cache file
    $caav = count($aav);                                                                      // count alerts
    for($i=0;$i<$caav;$i++) {                                                                 // FOR EACH alert
      // assemble alert data
			$title[$i][0] = '<b>'.strtoupper($aav[$i][0]).'</b>';
			@$adarray[$aav[$i][10].' '.$title[$i][0]] .=  ' -  <a href="'.$alertURL.'?a='
               .$aav[$i][14].'#WA'.$aav[$i][13].'" title="  Details for '.$aav[$i][12]
               .' '.$aav[$i][0].'">' .$aav[$i][12] ."</a>";
    }
  }
}

Here is a sample of the cached file the script reads from. 

$atomAlerts = array (
  'TXZ104' => 
  array (
    0 => 
    array (
      0 => 'Dense Fog Advisory',
      1 => 'Expected',
      2 => 'Minor',
      3 => 'Likely',
      4 => 1418136720,
      5 => 1418148000,
      6 => 'Anderson; Bell; Bosque; Collin; Comanche; Cooke; Coryell; Dallas; Delta; Denton; Eastland; Ellis; Erath; Falls; Fannin; Freestone; Grayson; Hamilton; Henderson; Hill; Hood; Hopkins; Hunt; Jack; Johnson; Kaufman; Lamar; Lampasas; Leon; Limestone; McLennan; Milam; Mills; Montague; Navarro; Palo Pinto; Parker; Rains; Robertson; Rockwall; Somervell; Stephens; Tarrant; Van Zandt; Wise; Young',
      7 => 'DENSE FOG IS OCCURRING OVER THE REGION WHICH WILL CREATE VERY
HAZARDOUS DRIVING CONDITIONS. SLOW DOWN...USE YOUR LOW BEAM
HEADLIGHTS...AND LEAVE PLENTY OF DISTANCE BETWEEN YOU AND THE
VEHICLE AHEAD OF YOU.',
      8 => '...DENSE FOG ADVISORY NOW IN EFFECT UNTIL NOON CST TODAY...
* LOCATION...ALL OF NORTH CENTRAL TEXAS.
* VISIBILITY...WIDESPREAD VISIBILITY REDUCTIONS OF LESS THAN 1/4
OF A MILE. VISIBILITIES WILL BEGIN TO IMPROVE AFTER NOON.
* IMPACTS...DENSE FOG WILL MAKE DRIVING HAZARDOUS.',
      9 => '#F60',
      10 => ' <img src="./images/FGY.gif" width="12" height="12" alt="Dense Fog Advisory" title=" Dense Fog Advisory" />',
      11 => '56',
      12 => 'Collin Co',
      13 => '1',
      14 => 'TXZ104',
      15 => '',
    ),
  ),

Any idea what might be causing this. I don't see anything right off the bat. Just strange it loads fine alone and the problem only happens and happened out of the blue when I include it in my site. I have tried to isolate it to see what might be conflicting with it but have been unsuccessful. I also would add it only errors and doesn't load when included in my site when there is an alert. Right now there is no alerts at the moment. There was alerts at the time this happened. 

 

-Thanks

Edited by Texan78
Link to comment
Share on other sites

at the time your code is running and isn't working, $title is likely a string. $title[$i] is iterating over the characters of that string, and $title[$i][0] = is trying to assign a value to a character element of the string as though it is an array.

 

what does using var_dump() show for the contents of $title and what is your code that defines $title?

 

i'm going to guess that this is due to re-usage of a variable that exists in your main code as a string. when you run this included code directly, that variable doesn't exist at all. if so, use a different variable or initialize it to an empty array before the first use in the posted code.

  • Like 1
Link to comment
Share on other sites

Thanks for the reply, I was only looking at that $title[$i] as well. It doesn't appear to be defined in this script. Which is strange since nothing as change. 

 

What are you suggestion is their is another variable named $title when I include it in my page that would be causing this? That is very possible as I have made some changes to the page which it is included. Strange thing is it kills every page when there is an alert, not just one. 

 

I will give that a look and see what I can dig up. 

 

-Thanks

Link to comment
Share on other sites

  • Solution

You say you are including that script in your site? Does your site code also use a variable called $title? I think the code for your site is conflicting with that portion of code you have posted.

 

If the script your are including is a standalone file then change all instances of $title used in that script to a different name, ie $atom_title 

Link to comment
Share on other sites

With all due respect: The entire code is a mess. You have PHPHTML spaghetti code from hell, cryptic variable names, arcane data structures, variables coming from nowhere, cross-site scripting vulnerabilities etc.

 

Yes, you could write a workaround for this one bug and hope that it will buy you some time until the next strange thing happens. But I'd rather take care of the underlying issue and clean up the mess.

  • How about a sane data structure which uses meaningful indexes instead of random numbers? What's the caching supposed to do, by the way?
  • How about proper code which passes values in a predictable way, follows best practices and can actually be read by humans?
  • How about untangling the PHP code and the HTML markup?

Ideally, this will not only fix the current bug but also prevent future bugs from happening. 

Link to comment
Share on other sites

With all due respect: The entire code is a mess. You have PHPHTML spaghetti code from hell, cryptic variable names, arcane data structures, variables coming from nowhere, cross-site scripting vulnerabilities etc.

 

Yes, you could write a workaround for this one bug and hope that it will buy you some time until the next strange thing happens. But I'd rather take care of the underlying issue and clean up the mess.

  • How about a sane data structure which uses meaningful indexes instead of random numbers? What's the caching supposed to do, by the way?
  • How about proper code which passes values in a predictable way, follows best practices and can actually be read by humans?
  • How about untangling the PHP code and the HTML markup?

Ideally, this will not only fix the current bug but also prevent future bugs from happening. 

 

 

With all due respect, I did not write this script. This was written by a friend of mine about 6-7 years ago who has since passed. It has worked great up until last night. I am very novice when it comes to PHP so what I know I have taught myself and I do not plan on being a career coder. I really do not like asking for help and mainly for the fact no matter what how you do it wether it is right or not. There will be someone else telling you it is wrong. This is a hobby site which I use to learn skills and help unwind from work and kids. I really do not need lectures or to be demeaning statements about how this or that is completely wrong. I have posted code in here before that I have learned from members just to have another other members trash it and say it is completely wrong. I come to this forum because there are some very knowledgable folks who are really helpful. I just believe the criticism can be toned down some especially for those who are not experts and are just looking for a little help. So with that said, your comment added nothing positive or constructive to my question as to what could have caused this. Thank you for your time and reply though. 

 

To the others, once I got home I did see there was a duplicate variable of the same name in the top of my site. Not sure why it is just now all of a sudden started causing problems. I like the idea of renaming it and I think that is what I will do. Can't do anymore testing at the moment as there is no alerts. So I will have to wait for the next ones which could be a few days to weeks. 

Link to comment
Share on other sites

Well I was lucky enough to get the same alerts issued tonight as last night so I could troubleshoot and test this further. Strange thing is it worked completely perfect without making any changes as it always had. For safe measures though I did change the variable $title to $atom_title as suggested and it is still working so I will roll with that. 

Link to comment
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.