header location help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

header location help

Post by Addos »

Hi,
I’m having trouble getting the ‘header location’ below to work after all the if statements are passed. Either it takes me to the admin_images_successful.php page or gives me the warning headers already sent message depending on where I place it. I’m hoping it’s simple but I have tried for many hours and can’t get a satisfactory result. Any help would be very welcome.
Thanks
Brian

Code: Select all

<? 
$nomessage='';
if ($_POST && array_key_exists('MM_insert',$_POST)) {
if (isset($_POST['fupload']) && !empty($_POST['fupload'])) {
 }
  else { 
  $nomessage = 'You must fill out the field';} 
if (isset($_FILES['fupload'] )) {
print "name: ". $_FILES['fupload']['name'] . "<br />";
print "size: ". $_FILES['fupload']['size']. "bytes<br />";
print "temp name: ". $_FILES['fupload']['tmp_name']. "<br />";
print "type: ". $_FILES['fupload']['type']. "<br />";
print "error: ". $_FILES['fupload']['error']. "<br />";
if ( $_FILES['fupload']['type'] == "image/jpeg") {
$source = $_FILES['fupload']['tmp_name'];
$target = "gallery_06/" .$_FILES['fupload']['name'];
move_uploaded_file( $source, $target ); // or die ("coultn't copy");
$size = getimageSize($target);
$imgstr = "<p><img width=\"$size[0]\" height=\"$size[1]\" ";
$imgstr .= "src=\"$target\" alt=\"uploaded image\" /> </p>";
print $imgstr;
 }} }
 header('Location: admin_images_successful.php');
 ?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please indent your code more carefully

Code: Select all

<?php
$nomessage='';
if ($_POST && array_key_exists('MM_insert',$_POST)) {
	if (isset($_POST['fupload']) && !empty($_POST['fupload'])) {
	}
	else {
		$nomessage = 'You must fill out the field';
	}
	if (isset($_FILES['fupload'] )) {
		print "name: ". $_FILES['fupload']['name'] . "<br />";
		print "size: ". $_FILES['fupload']['size']. "bytes<br />";
		print "temp name: ". $_FILES['fupload']['tmp_name']. "<br />";
		print "type: ". $_FILES['fupload']['type']. "<br />";
		print "error: ". $_FILES['fupload']['error']. "<br />";
		if ( $_FILES['fupload']['type'] == "image/jpeg") {
			$source = $_FILES['fupload']['tmp_name'];
			$target = "gallery_06/" .$_FILES['fupload']['name'];
			move_uploaded_file( $source, $target ); // or die ("coultn't copy");
			$size = getimageSize($target);
			$imgstr = "<p><img width=\"$size[0]\" height=\"$size[1]\" ";
			$imgstr .= "src=\"$target\" alt=\"uploaded image\" /> </p>";
			print $imgstr;
		}
	}
}
header('Location: admin_images_successful.php');
?>
header/location advises the client to load another document. What good would it do to display contents in this case?
Even worse, it cannot be done with http.

Let's see what I get with

Code: Select all

<?php
$fp = fsockopen('forums.devnetwork.net', 80) or die('merde');

$request = "GET /viewforum.php?f=1 HTTP/1.0\r\n" .
	"Host: forums.devnetwork.net\r\n" .
	"Connection: close\r\n" .
	"Accept: */*\r\n" .
	"Accept-Language: en,en-us\r\n" .
	"User-Agent: volka/3.0\r\n" .
	"\r\n";
	

fputs($fp, $request);
while(!feof($fp)) {
	echo fgets($fp);
}
?>
HTTP/1.1 200 OK
Date: Sat, 29 Jul 2006 19:19:05 GMT
Server: Microsoft-IIS/5.0
X-Powered-By: PHP/4.4.2
Set-Cookie: phpbb2mysql_data=...; expires=Sunday, 29-Jul-07 19:19:05 GMT; path=/; domain=.devnetwork.net
Set-Cookie: phpbb2mysql_sid=...; path=/; domain=.devnetwork.net
Cache-Control: private, pre-check=0, post-check=0, max-age=0
Expires: 0
Pragma: no-cache
Connection: close
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr">
<head>
First there are the http response headers: status code, cookies, Content-Type ect.
Then there is a blank line. This is the only seperator between response headers and response body, first blank line -> headers stop, body start.
There's no way back. And that's why you cannot send headers after the first tiny bit of the response body has been sent to the client.

And again: It doesn't make sense in case of a header/location either ;)
Post Reply