Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

[PHP] Need help on Error

Options
  • 22-01-2008 10:15pm
    #1
    Registered Users Posts: 7,041 ✭✭✭


    I'm getting an error in my php script. It works fine on localhost but the error occurs when I upload it.

    [PHP]Warning: Cannot modify header information - headers already sent by (output started at D:\path\to\file\login.php:10) in D:\path\to\file\login.php on line 35[/PHP]

    I've done a bit of googling and found the error is normally caused by whitespace before session_start() however there is no whitespace in mine.

    The code
    [PHP]<?PHP // <<Line 10
    if(isset($_POST) && isset($_POST)){

    require('library.php');

    $user = $_POST;
    $pass = $_POST;

    $connection = new connectDb;
    $connection->connect();
    $connection->select_db();


    $login = new login;
    $login->escape_input($pass,$user);
    $login->check_user();
    $login->check_pass();

    if($login->logged()=='True'){
    if(!isset($_SESSION)){
    $_SESSION='admin';
    }
    header('location: newpage.php'); // <<Line 35
    }else{
    echo 'Invalid Loggin';
    }
    $connection->disconnect();

    }[/PHP]

    Any help appreciated.


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Don't know much about php, but in ASP that error usually happens because you've output something before you try and redirect to a new page. Looking at your line 35 that looks like what you're doing, so you need to find where you're outputting something. What do you have before line 10?


  • Registered Users Posts: 927 ✭✭✭decob


    where are you calling session_start() ??? is it at the very top of that page.?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Well, what's before line 10? If there's anything whatsoever output before the cookies, it won't work.

    If you're going to use sessions, the first 2/5 characters of line 1 of your script must be
    <?php

    :)

    Remember that anything outside of the <?php ?> tags will be output to the browser. So if that's line 10 of your script, there is something being output.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    The first 9 lines
    [PHP]<?PHP
    session_start();
    ?>
    <html>
    <head>
    <title></title>
    <link rel='stylesheet' type='text/css' href='../Styles/adminstyle.css'/>
    </head>
    <body>[/PHP]

    The form is on the same page (underneath were the last snippet I gave you stopped) and it refreshes to that page which checks if the form login validates. Could that be a factor? The $_POST is being sent before the $_SESSION?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    D'oh! Totally missed your call to header();

    header() has the same restrictions as setting sessions and cookies. If you've already output anything to the browser, you can't call header()

    If you need to use header to redirect someone, do all your validation/processing before you output any HTML.

    Also remember to call die() after calling header(). This will stop the script from executing. If you don't call die(), something may get executed that you didn't want, e.g.
    [php]
    <?php

    if($_COOKIE != "admin") {
    header("location: error.php"); //The browser gets redirected here
    }

    mysql_query("DROP TABLE my_really_important_data"); //This line still gets executed by the server.

    ?>[/php]


  • Advertisement
  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    So shift all my validation above the html tags and add die() after my header()?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Yep. :)


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Thank you so much. Its so fusterating when you work on something offline, go to upload it and it breaks. The other day I found out my account didn't support PHP 5 causing a crash, then this! I hope thats the last of it.

    Thanks again :D.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    add this code at the very top of your pages and should sort out your problem:
    [php]
    <?php
    session_start(); // Initialize session data
    ob_start(); // Turn on output buffering
    ?>
    [/php]
    also before calling the header add
    [php]
    ob_clean();
    header("location: error.php"); //The browser gets redirected here
    [/php]


Advertisement