header() prolems

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

matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

header() prolems

Post by matthiasone »

I am trying to redirect to a clean form after a data insert in te mySQL...but when I run the script.......the insert works but I get a blank page.

Code: Select all

function do_pages($area, $page="default", $view="", $user="", $id="", $eid="", $form_vars="")
{
  do_html_header();
  load_menu($user);
  if ($page == "default")
    load_default();
  elseif ($page == "userform")
    form_user($user, $id);
  elseif ($page == "cal")
  {
  	if($view == "yr")
      form_cal_yr($id, $user);
  	elseif($view == "mth")
      form_cal_mth($id, $user);
  	elseif($view == "day")
      form_cal_day($id, $user, $eid);
  	elseif($view == "new")
	{
      db_cal_entry($form_vars, $user);
	  $tdate = date("Y-n-j", strtotime($form_varsї"edate"]));
 	  header("Location: staffpage.php?area=form&page=cal&view=day&id=".$tdate."&user=".$user);
    }
  	elseif($view == "update")
	{
      db_cal_update($form_vars, $user);
	  $tdate = date("Y-n-j", strtotime($form_varsї"edate"]));	    
 	  header("Location: staffpage.php?area=form&page=cal&view=day&id=".$tdate."&user=".$user);
    }
  }	  
  do_html_footer();
	
}
Thanks
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

This may be a quick fix....

From the php manual
Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

Code: Select all

header("Location: http://".$_SERVERї'HTTP_HOST']
                      .dirname($_SERVERї'PHP_SELF'])
                      ."/".$relative_url);
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

that didn't work. But thanks for the suggestion.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Have you examined the url that the header is generating? Does it have all the name=value pairs you expected? Is it losing characters (do any fields need to be urlencoded?)? Have you confirmed that the variables are set properly in the _GET array? (You'll need to view the edit page, then update your source to print our the get array, save the source, then submit in order to avoid the can't send headers error message)

For instance, I can tell that if either page or view get corrupted(or aren't being set) you'll only output a header/footer/menu without content.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

have you checked your system/error-log for something like
'Warning: Cannot add header information - headers already sent by (output started at file.php:23)'

if this happens read this topic
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

Vodka - yes, I have check the error log........not entry for a header.

nielsene - the generated url looks correct. but like you said I get a menu and head and footer but no content.

Not exactly sure I fully understand urlencoding...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

The next thing I would try would be to add elses after both your if-elseif trees, with a simple echo "I'm here"; type line, just to see which variable is causing the problems. This won't fix anything, but it might help localize the problem.
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

Well I put echos in the first if to ehco the url and all the variable echoed out correctly. :?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

OK the next thing to try is:

Code: Select all

// snip
     elseif($view == "day")
     {
          echo "I should be here, $id, $user, $eid"; 
          form_cal_day($id, $user, $eid); 
     }
// snip
If you get the new message after the redirect then you know that something is wrong in form_cal_day and not in the code you showed us.
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

Ok here is the current snippet of code

Code: Select all

elseif($view == "new")
	{
      db_cal_entry($form_vars, $user);
	  $tdate = date("Y-n-j", strtotime($form_varsї"edate"]));
	  $relative_url = "staffpage.php?area=form&page=cal&view=day&id=".$tdate."&user=".$user;
	  echo $relative_url;
	  echo "Location: http://".$_SERVERї'HTTP_HOST'] 
                      .dirname($_SERVERї'PHP_SELF']) 
                      ."/".$relative_url;
 	  header("Location: http://".$_SERVERї'HTTP_HOST'] 
                      .dirname($_SERVERї'PHP_SELF']) 
                      ."/".$relative_url); 
    }
  	elseif($view == "update")
	{
      db_cal_update($form_vars, $user);
	  $tdate = date("Y-n-j", strtotime($form_varsї"edate"]));	    
 	  header("Location: staffpage.php?area=form&page=cal&view=day&id=".$tdate."&user=".$user);
    }
When run this the is the resulting echos:

Location: http://www.lufkin.org/staffpages/staffp ... -22&user=1
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

OK, just looked at it and tried adding an event (I hope that was alright). And yes, the URL looks fine. However there definately should have been an error message. Double check the logs for the "Cannot send headers" message. I know you said it wasn't there before when volka asked. If its not there now, ether PHP is dropping all errors without telling/logging or you're looking in the wrong place. If you find them in a new place and older ones are also there then this is the common problem and can be fixed by reading the sticky announcements at the top of this forum.
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

Well now there is an error "Cannot send headers" in the logs. How do I go about fixing that?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Take out the echo debug code and check if its still there.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you may also turn on output_buffering in php.ini
but think of it as quick&dirty - it's always better to avoid it
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

Well I still get the same message

[Thu Aug 22 11:17:51 2002] [error] PHP Warning: Cannot add header information - headers already sent by (output started at /usr/home/faog/staffpages/staffpage_fns.php:133) in /usr/home/faog/staffpages/staffpage_fns.php on line 54
Post Reply