www.webdeveloper.com
Results 1 to 12 of 12

Thread: Problem with headers being sent

  1. #1
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81

    Problem with headers being sent

    I'm getting the following error when I try to run a login script:

    Warning: Cannot modify header information - headers already sent by (output started at /path/to/folder/login.php:3) in /path/to/folder/login.php on line 76

    Warning: Cannot modify header information - headers already sent by (output started at /path/to/folder/login.php:3) in /path/to/folder/login.php on line 79

    Warning: Cannot modify header information - headers already sent by (output started at /path/to/folder/login.php:3) in /path/to/folder/login.php on line 86


    Here is my code.. the problem is that when I run it on my private XAMPP server environment it works perfectly, but when I run it on the public server it gives me the error
    PHP Code:
    //line 3 require_once "../inc/db.php";

    //this section starts at line 64
    $password=sha1($_POST['pass']);
    $returned $db->query("SELECT * FROM members WHERE username = '".$_POST['username']."' and pass = '".$password."'");
    $returned->execute();
    $row=$returned->fetchAll(PDO::FETCH_ASSOC);

    //if user is found
        
    if(count($row)==1){
            
            
    $results=$row[0];
            
    $username=$results['username'];
            
            
    //login
            
    setcookie('[cookiename]',$username,time()+60*30,'/',"","",true);
            
            if(
    $username != "[specificname]" || $username != "[specificname]"){
                
    setcookie('[website]_login_time_for_'.$username,time(),0,'/',"","",true);
            }
            
            
    //update last login date
            
    $db->query("UPDATE members SET last_login='".date("Y-m-d H:i:s")."' WHERE username='".$username."'");
            
            
    //redirect
            
    header("Location:../index.php");
        }else{
            
    header('Location:../index.php?login_invalid');
        }

    Last edited by thewebiphyer; 08-02-2014 at 08:12 PM.

  2. #2
    Join Date
    May 2014
    Posts
    911
    You cannot send headers or set cookies if ANYTHING has been output by your code. This probably means you don't have <?php as the VERY FIRST LINE of your code. It's also why as a rule of thumb, I try to ONLY open <?php ONCE on a page, as the very first like, then use ECHO to output ALL markup so it's VERY clear when/where HTML is being output.

    Though if it's saying the problem is on the require line, it could be IN your db.php

    Without seeing the whole code it can be difficult to figure out where your problem is, it could be as simple as your db.php having a ?> at the end with whitespace after it.

    Remember, ANYTHING outside <?php ?> is output by php -- even linefeeds, tabs and spaces. That "starts output" meaning you can't do any of the stuff you're trying to do there after it.

    Though honestly, lose the bloody redirects and just include the proper result page. I don't know who started advocating using header() that way in PHP scripts, but whoever it was needs a good swift kick in the junk.
    Java is to JavaScript as Ham is to Hamburger.

  3. #3
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    Good heavens why didn't I think of that? I guess I just got so used to using headers for redirects that I never thought of doing it with includes. I'll definitely give that a shot. When I get back to work on monday.

  4. #4
    Join Date
    May 2014
    Posts
    911
    Quote Originally Posted by thewebiphyer View Post
    Good heavens why didn't I think of that? I guess I just got so used to using headers for redirects that I never thought of doing it with includes. I'll definitely give that a shot. When I get back to work on monday.
    I've been seeing people use header redirects more and more, and there's really no reason for it.

    Particularly since it looks -- at least on cursory inspection of what you've shown -- like you're using 'one index to rule them all' construction -- where the only file users should be allowed to call is index.php; a technique I really advocate for site building. I even have a mod_rewrite rule on my systems that force any non-static file call to the index.php in the root, which makes doing "friendly URL's" way easier. I do my version of that via a whitelist.

    Code:
    RewriteEngine On
    RewriteRule !\.(gif|jpg|png|css|js|html|htm|txt|ico|zip|gz|rar|pdf|xml|wav|mp3|mp4|mpg|flv|swf|mkv|ogg|avi|webm|woff|svg|eot|ttf)$ index.php
    Any request lacking a extension on that whitelist is sent to index.php -- I then parse $_SERVER['HTTP_REQUEST'] to figure out what's being asked for.

    I'm kind of guessing you're aiming to do something similar, since both your headers are sending to index.php

    Still, I'm trying to figure out who/what/why started that practice of header redirects - the time wasted on the extra handshaking for 'no good reason' is quite strange to me, and again, there's no real reason for it.

    Though I'm seeing it in more and more code. I've heard a few lame excuses for why it's done, but no good concrete reasoning for it. If anyone out there knows WHY it's being done, I'd love to hear it.
    Java is to JavaScript as Ham is to Hamburger.

  5. #5
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    Okay I tried putting in the include files and the page displayed fine with the includes but the setcookie commands still aren't working. I have to get that to work somehow. now I'm getting

    Cannot modify header information - headers already sent by (output started at /path/to/file/login.php:2) in /path/to/file/login.php on line 75

    (also same error about line 78)

    I've done code just like this plenty of times before and it worked. why is it screwing up now?

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    729
    Those line numbers are the key to your fix. They are where 'something' is being output, thus preventing your (later) cookie set to fail.

    Look for blank lines or space chars outside of php tags As was mentioned by d_s_ opening php and closing php is to be avoided just for this reason (as well as the additional complexity it adds to a script). (Personally I use one set of tags (actually just one tag) in my scripts 99% of the time. And I've got some pretty lengthy scripts.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  7. #7
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    Ah. I found the problem. Writing the require_once "file.php" as require_once("file.php"); solved it and still allows me to use header("location: bla bla bla")

  8. #8
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    729
    Well, that makes no sense to us since you never showed us that line of your code....

    It is generally recommended that to avoid browser incompatibilities (or something or like that) to use capital L in "Location: blablabla";
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  9. #9
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    Lol I actually did put that line of code in.. go look at it again.. it's commented out accidentally in this thread but it's there.. either way.. hope someone else can look at this and find their answer in similar problems

  10. #10
    Join Date
    May 2014
    Posts
    911
    Pretty much what ginerJM said and what I meant in the first para of my first post -- go through and check EVERY file to make sure that NOTHING, not even whitespace characters exist outside your <?php ?> tags, or is being echoed, printed, etc,etc... Given the error line, dimes to dollars it's the db.php that's got a errant space, enter, or tab in it someplace. Even a extra linefeed after the ?> could be your problem.

    ... also check the php logs to see if errors are being output. If something is sending an error that too could count as output.

    One of the reasons I like testing PHP on a local server -- I can watch the error logs in realtime.
    Java is to JavaScript as Ham is to Hamburger.

  11. #11
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    729
    Quote Originally Posted by thewebiphyer View Post
    Lol I actually did put that line of code in.. go look at it again.. it's commented out accidentally in this thread but it's there.. either way.. hope someone else can look at this and find their answer in similar problems
    Why would I need to look at a comment again??? (Not funny BTW.)

    Just goes to show how much we waste OUR time looking at code that Is Not What Was Being Executed, posted time and again by people looking for help.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  12. #12
    Join Date
    May 2014
    Posts
    75
    I got stuck the other day with a similar issue... A single space somehow appears before my opening " <?php" tag.
    Strange thing was I hadn't edited that code in a long time and suddenly my site went wonky.

    If you come across this error in the future a good method of tracking it is to add a random header like:

    PHP Code:
    header("muahaha: I'm evil!"); 
    Move it earlier and earlier through your code, when it stops giving errors you know you've moved past the problem point.
    I just zigzag back and forth until I find exactly where it is.

    Well... It can be faster when the problem is a real mystery.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles