Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    the windy city
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cannot add header information error

    on this particular server ever php page has to have
    #!/usr/local/bin/php4
    as its first line.

    my php page has a redirect in it, but it does not execute. instead, i get the following:
    Warning: Cannot add header information - headers already sent by (output started at /cgi/login.php4:4) in /cgi/login.php4 on line 152

    line 152 is the one containing the redirect
    header("Location:$location");

    what's the workaround?
    something like
    header(current header here + "Location:$location");
    ?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    That error means you have already outputted something to the browser before you tried to send the header. You cannot output anything to the browser before sending a header. That means you cannot echo() or print() anything before sending a header. Also make sure you don't have spaces before your <?php because that will output spaces to the browser.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Dec 2002
    Location
    the windy city
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Spookster,
    here's my page:
    Code:
    #!/usr/local/bin/php4
    
    <head>
    <title>Please Log In</title>
    javascript and stylesheet go here
    </head>
    
    <body>
    
    <?php
    
    $pg = '<form name="frm" method="post" onKeyUp="highlight(event)" onClick="highlight(event)" onSubmit="return validate();" action="' . $_SERVER['PHP_SELF'] . '">' . 
    'Please sign in:<br><br>' . 
    '<table border="0">' . 
    '<tr>' . 
    '<td>Email: </td>' . 
    '<td><input type="text" name="email" value="' . $_POST['email'] . '">' . 
    '</tr>' . 
    '<tr>' . 
    '<td>Password: </td>' . 
    '<td><input type="password" name="pwd">' . 
    '</tr>' . 
    '<tr>' . 
    '<td colspan="2" align="right"><input type="submit" value="Submit" class="submit">' . 
    '</table>' . 
    '</form>';
    
    
    if (!$_POST['email'] || !$_POST['pwd']) {
    	echo($pg);
    }
    else {
    	if(($_POST['email'] == "") || ($_POST['pwd'] == "")) {
    		echo('<span style="color:red">You did not fill in all the fields.</span><br/><br/>' . 
    		$pg);
    	}
    	else {
    		if($_POST['pwd'] != "hello") {
    			echo('<span style="color:red">Please enter a valid password.</span>' . 
    		$pg);
    		}
    
    		else {
    			send_mail();
    			$location = "hello.htm";
    			header("Location:$location");
    		}
    
    	}
    }
    
    function send_mail() {
    
    	$txt = "The following peraon had just accessed the restricted directory: " . $_POST['email'];
    
    
    	// $headers = "From: " . $_POST['Fname'] . " " . $_POST['Lname'] . "<" . $_POST['Email'] . ">\n" . 
    
    	$headers = "From: " . $_POST['email'] . "<email here>\n" .
    	"X-Mailer: PHP\n" .  //mailer
    	"Return-Path: " . $_POST['email'];
    
    	@mail('my email here','Your directory was accessed',$txt,$headers);
    
    }
    
    ?>
    
    </body>
    </html>
    i am not writing anything to a browser in the else portion containing teh redirect. am i not getting the point?

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Can't have the below, before the header

    <head>
    <title>Please Log In</title>
    javascript and stylesheet go here
    </head>

    <body>

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Location
    the windy city
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so what should i do?
    what should i modify for the whole thing to work?
    move that into <?php part?

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    A simple way would be to do this
    PHP Code:
    <?php
    $htmlheaders 
    "
    <html>
    <head>
    <title>Please Log In</title>
    javascript and stylesheet go here
    </head>

    <body>"
    ;

    $pg '<form name="frm" method="post" onKeyUp="highlight(event)" onClick="highlight(event)" onSubmit="return validate();" action="' $_SERVER['PHP_SELF'] . '">' 
    'Please sign in:<br><br>' 
    '<table border="0">' 
    '<tr>' 
    '<td>Email: </td>' 
    '<td><input type="text" name="email" value="' $_POST['email'] . '">' 
    '</tr>' 
    '<tr>' 
    '<td>Password: </td>' 
    '<td><input type="password" name="pwd">' 
    '</tr>' 
    '<tr>' 
    '<td colspan="2" align="right"><input type="submit" value="Submit" class="submit">' 
    '</table>' 
    '</form>';


    if (!
    $_POST['email'] || !$_POST['pwd']) {
        echo(
    $pg);
    }
    else {
        if((
    $_POST['email'] == "") || ($_POST['pwd'] == "")) {
            echo(
    '<span style="color:red">You did not fill in all the fields.</span><br/><br/>' 
            
    $pg);
        }
        else {
            if(
    $_POST['pwd'] != "hello") {
                echo(
    '<span style="color:red">Please enter a valid password.</span>' 
            
    $pg);
            }

            else {
                
    send_mail();
                
    $location "hello.htm";
                
    header("Location:$location");
            }

        }
    }

    function 
    send_mail() {

        
    $txt "The following peraon had just accessed the restricted directory: " $_POST['email'];


        
    // $headers = "From: " . $_POST['Fname'] . " " . $_POST['Lname'] . "<" . $_POST['Email'] . ">\n" . 

        
    $headers "From: " $_POST['email'] . "<email here>\n" .
        
    "X-Mailer: PHP\n" .  //mailer
        
    "Return-Path: " $_POST['email'];

        @
    mail('my email here','Your directory was accessed',$txt,$headers);

    }

    echo 
    $htmlheaders;
    ?>

    </body>
    </html>
    <edit>Just noticed that if it's done my way, all your stuff will be before the body tag... but hopefully you understand where html and stuff should be... before the header
    Last edited by Nightfire; 05-09-2003 at 10:49 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •