Jump to content

Problematic headers


xXREDXIIIXx

Recommended Posts

OK you fantastic PHPfreakers I'm not sure how to describe this so I hope you will understand..

 

I have a predicament where I set the header info to: header('Content-Type: text/html; charset=utf-8'); but later on I have a part of my site that requests an image from somewhere else using getImage() here I need to set header("Content-type: image/jpeg"); in order for the image to show correctly. but this conflicts with the previous page of headers already sent.

 

so I was looking for a way around this OR maybe can I load them in after the page has loaded? I have tried using Jquery to post with ajax but I don't seem to get any response the alert does not trigger for any conversation with an image.

 

I have tested all of this and it works as long as I don't put any OUTPUT before triggering the getImage()

<script type='text/javascript'>
	$(document).ready(function(){
		function loadImages(){										
			$.ajax({
			type: "POST",
			url: "/admin/getImage.php",
			data: {token: '<?php echo $token; ?>', chatId: '<?php echo $chat; ?>', uId: '<?php echo $uId; ?>'},
			success: function(data){		
				alert(data);
				$('#img').html(data);
			}
		});
		return false;
		}
	});
</script>

here is part of the messages page that would show the image. (just imagine there was more content etc..)

<?php 
	header('Content-Type: text/html; charset=utf-8'); 
	## OTHER GENERIC STUFF		
	$parser = new Parser($v1, $v2);			
	$convo = $parser->getConvo($token, $id);

	foreach($convo['messageGroups'] as $con)
	{	
		$chat = $con['messageGroupId'];		
			
		$uId = $con['latestMessage']['messageUid'];
		$key = $con['latestMessage']['messageKind'];						
		if($key == '3'){
										
			$image = $parser->getImage($token, $chat, $uId);
			echo $image;
		}
		
	}
?>

here is the function for getImages()

public function getImage($token, $chat, $uId){
	$this->initCURL();
	$chatter = str_replace("{{chat}}", $chat, $this->url['image']);
	$chatter = str_replace("{{uId}}", $uId, $chatter);
			
	$headerData = $this->defaultGETHeader($token);
			
	curl_setopt($this->curl, CURLOPT_URL, $chatter);
	curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headerData);
			
	$output = curl_exec($this->curl);
	$this->closeCURL();
			
	header("Content-type: image/jpeg");
			
	return $output;
}
Edited by xXREDXIIIXx
Link to comment
Share on other sites

Setting the same header multiple times does not lead to conflcts. The only reason why you're getting an error message is because you have output before the second header() call, which is not allowed.

 

Looks like you've forgotten the CURLOPT_RETURNTRANSFER.

 

Exactly, but I already knew WHY I was getting the error message, I was asking if there was a way around it. Like I said it executes mid-page and possibly multiple times depending if there is more than 1 image.

 

Not exactly sure what CURLOPT_RETURNTRANSFER. does but I added it. was working fine before without it.

Link to comment
Share on other sites

This simply makes no sense.

 

It seems you misunderstand how HTTP and HTML works. An HTTP response contains a single resource with exactly one type: You can either send an HTML document or one(!) image, but not HTML mixed with multiple images or something like that.

 

If you want to use images in HTML, you need an img element with a URL pointing to the image content:

<img src="https://yoursite.com/images/cute_kitten.jpg" alt="cute kitten">

The image source may very well be a PHP script which outputs the image data dynamically:

<img src="/admin/getImage.php?id=1234" alt="some sensible description">

I guess that's what you want. The getImage.php script would take the image ID from the URL and output this particular image.

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.