Skip to main content
Participating Frequently
November 5, 2012
Answered

PHP Header Error on HTML Page

  • November 5, 2012
  • 1 reply
  • 5010 views

I'm trying to put some php I made into an html file but I'm running into a snag. My PHP script is a login in script and when I successfully login I get the folowing error:

Warning: Cannot modify header information - headers already sent by (output started at /home/tyharox1/public_html/login.php:64) in/home/tyharox1/public_html/login.php on line 83

Below is the code for the html file with with the php already writen in. Can anyone help me resolve this issue?

<?php

session_start();

include ('mysql.php');

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<!--[if lt IE 9]>

<script src="html5shiv.js"></script>

<![endif]-->

<link href='http://fonts.googleapis.com/css?family=Coda' rel='stylesheet' type='text/css'>

<link rel="stylesheet" type="text/css" href="mainmenu.css"/>

<link rel="stylesheet" type="text/css" href="submenu.css"/>

<link rel="stylesheet" type="text/css" href="mainpage.css"/>

<link rel="stylesheet" type="text/css" href="slideshow.css"/>

<link rel="stylesheet" type="text/css" href="ribons.css"/>

<script type="text/javascript" src="js/jquery-1.2.6.js"></script>

<script type="text/javascript" src="js/startstop-slider.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!-- TemplateBeginEditable name="doctitle" -->

<title>Home</title>

<!-- TemplateEndEditable -->

<!-- TemplateBeginEditable name="head" -->

<!-- TemplateEndEditable -->

<style type="text/css">

body {

          background-color: #222;

}

</style>

</head>

<body>

<div>

<img name="Logo" src="images/84087.jpg" width="100%" height="50px" alt="" />

</div>

<div id='mainmenu'>

          <ul>

                      <li class='active '><a href='Templates/index.html'><span>AITP</span></a></li>

                       <li><a href='#'><span>test</span></a></li>

                       <li><a href='#'><span>test</span></a></li>

                       <li><a href='#'><span>test</span></a></li>

                       <li><a href='#'><span>test</span></a></li>

          </ul>

    </div>

</div>

<div id="submenu">

                    <ul>

                                 <li class='active'><a href='Templates/index.html'><span>Home</span></a></li>

                                 <li><a href='#'><span>About Us</span></a></li>

                                 <li><a href='#'><span>Calendar</span></a></li>

                                 <li><a href='#'><span>Contact</span></a></li>

                                 <li><a href='#'><span>Members</span></a></li>

                    </ul>

          </div>

<div>

           

</div>

<div class="ribbon">

    <h3>Memeber Login</h3>

    <section id="filler">

          Please Login

    <?php

session_start();

include ('mysql.php');

if (isset ($_POST['submit'])) {

                    $username = mysql_escape_string($_POST['username']);

                    $password = mysql_escape_string(sha1($_POST['password']));

 

                    if (!empty ($username) && !empty ($password)) {

                                        $sql = mysql_query ("SELECT * FROM users

                                                                                          WHERE username='".$username."' AND

                                                                                          user_password='".$password."' LIMIT 1");

 

                                        if (mysql_num_rows ($sql) > 0) {

                                                  $_SESSION['loggedin'] = true;

                                                  $_SESSION['username'] = $username;

 

                                                  header("Refresh: 2; url=index.html");

                                                                   echo '<span style="color: #063; font-weight: bold;">Login Successful! You are now being redirected, Please Wait...</span>';

                                        }else{

                                                  echo 'Your username and/or password is incorrect!';

                                                  }

 

                    }else{

                              echo 'You must enter a suername and password!';

                    }

}else{

echo '<form action="login.php" method="post">

Username: <input type="text" name="username" /><br />

Password: <input type="password" name="password" /><br />

<input type="submit" name="submit" value="login" />

</form>';

}

?>

    <section id="date">

                    <span id="publisher">Publisher:</span> Jane Doe <span id="publisher">Date:</span> Oct. 10, 2012

    </section>

    </section>

</div>

                     

</div>

<div id="footer">

</div>

</body>

</html>

This topic has been closed for replies.
Correct answer Rob Hecker2

You cannot use the PHP header function after anything has sent to the browser. Anything. You can have 100 lines of PHP before header() as long as the script doesn't send anything to the browser.

So your options are to revise your code accordingly or use a javascript alternative, as in this example, where a little embedded php defines the actual url, but javascript is used to process it:

<script type="text/javascript">

window.location = "<?=$url?>";

</script>

1 reply

Rob Hecker2
Rob Hecker2Correct answer
Legend
November 5, 2012

You cannot use the PHP header function after anything has sent to the browser. Anything. You can have 100 lines of PHP before header() as long as the script doesn't send anything to the browser.

So your options are to revise your code accordingly or use a javascript alternative, as in this example, where a little embedded php defines the actual url, but javascript is used to process it:

<script type="text/javascript">

window.location = "<?=$url?>";

</script>

TyharoAuthor
Participating Frequently
November 5, 2012

How would I go about placing the peice of code in my php script?

I tried to replace the header line with it but I get a syntax error when I do. Sorry If this sounds like it should be easy, I'm still new to php.

Rob Hecker2
Legend
November 5, 2012

You can't place javascript in PHP, you must place it in your HTML. And you can't just put that script in your page and expect it to work. You must define the url to use for window.location. You don't have to use a variable. You can simply put the appropriate url in place of the variable.