我遇到以下幾個問題。使用include語句,?view - ?view被重定向到錯誤include
基本上我建立使用PHP/SQL登錄系統。
根文件夾(WWW)包含CSS樣式,在index.php和其他一些PHP文件它們是不相關的。
此文件夾中還有一個名爲views的文件夾。在「視圖」中還有2個文件夾:主要和佈局。
佈局文件夾中包含的,僅僅在他們1行PHP文件一個2-14。 e.g
<h1> You have logged in! </h1>
(loginconfirm.php),或
<h1> Log in failed </h1>
(loginfailed.php)。
主文件夾中包含2個PHP文件, 「佈局」 和 「loggedinlayout」。
在我的索引文件,在最後 - 我有這樣的:
include 'views/main/'.$controller.'.php';
的想法是,當用戶登錄時,我$控制器改變從=佈局,= loggedinlayout。這將導致新的佈局被加載。
在現實中,這並不工作,我can'get了包括線路的工作,它只會繼續使用它已經使用前什麼。
<?php
// occasional bugs bring up notices, this ignores them
error_reporting(E_ALL^E_NOTICE);
// includes the database and cart functions
include('db_function.php');
session_start();
// defaults to index view unless a different view is requested
$view = empty($_GET['view']) ? 'welcome' : $_GET['view'];
function validateUser()
{
session_regenerate_id(); //this is a security measure
$_SESSION['valid'] = 1;
$_SESSION['userid'] = $userid;
}
function isLoggedIn()
{
if(isset($_SESSION['valid']) && $_SESSION['valid'])
return true;
return false;
}
function logout()
{
$_SESSION = array(); //destroy all of the session variables
session_destroy();
}
switch($view) {
case "logout";
logout();
$controller='layout';
//$_SERVER['DOCUMENT_ROOT'];
header('Location:index.php');
break;
case "login";
$user = ($_POST['username']);
$pw = ($_POST['password']);
session_start(); //must call session_start before using any $_SESSION variables
//connect to the database here
db_connect();
$qry = "SELECT userid FROM user WHERE username='$user' AND password='$pw'";
$result = mysql_query($qry);
if(mysql_num_rows($result) < 1) //no such user exists
{
header('Location: ?view=loginfailed');
die();
}
else
{
validateUser(); //sets the session data for this user
}
//redirect to another page or display "login success" message
header('Location: ?view=loginconfirm');
break;
}
//used for layout
// the layout folder is where we put all the different views php files,
// such as the index (first page displayed, even though there is another index)
// or view players for example
if(isset($_SESSION['valid']) && $_SESSION['valid'])
{
$controller = 'loggedinlayout';
}
$controller = 'layout';
// KEEP THIS HERE ALWAYS - THIS IS OUT OF THE CASE/VIEW SECTION
include 'views/main/'.$controller.'.php';
?>
你們有沒有什麼建議?
的問題是 - 當我登錄,它shoudld我重定向到登錄確認頁面,使用「loggedinlayout」的佈局,而不是「佈局」
,如果你不能打擾格式化你的代碼,爲什麼要別人不屑於環繞你惹他們的頭?順便說一句,在'include'語句之前你有一個無條件的'$ controller ='layout'。可能與你的問題有關。 –
我的歉意,我的格式有一些錯誤,是的。 –