troubleshooting Question

PHP - Include file writes header data?

Avatar of jfrent
jfrent asked on
PHP
8 Comments1 Solution315 ViewsLast Modified:
Hi,

I'm trying to setup a 'Function' file that I can use in a number of pages.  The problem I'm running into is when I include the file in my PHP pages the Header Command (function?) does not work.  When I debug using the headers_sent function, it says that my function include is sending header information.  I've included my scripts (stripped down to the bare bones).

When I include the file Functions_test.php in my page, the Header function doesn't work.

If I comment out the include line and put the function in my page, the header redirect works fine.

I understand that the Header function has to be the first thing output on a page.  Does the include file somehow violate that rule?  Are you not able to use includes when you are going to redirect using the Header command?

Any help would be appreciated.
Thanks!
Functions_test.php:
<?php
function jsspecialchars( $string = '') {
    $string = preg_replace("/\r*\n/","\\n",$string);
    $string = preg_replace("/\//","\\\/",$string);
    $string = preg_replace("/\"/","\\\"",$string);
    $string = preg_replace("/'/"," ",$string);
    return $string;
}
?> 
 
login_test.php:
<?php
//Include file - When uncommented Header redirect does not work - headers_sent reports 'Headers already sent'
include('Scripts/Functions_test.php');
 
//Function - When uncommented (and include line commented) - Header redirect works fine
//function jsspecialchars( $string = '') {
//    $string = preg_replace("/\r*\n/","\\n",$string);
//    $string = preg_replace("/\//","\\\/",$string);
//    $string = preg_replace("/\"/","\\\"",$string);
//    $string = preg_replace("/'/"," ",$string);
//    return $string;
//}
 
$user = $_POST["username"];
 
//If username is specified, checkpassword
if ($user) {
 
	if(headers_sent($file, $line)){
		// ... where were the mysterious headers sent from?
		echo "Headers were already sent in $file on line $line...";
	}
	
	header("Location: User_Home.php");
	exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>User Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<form name="form1" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
  <table width="95" border="0" align="center" cellpadding="0" cellspacing="0">
  	<?php
		if ($error) { ?>
			<tr>
				<td align="center"><font color="#FF0000"><b><? print $error ?></b></font></td>
			</tr>
	<?php } ?>
    <tr>
      <td>User Name: </td>
    </tr>
    <tr>
      <td><p>
        <input name="username" type="text" value="<?php print $user; ?>">
      </p>      </td>
    </tr>
    <tr>
      <td><p>Password: </p></td>
    </tr>
    <tr>
      <td><p>
        <input name="password" type="password" value="<?php print $pass; ?>">
      </p>      </td>
    </tr>
    <tr>
      <td align="center">
	  	<input type="submit" name="Submit" value="Submit">
	  </td>
    </tr>
    <tr>
      <td align="center"><p><a href="Accounts/create_new.php">New Account</a></p>      </td>
    </tr>
    <tr>
      <td align="center"><p><a href="#">Forgot Username/Password?</a></p>      </td>
    </tr>
  </table>
  </form>
</body>
</html>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros