PHP Debugging
- Undead_Mercenary
- Posts: 2914
- Joined: Wed Aug 21, 2002 10:01 am
- Location: Barrie, Ontario
PHP Debugging
Alright, I am having a real mystery bug with my PHP code at work. I have this script (most of it found on the Internet) that grabs emails from an email account that receives binary data from aircraft using the weather-sensing probe the company I work for manufactures. If there's an email, it grabs the attachment (all emails will have 1 attachment called .sbd files), converts the binary data to readable numbers and inserts the values into a database. Each record in the file is 30 bytes long.
It was worked 100% for a few months. 0 problems. Now, I am trying to get the beta version of my project ready for a real-time test. However, one of my employers has added an extra byte to each record for this laser range variable. He isn't using it yet, but all the new sbd files will have this extra byte. Easy enough, I just get the script to read an extra byte. At first, it doesn't work. It's way off, every single value is nothing close to where it should be.
Alright, so instead of busting my balls, I upload a working backup of the script. Try again, and I get the exact same values as before! Yet I am using a version of the script that has always worked before. Believe me, I triple checked all of the code, and nothing has changed.
Here's a snippet of code:
$dataBeingRead = fread($fileHandle, 1)
or die("Error reading attachment!");
$convertedData = unpack('c', $dataBeingRead)
or die ("Error converting data!");
$day[$lineNumber] = $convertedData[1];
($fileHandle is the attachment being read in "rb" mode, $lineNumber increasing by 1 after each record)
So it reads a single byte, converts it to a signed character variable, then adds it to an array. Like I said, never had any problems before. But this is basically what is giving me problems.
I am at the point where I think it's something on the server (Bell) that isn't functioning properly. Is it possible there's some sort of error in memory occuring on the server? I have a hard time believing that it's something I did. And it has happened on and off since yesterday. All morning it was fine, then early afternoon (after I tried to read the extra byte), it was having problems. After a couple hours, it worked fine (with the old version). Now today, it's doing it again.
Any ideas?
It was worked 100% for a few months. 0 problems. Now, I am trying to get the beta version of my project ready for a real-time test. However, one of my employers has added an extra byte to each record for this laser range variable. He isn't using it yet, but all the new sbd files will have this extra byte. Easy enough, I just get the script to read an extra byte. At first, it doesn't work. It's way off, every single value is nothing close to where it should be.
Alright, so instead of busting my balls, I upload a working backup of the script. Try again, and I get the exact same values as before! Yet I am using a version of the script that has always worked before. Believe me, I triple checked all of the code, and nothing has changed.
Here's a snippet of code:
$dataBeingRead = fread($fileHandle, 1)
or die("Error reading attachment!");
$convertedData = unpack('c', $dataBeingRead)
or die ("Error converting data!");
$day[$lineNumber] = $convertedData[1];
($fileHandle is the attachment being read in "rb" mode, $lineNumber increasing by 1 after each record)
So it reads a single byte, converts it to a signed character variable, then adds it to an array. Like I said, never had any problems before. But this is basically what is giving me problems.
I am at the point where I think it's something on the server (Bell) that isn't functioning properly. Is it possible there's some sort of error in memory occuring on the server? I have a hard time believing that it's something I did. And it has happened on and off since yesterday. All morning it was fine, then early afternoon (after I tried to read the extra byte), it was having problems. After a couple hours, it worked fine (with the old version). Now today, it's doing it again.
Any ideas?

- Archangelus
- Posts: 4286
- Joined: Mon Jun 24, 2002 9:01 pm
- Contact:
RE: PHP Debugging
Silly question, just at a quick glance.
On the last line, why wouldn't you have it like this instead:
$day[$lineNumber] = $convertedData[$lineNumber];
Otherwise, aren't you setting every value in your $day array to the same value ($convertedData[1])?
On the last line, why wouldn't you have it like this instead:
$day[$lineNumber] = $convertedData[$lineNumber];
Otherwise, aren't you setting every value in your $day array to the same value ($convertedData[1])?
- Undead_Mercenary
- Posts: 2914
- Joined: Wed Aug 21, 2002 10:01 am
- Location: Barrie, Ontario
RE: PHP Debugging
But that value is always changing. It loops through the entire file, so convertedData[1] changes about 36 times for each field. I had to do this because the unpack method returns an array.

- Undead_Mercenary
- Posts: 2914
- Joined: Wed Aug 21, 2002 10:01 am
- Location: Barrie, Ontario
RE: PHP Debugging
Weird... I just tried it from home and it worked fine. WTF? Well I'm going to try reading that extra byte again and see what happens.

RE: PHP Debugging
Gremlins, man. They've got tech support now.
Re: PHP Debugging
Undead_Mercenary wrote:$dataBeingRead = fread($fileHandle, 1)
or die("Error reading attachment!");
$convertedData = unpack('c', $dataBeingRead)
or die ("Error converting data!");
$day[$lineNumber] = $convertedData[1];
I did not know you spoke Chinese Merc?

-
midnightservice
- Posts: 1483
- Joined: Wed May 21, 2003 10:16 pm
- Location: Missouri
- Contact:
RE: Re: PHP Debugging
merc it sounds like to me you are having a cache problem on the machine you are testing on. What browser are you using.
<src="http://myweb.cableone.net/tanda5/midnight.jpg"><br /><a href="http://profile.xfire.com/midnightservice"><br /><img src="http://miniprofile.xfire.com/midnightservice.png" alt="midnightservice Xfire Miniprofile" border=0><br /></a>
- Undead_Mercenary
- Posts: 2914
- Joined: Wed Aug 21, 2002 10:01 am
- Location: Barrie, Ontario
RE: Re: PHP Debugging
I always use Firefox, but I also tested it in IE. After getting that error, I cleared my temporary internet files and even powered down my pc and did a restart. But yeah, it has to be something outside my code.

- Undead_Mercenary
- Posts: 2914
- Joined: Wed Aug 21, 2002 10:01 am
- Location: Barrie, Ontario
RE: Re: PHP Debugging
Well the more I debug this issue, the more messed up it gets. We finally narrowed down the problem to the email section of the script. Somehow, attachment binary data was being altered. We even tried with a simple ASCII text file that wrote "Hello World". When the script grabbed the email containing the text file and spit out it's contents (read in binary mode and then converted), the decimal values for each letter weren't the same as those found on the ASCII table. So at some point before I even touch the attachment, something is messing up my files.
I talked with their web hosting, Bell, to find out if any changes were made to the server since last week (when I started having the problem). I figure maybe they updated to a new version of PHP or something. Of course, they told me nothing has changed and said it had to be my code. Yet here I am at home, and it just worked perfectly fine. That just seems to indicate that something is fucked with their server. And I know it isn't my PC at work, because I had my employer try it, and even my room mate at home and both of them had it mess up.
Kinda frustrating, especially since we're trying to get this beta out for some real testing and this issue is holding me back.
I talked with their web hosting, Bell, to find out if any changes were made to the server since last week (when I started having the problem). I figure maybe they updated to a new version of PHP or something. Of course, they told me nothing has changed and said it had to be my code. Yet here I am at home, and it just worked perfectly fine. That just seems to indicate that something is fucked with their server. And I know it isn't my PC at work, because I had my employer try it, and even my room mate at home and both of them had it mess up.
Kinda frustrating, especially since we're trying to get this beta out for some real testing and this issue is holding me back.

- Archangelus
- Posts: 4286
- Joined: Mon Jun 24, 2002 9:01 pm
- Contact:
RE: Re: PHP Debugging
Do you know if they expose PHP on their server (i.e. - can you run a phpinfo page)?
If so, I'd get the PHP Info page from your system and the server with issues and start doing a compare there. It may help you understand if you have a configuration issue on the server. I'd definitely be concerned if you are running on two different major versions (PHP4 vs PHP5) considering there are some major differences between the two.
Good luck!
If so, I'd get the PHP Info page from your system and the server with issues and start doing a compare there. It may help you understand if you have a configuration issue on the server. I'd definitely be concerned if you are running on two different major versions (PHP4 vs PHP5) considering there are some major differences between the two.
Good luck!
- Undead_Mercenary
- Posts: 2914
- Joined: Wed Aug 21, 2002 10:01 am
- Location: Barrie, Ontario
- Archangelus
- Posts: 4286
- Joined: Mon Jun 24, 2002 9:01 pm
- Contact:
RE: Re: PHP Debugging
I meant your development system that it works on.
-
midnightservice
- Posts: 1483
- Joined: Wed May 21, 2003 10:16 pm
- Location: Missouri
- Contact:
RE: Re: PHP Debugging
merc. I have ran into similar problems when it comes to reading info from emails as attachments. What virus scanner is the email passign thru before you get to read the contents. Virus Scanners will open up attchments and read the contents and then put them back. Some times the virus scanner will change the binary of the file but leave the contents as normal read. I fill this may be your problem.
<src="http://myweb.cableone.net/tanda5/midnight.jpg"><br /><a href="http://profile.xfire.com/midnightservice"><br /><img src="http://miniprofile.xfire.com/midnightservice.png" alt="midnightservice Xfire Miniprofile" border=0><br /></a>
- Undead_Mercenary
- Posts: 2914
- Joined: Wed Aug 21, 2002 10:01 am
- Location: Barrie, Ontario
RE: Re: PHP Debugging
Maybe... my machine uses this HP Center all-in-one kind of thing. But I don't think the virus scanner can do anything with the attachments, because everything is happening on the server. Plus it wouldn't explain why the same thing has happened on other machines (bosses, room mates), but never mine.
It's worth a shot though. I'll have to try it tomorrow when I go in to work.
It's worth a shot though. I'll have to try it tomorrow when I go in to work.

RE: Re: PHP Debugging
I'm certainly not an expert on this stuff, so I'm just tossing out some ideas here.
You say the attachment data is changing, so why not throw in some sort of CRC or encapsulate the data so that it guarantee's data integrity? I have no clue how PHP handles this.
Have you messed with any MIME settings in the email server that gets these attachments?
These binary .sbd files - do they get converted instantly upon email delivery? or could you verify data integrity at the attachment level (before conversion) that way you could verify if the file had changed en route, or a virus scanner messing with them like Mid said? Basically, look for changes at every step of the way, before conversion. Once you know that the binary .sbd file has not changed, you're getting really close I would guess.
You say the attachment data is changing, so why not throw in some sort of CRC or encapsulate the data so that it guarantee's data integrity? I have no clue how PHP handles this.
Have you messed with any MIME settings in the email server that gets these attachments?
These binary .sbd files - do they get converted instantly upon email delivery? or could you verify data integrity at the attachment level (before conversion) that way you could verify if the file had changed en route, or a virus scanner messing with them like Mid said? Basically, look for changes at every step of the way, before conversion. Once you know that the binary .sbd file has not changed, you're getting really close I would guess.
Açieeed! style by