I can see this topic has been discussed numerous times and I have basic understanding of the theory here but I just can't see where the problem is in my code.
I am working on a project that has some processes that require a user login (by site administrators) so am working on a login function. The way my Pages is structured roughly is the user navigates to what I am calling the parent page:
http://phzfitness.com/Site_Admin.php (username: CraigB Password: password).
This page has a number of included php files (child pages) one of which is the Admin Console (../inc/adminConsole.inc.php) which contains an if statement that should either display the admin console (a few links to things like add new admin user, upload photos and create blog entry) if the user is logged in else another php file (grandchild page) is included which contains the log in form.
So when I land on my page I am presented with the login form, when I log in I have set it to redirect using to the same page and thats when I get the following message:
Warning: Cannot modify header information - headers already sent by (output started at /home2/titch77/public_html/phzfitness.com/Site_Admin.php:13) in /home2/titch77/public_html/phzfitness.com/inc/login_new.php on line 14
line 13 refers to a seemingly unrelated code <?php opening tag for the first include (child page) which is the navigation menu.
Here is the code for the parent page:
<?php
require_once 'core/init.php';
?>
<HTML>
<head>
<link href="CSS/dark.css" rel="stylesheet" type="text/css">
<title>Admin Console</title>
</head>
<body>
<div id="wrapper">
<!--### Navigation ###-->
<div id="navigation">
<?php
include_once('inc/nav.inc.php');
include_once('inc/social.inc.php');
?>
</div>
<!--### Header ###-->
<div id="header">
<?php
include_once('inc/head.inc.php');
include_once('inc/promo.inc.php');
?>
</div>
<div id="main">
<?php
include_once('inc/adminConsole.inc.php');
?>
</div>
<!--Right Menu-->
<div id="Right" style="min-height:600">
<?php
include_once('inc/twit.inc.php');
?>
</div>
<div id="footer">
<?php
include_once('inc/footer.inc.php');
?>
</div>
</div>
</body>
</HTML>
code that goes into adminConsole.inc.php
<?php
$user = new user();
if($user->isLoggedIn()) {
?>
"<p>Admin Console</p>";
Welcome <?php escape($user->data()->user_username); ?> You are logged in!<br>
<ul>
<li><a href="../blogpost.inc.php" target="_blank">Crete a new blog entry</a></li>
<li><a href="../add_user.php" target="_blank">Register new user</a></li>
</ul>
echo '';
<?php
} else {
//include_once('inc/loginform.inc.php');
include_once('inc/login_new.php');
}
Finally the code that goes into login_new.php (This is where it starts to go wrong)
<?php
if(input::exists()) {
if(Token::check(input::get('token'))){
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array('required' => true),
'password' => array('required' => true)
));
if($validation->passed()) {
// log user in
$user = new user();
$login = $user->login(input::get('username'), input::get('password'));
if($login) {
header('Location: '.$_SERVER['PHP_SELF']);
//redirect::to('Site_Admin.php');
} else {
echo 'failed to log in!';
}
} else {
foreach($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
}
?>
<link href="../CSS/Forms.css" rel="stylesheet" type="text/css">
<form class="dark-matter" action="" method="post">
<h1>Admin Log in
<span>You need to log in to use these features!</span>
</h1>
<div class="field">
<label for="username"><span>Username :</span>
<input type="text" name="username" id="username" placeholder="Username" autocomplete="off">
</label>
</div>
<div class="field">
<label for="password"><span>Password :</span>
<input type="password" name="password" id="password" placeholder="Password" autocomplete="off">
</label>
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>"
<label> <span> </span><input type="submit" name="button" value="Login" class="button button-primary"></label>
</form>
There are a number classes contained within other php files but wasn't sure if necessary at this stage. Thanks in advance for any assistance.