Forum Moderators: coopster

Message Too Old, No Replies

$insertGoTo error

String is right, but script is not using it.

         

inveni0

2:15 pm on Feb 22, 2006 (gmt 0)

10+ Year Member



I've built insert record forms hundreds of times, and this is the first one that I can't get to load a second page after the record is inserted. I compared the code with other scripts, and it's the same. It looks like this:

$insertGoTo = "notify.php?=";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?'))? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

The record is inserted just fine, and if I echo the $insertGoTo string, it shows exactly what it should. Why is it not loading?

coopster

5:35 pm on Feb 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you tried dumping the whole sprintf() format to see what that is showing? Also, I notice no scheme or hostname here such as

http://example.com/notify.php?name=value

HTTP/1.1 requires an absolute URI as argument to

Location:
including the scheme, hostname and absolute path, even though some clients accept relative URIs. See header() [php.net] for details.

[edited by: coopster at 4:47 pm (utc) on Feb. 23, 2006]

inveni0

4:44 pm on Feb 23, 2006 (gmt 0)

10+ Year Member



None of my forms have the full URL and they redirect just fine. Printing the sprintf does nothing, but this page is still identical to my others. Any other ideas?

coopster

4:49 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, I realize the redirects will work just fine on some servers but you may want to change your habits, that was the only point I was making there.


Printing the sprintf does nothing

Then there you go. Hard to redirect when there is nothing to redirect to.

inveni0

10:45 pm on Feb 23, 2006 (gmt 0)

10+ Year Member



What do you think the problem may be?

coopster

11:40 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You just said that when you dump the variable there is nothing there. So if
printf("Location: %s", $insertGoTo);

is showing this:
Location:

then it would only make sense that the redirect is failing. Still, I would probably look at two things here, first, why do this?
$insertGoTo = "notify.php?=";

I would just leave the "?=" part of it off
$insertGoTo = "notify.php";

The second thing you may want to look into is building a full URI. Yes, it will work but it will also work just fine if you do what was recommended. It is easy to do, just use $_SERVER variables to rebuild the redirect location.
$http = 'http' . ((isset($_SERVER['https']) && $_SERVER['https'] == 'on') ? 's' : '') . '://'; 
$host = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER['SCRIPT_NAME']);
$insertGoTo = "notify.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: $http$host$path/%s", $insertGoTo));

inveni0

2:35 pm on Feb 24, 2006 (gmt 0)

10+ Year Member



See, the sprintf command doesn't even print "Location:" There is simply nothing. Am I using the right print command? What should I use to print it?

khaki monster

3:15 pm on Feb 24, 2006 (gmt 0)

10+ Year Member



im also new to PHP...

but try to add ob_start(); before header(); function or any where else as long before header();

cheerz!...

coopster

4:53 pm on Feb 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to stop your script before you get to the header() function, too. So exit right after you print it out.
$http = 'http' . ((isset($_SERVER['https']) && $_SERVER['https'] == 'on') ? 's' : '') . '://';  
$host = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER['SCRIPT_NAME']);
$insertGoTo = "notify.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?'))? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
printf("Location: %s", $insertGoTo);
exit;

header(sprintf("Location: $http$host$path/%s", $insertGoTo));

inveni0

5:56 pm on Feb 24, 2006 (gmt 0)

10+ Year Member



Hey, thanks for your help, but I've changed my process a bit, so now it doesn't matter if it redirects; however, I've saved this thread to a text file should I encounter it again.