PHP Login header location

I have the following code, but im blocked here..

<?php
require 'members/core/init.php';
$general->logged_in_protect();

if (empty($_POST) === false) {

$username = trim($_POST['username']);
$password = trim($_POST['password']);

if (empty($username) === true || empty($password) === true) {
    $errors[] = 'Sorry, but we need your username and password.';
} else if ($users->user_exists($username) === false) {
    $errors[] = 'Sorry that username doesn\'t exists.';
} else if ($users->email_confirmed($username) === false) {
    $errors[] = 'Sorry, but you need to activate your account.
                 Please check your email.';
} else {
    if (strlen($password) > 18) {
        $errors[] = 'The password should be less than 18 characters, without spacing.';
    }
    $login = $users->login($username, $password);
    if ($login === false) {
        $errors[] = 'Sorry, that username/password is invalid';
    }else {
        session_regenerate_id(true);// destroying the old session id and creating a new one
        $_SESSION['id'] =  $login;
        header('Location: http://www.site.ro/1/index.html');
        exit();
    }
}
}
?>

and the following:

<?php if ($page == 'login'): ?>
<?php endif; ?>

Can someone help me to combine that part of code?

I've tryed that, but tell me header already send..

<?php if ($page == 'login'): ?>
<?php
require 'members/core/init.php';
$general->logged_in_protect();

if (empty($_POST) === false) {

$username = trim($_POST['username']);
$password = trim($_POST['password']);

if (empty($username) === true || empty($password) === true) {
    $errors[] = 'Sorry, but we need your username and password.';
} else if ($users->user_exists($username) === false) {
    $errors[] = 'Sorry that username doesn\'t exists.';
} else if ($users->email_confirmed($username) === false) {
    $errors[] = 'Sorry, but you need to activate your account.
                 Please check your email.';
} else {
    if (strlen($password) > 18) {
        $errors[] = 'The password should be less than 18 characters, without spacing.';
    }
    $login = $users->login($username, $password);
    if ($login === false) {
        $errors[] = 'Sorry, that username/password is invalid';
    }else {
        session_regenerate_id(true);// destroying the old session id and creating a new one
        $_SESSION['id'] =  $login;
        header('Location: http://www.site.ro/1/index.html');
        exit();
    }
}
}
?>
<?php endif; ?>

Im beginner in php, so please understand me.
Thank you in advance.

Resolved !

That it's the final code:

<?php if ($page == 'login'):
require 'members/core/init.php';
$general->logged_in_protect();

if (empty($_POST) === false) {

$username = trim($_POST['username']);
$password = trim($_POST['password']);

if (empty($username) === true || empty($password) === true) {
    $errors[] = 'Sorry, but we need your username and password.';
} else if ($users->user_exists($username) === false) {
    $errors[] = 'Sorry that username doesn\'t exists.';
} else if ($users->email_confirmed($username) === false) {
    $errors[] = 'Sorry, but you need to activate your account.
                 Please check your email.';
} else {
    if (strlen($password) > 18) {
        $errors[] = 'The password should be less than 18 characters, without spacing.';
    }
    $login = $users->login($username, $password);
    if ($login === false) {
        $errors[] = 'Sorry, that username/password is invalid';
    }else {
        session_regenerate_id(true);// destroying the old session id and creating a new one
        $_SESSION['id'] =  $login;
        header('Location: http://www.site.ro/1/index.html');
        exit();
    }
}
}
endif; ?>

Once any content is sent to the user, you can not modify headers. This includes any errant whitespace or line breaks.

<?php if ($page == 'login'): ?>
<?php
require 'members/core/init.php';
$general->logged_in_protect();

You stop php execution long enough to send a single line break between lines 1 and 2.

Change this to the following and try again.

<?php if ($page == 'login'):

require 'members/core/init.php';
$general->logged_in_protect();

You may have some whitespace somewhere in your code.

  1. You can find it.

  2. Or the easiest solution for this is to turn on output buffering

What output buffering allows you to do is echo/print anything you want, and still maintain the ability to send header info anywhere in your page.

Related Articles
  • I have some code like so: $('.entry a:first').click(function() { <?php header("Location:" . "http://www.google.com"); ?> }); I would like to know how I can achieve this using Javascript. The PHP code is executed on the server, so
  • This question already has an answer here: How to fix “Headers already sent” error in PHP 12 answers I'm trying to build a form using php error checking on the same page. Everything seems to be working fine except redirecting visitors to another page
  • This question already has an answer here: How can I make a redirect page using jQuery? 37 answers Can I use header location in Jquery for redirection or refresh. Like in PHP header('location:www.google.co.in'); header("Refresh:1,url=home.php");
  • Possible Duplicate: Headers already sent by PHP I'm trying to set up a simple php username and password login. Everything is working correctly until I try to redirect to the login_success.php page. All information is below please help. ##############
  • Possible Duplicate: PHP header() redirect with POST variables I'm writing a script where form data is being submitted to another script. I want the second script to perform some error-checking on the submitted $_POST data, and if everything is okay,
  • This question already has an answer here: How to fix “Headers already sent” error in PHP 12 answers My header location fails to work on a server wich uses php 5.3.21. It does work on a localhost with 5.4.7. It is backwards compatible.. can anybody te
  • i've just made a sign script for my website. The form posts the data to itself and then checks to see if all the data is correct. if so it then loads a payment page. To load this payment page i use: header(location: payment.php); So i want to send da
  • Possible Duplicate: Headers already sent by PHP I'm having some difficulty with my php coding. I have 3 files, add.php, lib.php, and view.php I created a simple form, and when the user clicks submit, it should direct them to the view.php where it wil
  • I have a simple form which is inside IFRAME. When user click on SUBMIT, it redirects to a specific page on my server. The function I use for the redirect is header ('Location: mypage2.html'); exit (); But I want the new page to open in _top location,
You Might Also Like