Jump to content

Can You Validate Content Type Being Sent To PHP Script?


phpQuestioner

Recommended Posts

Is there a way to validate the content-type that is being sent to a PHP script? I have an example of what I am wanting to do; even though, I know it is not right - maybe you can see what I am trying to do.

 

 

<?php

$normal = header("Content-Type");

$acceptableheader="image/jpeg";

if ($normal != $acceptableheader) {
// create alternate image
exit;
}

?>

Link to comment
Share on other sites

The problem is that mime/types can be spoofed. According to the manual the input from the $FILES array should not be trusted. You can validate that an extension matches a list of allowable extensions but I'm not sure what the best practice is when trying to display it back to the screen via a browser.

Link to comment
Share on other sites

Is there a way to validate the content-type that is being sent to a PHP script? I have an example of what I am wanting to do; even though, I know it is not right - maybe you can see what I am trying to do.

 

 

<?php

$normal = header("Content-Type");

$acceptableheader="image/jpeg";

if ($normal != $acceptableheader) {
// create alternate image
exit;
}

?>

Checking the headers the client sends and the extension the client sends is useless; anyone can send false ones, unless they are complete retarded and can't even type a question into Google.

You need you find a way to actually validate the data itself and make sure it's what you are looking for.

 

For example usually a PNG file will begin with "‰PNG" at the start. Just open stuff up in a text editor and find things that are always the same for that type of file, E.G. signatures. Then just have PHP check for these signatures and make sure they are in the right places. Be careful though, and make sure that they can never overlap another file type.

Link to comment
Share on other sites

The thing is, this is not for security reasons; this is to display a alt thumbnail when I do not have a image present. I have a "image does not exist" thumbnail, but it is a GIF and I know I could easily convert the GIF to JPEG; but the GIF just looks better. I tried this script that I just made; but it did not validate it right. It did not display the alternate PNG thumbnail . So I am trying to create an alternate PNG file for thumbnails that do not display. The thumbnails have been created by querying my GD Library Script, so that the script will resize the original JPEG size of 640X480.

 

<?php

$aict = header("Content-type: image/jpeg");

if ($aict != image/jpeg) {
        $img_handle = ImageCreate (75, 75) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 0, 0);
        $txt_color = ImageColorAllocate ($img_handle, 255, 255, 0);
        ImageString ($img_handle, 31, 14, 20,  "No", $txt_color);
        ImageString ($img_handle, 31, 10, 40,  "Image", $txt_color);
        header("Content-type: image/png");
        imagepng($img_handle);
// do not allow GD Library Script To Be Queried In Order To Display Thumbnail Image, Because This Is Not A JPEG Image
exit;
}

?>

Link to comment
Share on other sites

ok - tried this:

 

<?php

$acceptable = mime_content_type('php.jpeg');

if (($acceptable != image/jpeg) {
        $img_handle = ImageCreate (75, 75) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 0, 0);
        $txt_color = ImageColorAllocate ($img_handle, 255, 255, 0);
        ImageString ($img_handle, 31, 14, 20,  "No", $txt_color);
        ImageString ($img_handle, 31, 10, 40,  "Image", $txt_color);
        header("Content-type: image/png");
        imagepng($img_handle);
exit;
}

?>

 

but no image to display at all, not even ones that are really there

 


 

Before the code above I tried this code, because thumbnail images are being made with query string variable "$filename".

 

<?php

$acceptable = $filename['extension'];

if (($acceptable != JPG) || ($acceptable != jpg)) {
        $img_handle = ImageCreate (75, 75) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 0, 0);
        $txt_color = ImageColorAllocate ($img_handle, 255, 255, 0);
        ImageString ($img_handle, 31, 14, 20,  "No", $txt_color);
        ImageString ($img_handle, 31, 10, 40,  "Image", $txt_color);
        header("Content-type: image/png");
        imagepng($img_handle);
exit;

?>
}

 

but still no images at all........

Link to comment
Share on other sites

I probably just am not getting getimagesize() function, but I tried to write this script anyways - but it did not work either.

 

 

<?php

$source = imagecreatefromjpeg($filename);

$acceptable = getimagesize("img/jpeg");

if (($source != $acceptable) {
        $img_handle = ImageCreate (75, 75) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 0, 0);
        $txt_color = ImageColorAllocate ($img_handle, 255, 255, 0);
        ImageString ($img_handle, 31, 14, 20,  "No", $txt_color);
        ImageString ($img_handle, 31, 10, 40,  "Image", $txt_color);
        header("Content-type: image/png");
        imagepng($img_handle);
exit;
}

?>

 

 

I also tried this with mime_content_type

 

<?php

$source = imagecreatefromjpeg($filename);

$acceptable = mime_content_type('php.jpeg');

if (($source != $acceptable) {
        $img_handle = ImageCreate (75, 75) or die ("Cannot Create image");
        $back_color = ImageColorAllocate ($img_handle, 0, 0, 0);
        $txt_color = ImageColorAllocate ($img_handle, 255, 255, 0);
        ImageString ($img_handle, 31, 14, 20,  "No", $txt_color);
        ImageString ($img_handle, 31, 10, 40,  "Image", $txt_color);
        header("Content-type: image/png");
        imagepng($img_handle);
exit;
}

?>

 

 

Neither of the two of these worked.

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.