包含的 PHP + MySQL 登录

标签 php html mysql authentication include

我正在为我使用 PHP 和 MySQL 创建的网站创建登录系统。数据库端我已经完成,一切正常。

这是我的问题。我的网站使用 includes。基本上我有 index.php,这是我的模板,我使用 id 将页面调用到该页面的主体中。 (即:index.php?id=about、index.php?id=admin 等)

按照登录教程的指示,我的管理页面 (admin.php) 将其置于顶部

<?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);
?>

但是因为 admin.php 被加载为 index.php?id=admin(为了使用我的模板)我得到这个错误:

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

我认为解决此问题的最简单方法是为我的所有管理页面创建另一个模板文件,然后将每个相应的页面包含到该模板文件中,但是有没有一种方法可以在不执行这些额外步骤的情况下执行此操作?

我真的希望有人理解我的问题,如果已经回答了很多很多次,我再次道歉。我真的用尽了搜索关键字来找到我真正要找的东西。

索引.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>

最佳答案

尝试将 session_start(); 放在 index.php 的最顶部

关于包含的 PHP + MySQL 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36860579/

相关文章:

php - MysQL 忽略 int 值 - 键 'PRIMARY' 的重复条目

html - 如何创建一个占据整个屏幕并在滚动时折叠到标题的div

python - 通过部分 href 查找元素(Python Selenium)

php - MySQL 日期时间和时间戳字段是否比 Unix 时间戳整数更适合 PHP 应用程序?

java - Android Studio MainActivity 类无法连接到本地 Mysql Workbench

php - PHP 中的数值数组和关联数组是两种不同的东西吗?

php - PHP 中的命名空间是什么?

php - 关于常数时间算法和字符串比较的说明

html - CSS 目标主题

php - 我想要分页在底部,它显示在右侧