我是php编码的新手,我希望你可以帮助一个java-boy摆脱困境。
我尝试在“index.php”中动态地包含导航菜单和内容。 如果Í测试下面的代码,我只会再次获得相同的index.php,而不会更改导航菜单,这在我看来会发生,但也许我不知道php足够好。
我的 index.php :
<?php
//^ E_NOTICE ^ E_WARNING
error_reporting(E_ALL);
if (!defined("ABS_PATH"))define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);
if (!isset($_SESSION['login'])) {
session_start();
$_SESSION['login'] = 1;
$_SESSION['role'] = "guest";
$_SESSION['navi'] = "/view/navigation_guest.php";
$_SESSION['content'] = "/view/content_login.php";
$_SESSION['calendar'] = "/view/calendar.php";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Klausurplaner</title>
<link href="view/design.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="frame">
<div id="header">
<?php
include_once "/view/header.php";
?>
</div>
<div id="main">
<div id="navi">
<ul>
<?php
include_once $_SESSION['navi'];
?>
</ul>
</div>
<div id="content">
<?php
include_once $_SESSION['content'];
?>
</div>
<div id="calendar">
<?php
include_once $_SESSION['calendar'];
?>
</div>
</div>
</div>
</body>
</html>
如果我尝试使用以下内容修改index.php,如上所述,我只会再次获得相同的网站,而不会更改导航菜单。
content_login.php :
<?php
if (!defined("ABS_PATH"))define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);
include_once(ABS_PATH . "/Klausurplaner/Control/LoginControl.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="design.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>Bitte loggen Sie sich ein.</h1>
<form method="post" action="control/btnLoginClicked.php">
<p1>Benutzername:</p1>
<br>
<input id="edt_username" type="text" size="25%"/>
<br>
<p1>Passwort:</p1>
<br>
<input id="edt_password" type="password" size="25%"/>
<br><br>
<input name="btn_login" type="submit" value="login" size="5%"/>
</form>
<br><br>
<!-- <br><br>
<a href="content_guest_newPassword.php">Passwort vergessen</a>
<!-- E-Mail an die eigene E-Mail-Adresse mit neuem PW -->
</body>
</html>
btnLoginClicked.php :
<?php
if (!defined("ABS_PATH"))define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);
/**
* @author Falk Libor
* @version 1.0
*
* Date: 19.03.2013
*/
if (isset($_POST['btn_login'])) {
$_SESSION['navi'] = "/view/navigation_admin.php";
$path = ABS_PATH . "/Klausurplaner/index.php";
header("Location: " . $path);
exit();
}
?>
编辑: session_start()现在在if-block之前,但在我点击“btn_login”之前它现在以某种方式显示“navigation_admin.php”而不是定义的“navigation_guest.php”在“content_login.php”中。有什么想法吗?
答案 0 :(得分:2)
$ _SESSION数组在您使用session_start()之前不存在,因此在启动它之前,您永远不会从会话中获取任何值,也不能添加它。
答案 1 :(得分:1)
尝试在
之前调用session_start
session_start();
if (!isset($_SESSION['login'])) {
$_SESSION['login'] = 1;
$_SESSION['role'] = "guest";
$_SESSION['navi'] = "/view/navigation_guest.php";
$_SESSION['content'] = "/view/content_login.php";
$_SESSION['calendar'] = "/view/calendar.php";
}