Navigate
Home
ArticleWiki
Forum
Newsletter
Links
Tech News
Welcome Guest.
Username:

Password:

Remember me

header function in php
Welcome, Guest. Please login or register.
February 07, 2012, 06:37:38 AM
11513 Posts in 1262 Topics by 496 Members
Latest Member: Beerdernill
Experts Round Table Network  |  Web Technologies  |  General Web Dev  |  header function in php « previous next »
Pages: [1]
Author Topic: header function in php  (Read 1227 times)
jood

Offline Offline

Posts: 5


« on: November 22, 2007, 11:45:03 PM »

I have just read nicholassolutions article on the header() funciton in php, and found it very instructive, however, I'm still unable to solve my problem.

Until recently I had a script using the header() function working on my site. Something must have changed and it no longer works. (php 4.4.7 / apache 1.3.33 / mysql 4 /
Both php and apache have been updated recently / magic_quotes_runtime is turned off)

The script was calling an image stored in binary code in a mysql database to display on a web page.
the page calling the image had the image html code
<img src="getImage.php?id=blah >
the file getImage.php has code in it that
1.connects to the database,
 2. makes a query using the id,
 3. finds out from info stored inthe db what the image binary type is and according to that result calls the header function, eg if image/jpeg
header('Content-Type: image/jpeg');
4. then ehcos the image binary data sored in db eg
echo $imageBinary;

It worked before and I can't see what is incorrect.

I hope someone might be able to point me in the right direction.

Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 501



WWW
« Reply #1 on: November 23, 2007, 10:07:51 PM »

Do you have a live demo we can view? That way we can check what headers are being sent and if so if they are being done correctly. Your above method should work though, so could possibly be that Apache may be overriding the mime types, but would need to see script in action to see what its outputting to browser.
Logged

[x] Fight | www.crypticmauler.com
"You must be
jood

Offline Offline

Posts: 5


« Reply #2 on: November 25, 2007, 03:35:58 PM »

thanks for your response.
You can view the page at:
http://rspas.anu.edu.au/director/newsitem.php?searchterm=194

Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 501



WWW
« Reply #3 on: November 25, 2007, 06:10:13 PM »

Ok I tried viewing the image's url and it came up with image error, I checked the HTTP headers and the correct headers are being sent. Only thing I can say is going on is either the image was corrupted in the database by the new install, possibly some sort of truncation of the data or different encoding. Try putting the original image back into database to see if it works, if not compare the data of the image to what is being stored in the database, if even 1 byte is being altered its enough to cause the image to error.
Logged

[x] Fight | www.crypticmauler.com
"You must be
jood

Offline Offline

Posts: 5


« Reply #4 on: November 25, 2007, 08:23:05 PM »

I uploaded the image  again and still same result at:
http://rspas.anu.edu.au/director/newsitem.php?searchterm=194

This problem goes across several web pages, and the images are sourced from different tables in the one database. Another example of a page that was working until recently, uses similar code, but draws from a different table is: http://rspas.anu.edu.au/grc/project_details.php?searchterm=negotiating%20change_violence_women_pakistan

Is there a quick way that I  can
'compare the data of the image to what is being stored in the database'
I can display the data of the one stored in the database on to the web page, and can open the original image file in a text editor, but don't really know how to do the comparison.

Given that it is not just the one image having a problem, is it possible that all the stored images have been corrupted? Is there something I should be checking with the systems administrator, ie apache settings for mime types etc

thanks for your patience I'm on a learning curve.
Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 501



WWW
« Reply #5 on: November 26, 2007, 04:27:10 AM »

quickest way to check is run http://www.php.net/manual/en/function.sha1-file.php on the image file and then run http://www.php.net/manual/en/function.sha1.php on the image data in database, if the resulting hashs do not match then image was altered in database.

It is possible that all images could have been corrupted, maybe the table or database types were changed or different encoding could be used, I don't know much about MySQL's compression features but it too could be the culprid. First thing is to see if the hashs match using above method.
Logged

[x] Fight | www.crypticmauler.com
"You must be
jood

Offline Offline

Posts: 5


« Reply #6 on: November 26, 2007, 04:31:39 PM »

Results at:
http://rspas.anu.edu.au/getImageTest.php

show
binary file is:
dbe9224a59a0270d60260b228b268e2d7189b616

file hash is:
dbe9224a59a0270d60260b228b268e2d7189b616

So I guess that is a good thing, ie images stored in database tables not corrupted. (and I've learnt something new!) but still don't know why it is not working.

thanks jood
Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 501



WWW
« Reply #7 on: November 26, 2007, 04:56:29 PM »

Ok I think I know what is wrong now, I checked out the image data from another JPEG from the site to the one the script is generating and the script one has whitespace before the image data. Make sure your script is not inserting a newline or even a blank space anywhere. From the looks of it whitespace is being put before the image data so an include/require file may have some space in it that is being then inserted into the script, if not then ensure the script itself is free of any whitespace before the data.
Logged

[x] Fight | www.crypticmauler.com
"You must be
jood

Offline Offline

Posts: 5


« Reply #8 on: November 26, 2007, 09:17:07 PM »

That's the answer! What had changed recently was our database connect statement, which is an include, and there were white spaces there. thank you so much for that. Everything is back to working now and I've learnt a few things along the way.
Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 501



WWW
« Reply #9 on: November 27, 2007, 08:18:40 AM »

You welcome! Its usually the most simple overlooked things that cause the most frustration, I've spent hours trying to debug something only to find I made a mistake as simple as a misspelled variable. Glad you got it worked out and in the process learned a few things for future use. Thank you for coming to ERT for your IT help.

« Last Edit: November 27, 2007, 08:21:24 AM by CrYpTiC_MauleR » Logged

[x] Fight | www.crypticmauler.com
"You must be
Pages: [1]
« previous next »
    Jump to: