Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,934 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,842 people online right now. Registration is fast and FREE... Join Now!




Problem with php header() function

 
Reply to this topicStart new topic

Problem with php header() function

linuxunil
13 Mar, 2008 - 09:43 AM
Post #1

New D.I.C Head
Group Icon

Joined: 7 Mar, 2006
Posts: 46



Thanked: 2 times
Dream Kudos: 125
My Contributions
We used this code in one of my classes but i haven't been able to get it to work. I'm using the default apache and php on os x leopard.

I get this error Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/index.php:7) in /Library/WebServer/Documents/index.php on line 67

and this is the code from my index.php

CODE

<?php $page = isset($_GET['page']) ? $_GET['page'] : 'login'; ?>
<?php session_start(); ?>
<!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">
    <body>
    
        <?php
            switch ($page) {
                case 'login';
                    // provide login table [html code and form]
                    echo "
                        <table width='300' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#ccc'>
                            <tr>
                                <form name='form1' method='post' action='?page=process'>
                                <td>
                                    <table width='100%' border='0' cellpadding='3' cellspacing='1' bgcolor='#fff'>
                                        <tr>
                                            <td colspan='3'><strong>Member Login</strong></td></tr>
                                        <tr>
                                            <td width='78'>Username</td>
                                            <td width='6'>:</td>
                                            <td width='294'><input name='username' type='text' id='username'></td></tr>
                                        <tr>
                                            <td>Password</td>
                                            <td>:</td>
                                            <td><input name='password' type='password' id='password'></td></tr>
                                        <tr>
                                            <td> </td>
                                            <td> </td>
                                            <td><input name='submit' type='submit' value='Login'></td></tr></table>
                                </td></form>
                            </tr>
                        </table>";
                    break;
                case 'process';
                    // verify existence of form variables
                    if (!$_POST['username'] || !$_POST['password']) {
                        header('Location: ?page=login');
                    }
                    // set database connection variables
                    $dbhost = 'localhost';
                    $dbuser = 'root';
                    $dbpass = '';
                    $dbname = 'users';
                    
                    // connect to database
                    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Database connection failure');
                    mysql_select_db($dbname) or die('Database name invalid');
                    
                    // set username and password from form
                    $username = $_POST['username'];
                    $password = $_POST['password'];
                    
                    // protect SQL injection
                    $username = stripslashes($username);
                    $username = mysql_real_escape_string($username);
                    $password = stripslashes($password);
                    $password = mysql_real_escape_string($password);
                    
                    $dbsql = "SELECT * FROM logins WHERE username = '$username' AND password = '$password'";
                    $result = mysql_query($dbsql);
                    
                    // check for successful result
                    $count = mysql_num_rows($result);
                    
                    if ($count == 1) {
                        header('Location: main.php');
                        session_register("username");
                        session_register("password");
                    } else {
                        echo 'Invalid Username or Password...';
                    }
                    mysql_close($conn);
                    break;
                default:
                    header('Location: ?page=login');
            }
        ?>
    
    </body>
</html>

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Problem With Php Header() Function
13 Mar, 2008 - 11:06 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Remember that you can only use a header() function prior to anything going to the output stream. When you start printing your regular HTML code tags such as <HTML> or <BODY> you are putting those in the output stream. Even outputting any spaces or echoing content prior to header() is considered sending output to the client and thus HTML headers will have been sent.

To make a long story short, you can't display or show anything prior to using a header() call. This is why you typically find header() calls in scripts that handle info before rendering any of the HTML page or echoing any info.

As soon as you did this....

CODE

<!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">
    <body>
    


You sent your headers thus header() is no longer available.

Hope that helps. smile.gif

This post has been edited by Martyr2: 13 Mar, 2008 - 11:07 AM
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:44PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month