Brooke Brooke - 3 months ago 9
HTML Question

PHP + MySQL Login for Includes

I am creating a login system for a website I have created using PHP and MySQL. The database end I have already done and everything is working.

Here is my problem. My website works with includes. Basically I have index.php which is my template and I work off of id's to call the pages into the body of that page. (ie: index.php?id=about, index.php?id=admin, etc.)

My admin page (admin.php) has this on top, as instructed by login tutorials

<?php
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysql_query("SELECT * FROM danusers WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
?>


But because admin.php is loaded as index.php?id=admin (for the sake of using my template) I get this error:


Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/home/content/08/10038808/html/sites/danintra/index.php:28) in
/home/content/08/10038808/html/sites/danintra/admin.php on line 2


The easiest way I see to solve this is to create another template file for all my admin pages and then include each respective page into that one, but is there a way to do this without those extra steps?

I really hope someone understands my question and once again I apologize if it has been answered many, many times. I literally have run out of search keywords to find what I am truly looking for.

index.php:

<?php
include 'dbconnect.php';

$sql = mysql_query ("SELECT * FROM `danintra` WHERE id='1'");
$row=mysql_fetch_array($sql);

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:600' rel='stylesheet' type='text/css'>
<title><?php echo $row['title'];?> | <?php echo $row['subtitle'];?></title>
<style>
body {text-align:center;margin-top:27px;margin-bottom:27px;background-color:#C3C3C3;font-family: 'Open Sans', sans-serif;}
.wrap {width:90%;max-width:1200px;margin:0px auto;background-color:#FFFFFF;}
.header {width:100%;}
.main {width:100%;}
.page {padding:40px;margin-bottom:40px;}
.footer {width:100%;}
.button-add {color:#FFFFFF;background-color:#e2393e;text-transform:uppercase;text-decoration:none;font-weight:bold;padding:15px;float:right;margin-top:10px;margin-bottom:10px;}
.pdf {color:#FFFFFF;background-color:#e2393e;text-transform:uppercase;text-decoration:none;font-weight:bold;width:100px;padding:15px;margin-top:50px;}
</style>
</head>
<body>

<table class="wrap" border=0 cellpadding=0 cellspacing=0>
<tr><td class="header">
<?php include ('header.php'); ?>
</td>
</tr><tr>
<td class="main">
<?php
$id=$_GET['id'];
if (!empty($id)) {
$page = $id. ".php";
if(file_exists("$page")){
include("$page");
}
else {
include("error.php");
}
}
else {
include("home.php");
} ?>
</td>
</tr><tr>
<td class="footer">
<?php include ('footer.php'); ?></td>
</tr>
</table>

</body>
</html>

Answer

Try to put session_start(); at the very top of your index.php