2

Here is my code:

My root directory is: root

An index.php file located at the root/index.php

<?php 
require_once('root/includes/initialize.php');
<?php template('header.php', 'TITLE');?>;
?>

<div id="main">
//SOME CONTENT
</div>

My initialize.php file gets all my core include files and puts them into one "require_once". Located in root/includes/initialize.php

<?php
//Define Path
defined('LIB_PATH') ? null : define('LIB_PATH', 'root/includes/');

//Core Functions
require_once(LIB_PATH.'functions.php');

//Core Objects
require_once(LIB_PATH.'_database.php');
require_once(LIB_PATH.'_session.php');

//Classes
require_once(LIB_PATH.'_user.php');
?>

updated**

My functions.php file includes a simple templating function that grabs a template file such as my header.php. It is located in root/includes/functions.php

<?php
//Templating
function template($path="", $pageTitle=NULL) {
    if ($pageTitle != NULL) {
        $_POST['page_title'] = $pageTitle;
    }
    include(root/public/templates/'.$path);
}
?>

My _session.php file takes care of my session control. Located in root/includes/_session.php

<?php

/**
* Class for Sessions
*/
class Session
{
    public $logged_in = FALSE;
    public $uid;

    function __construct() {
        session_start();
        $this->check_login();
    }

    public function check_login() {
        if (isset($_SESSION['uid'])) {
            $this->uid = $_SESSION['uid'];
            $this->logged_in = TRUE;
        } else {
            unset($this->uid);
            $this->logged_in = FALSE;
        }
    }

    public function logged_in() {
        return $this->logged_in;
    }

    public function login($user) {
        if ($user) {
            $this->uid = $_SESSION['uid'] = $user;
            $this->logged_in = TRUE;
        }
    }

    public function logout() {
        unset($_SESSION['uid']);
        session_unset();
        session_destroy();
        redirect(WEB_ROOT);
    }
}

$session = new Session();

?>

updated**

My header.php holds the top of all the pages in my site. Located in root/public/templates/header.php. This is the file I'm having trouble with, I cant figure out why I am unable to echo out the $session->uid or the $_SESSION['uid'] in this file.

<html>
<head>
    <!--CSS-->
    <link rel="stylesheet" type="text/css" href="root/public/css/style.css">

    <title>MY SITE</title>
</head>
<body>
    <div id="header">
        <div id="logo">
            <a href="root"><?php echo $_POST['page_title'];?></a>
        </div>
        <?php echo $session->uid;?> //DOESN'T WORK
    </div>

I am able to echo out everything just fine in my index.php file and the other files on my site, but not in the included header.php. Any one know why? Thanks.

4

2 に答える 2

2

session_start()セッション変数を設定または取得するすべてのphpファイルの先頭で呼び出す必要があります。私があなたが呼んでいるのを見る唯一の場所session_start()は1つのファイルにあります。

http://www.php.net/manual/en/session.examples.basic.php

<?php
session_start();
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}
?> 

サイドノートにも。私はあなたのクラスを見ていますが、セッションを開始する場所もどこにもSession見当たりません。$mySession = new Session();

アップデート:

IDEで基本的なファイル構造とコードを再作成し、この行をクラスに追加することで機能させました。

public function check_login() {
    if (isset($_SESSION['uid'])) {
        $this->uid = $_SESSION['uid'];
        $this->logged_in = TRUE;
    }
    else {
        unset($this->uid);
        $this->logged_in = FALSE;
        $_SESSION['uid'] = session_id();

        /*Add this next line */
        $this->uid = $_SESSION['uid'];
    }
}

index.phpを初めて実行したときは<?php echo $_SESSION['uid']; ?>、ヘッダーの一部だけが機能しました。リフレッシュして動作<?php echo $session->uid; ?>したので、2回エコーしました。これは、クラスがクラス変数にIDを割り当てていないことを示しています。うまくいけば、これは私の側で機能したので望ましい結果であるか、必要に応じてtweekすることができます。

ここで動作します

更新2:

関数ファイル(パスに一致するように編集しますが、文字列を返す必要があります)

<?php

//Templating
function template($path = "", $pageTitle = NULL) {
    if ($pageTitle != NULL) {
        $_POST['page_title'] = $pageTitle;
    }
    return "$path";
}
?>

次に、Index.phpファイルに代わりに次のように追加します。

<?php
require_once('initialize.php');
include(template('header.php', 'TITLE'));
//include('header.php');
?>

<div id="main">
    //SOME CONTENT
</div>
</body>
</html>

_session.phpファイル:

<?php

/**
 * Class for Sessions
 */
class Session
{

    public $logged_in = FALSE;
    public $uid;

    function __construct() {
        session_start();
        $this->check_login();
    }

    public function check_login() {
        if (isset($_SESSION['uid'])) {
            $this->uid = $_SESSION['uid'];
            $this->logged_in = TRUE;
        }
        else {
            unset($this->uid);
            $this->logged_in = FALSE;

            $_SESSION['uid'] = session_id();
            $this->uid = $_SESSION['uid'];
        }
    }

    public function logged_in() {
        return $this->logged_in;
    }

    public function login($user) {
        if ($user) {
            $this->uid = $_SESSION['uid'] = $user;
            $this->logged_in = TRUE;
        }
    }

    public function logout() {
        unset($_SESSION['uid']);
        session_unset();
        session_destroy();
        redirect(WEB_ROOT);
    }

}

$session = new Session();
?>

そしてheader.php

<html>
    <head>
        <!--CSS-->
        <link rel="stylesheet" type="text/css" href="style.css">

        <title>MY SITE</title>
    </head>
    <body>
        <div id="header">
            <div id="logo">
                <a href="root"><?php echo $_POST['page_title'];?></a>
            </div>
            <?php echo $session->uid; ?> //WORKS NOW
            <?php echo $_SESSION['uid']; ?> //WORKS NOW
        </div>
于 2013-02-19T19:28:47.180 に答える
0

質問はかなり曖昧です。私の推測では、index.phpファイルはにあるのでroot/index.php、インクルードパスは次のようになります。

require_once('root/includes/initialize.php');
include('root/public/templates/header.php');

正しくありません。で始まらない/ので、パスは相対的であり、の場所を考慮してindex.php、を含めroot/root/includes/initialize.phpます。その場合は、ページがないことで簡単に見つけることができ<title>MY SITE</title>ます<a href="root">TITLE</a>。ね?

HOMEそれが問題である場合は、たとえば、ある種の定数を定義することをお勧めします

define ('HOME', dirname(__FILE__));
// or define ('HOME', __DIR__); depending on your PHP version

そのため、その定数に関連するすべてのものを含めることができます

require_once(HOME . '/includes/initialize.php');

それ以外は、コードにエラーはありません。

于 2013-02-19T19:28:06.297 に答える