Jump to content

Recommended Posts

Hi guys. I apologize in advance if this is tricky to understand. I am going to try to explain it as best as I can.

 

I am in the process of learning PHP and have a problem on my "register.php" page. The user gets entered in the database but I am trying to redirect the user to a thanks.php page after registering. I am getting this error:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/name/public_html/newsite/header.htm:7) in /home/name/public_html/newsite/register.php on line 73

 

My register page has an include file:

include ('header.htm');

 

In that include file, it has its own head tag.

 

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title></title>
<style type="text/css" media="all">@import "./includes/layout.css";</style>
<link href="styles.css" rel="stylesheet" type="text/css">
</head>

 

On my register.php page I have this code:

 

$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

			// Check for a trailing slash.
			if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
				$url = substr ($url, 0, -1); // Chop off the slash.
			}

			// Add the page.
			$url .= '/thanks.php';

			header("Location: $url");
			exit();

 

So it seems that this header("Location: $url"); isn't working because I have that include in the register page. When I comment out the include file at the top of my register page it works fine and directs the user to a thanks.php page.

 

So finally... My question is how would I make it still redirect to the thanks.php while keeping the include header?

 

 

Link to comment
https://forums.phpfreaks.com/topic/132472-cannot-modify-header-information/
Share on other sites

http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

 

Topic that is stickied explains about everything.

 

Basically you are outputting the HTML to the browser before calling the header function. That is not allowed, either move the processing to before the include(html) portion or use a javascript redirect:

 

$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
            
            // Check for a trailing slash.
            if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
               $url = substr ($url, 0, -1); // Chop off the slash.
            }
            
            // Add the page.
            $url .= '/thanks.php';
            
            echo '<script type="text/javascript>window.location = \'' . $url . '\';</script>';
            exit();

 

 

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.