2013-01-25 38 views
1

我用PHP登錄系統從http://tutorialzine.com/2009/10/cool-login-system-php-jquery/工作檢查會話狀態只給你一個快速的概述,相信教程建立以下列方式變量:PHP:在二級頁面

<?php 
define('INCLUDE_CHECK',true); 
require 'connect.php'; 
require 'functions.php'; 
// Those two files can be included only if INCLUDE_CHECK is defined 

session_name('tzLogin'); 
// Starting the session 

session_set_cookie_params(1*7*24*60*60); 
// Making the cookie live for 1 weeks 

session_start(); 
if($_SESSION['id'] && !isset($_COOKIE['tzRemember']) && !$_SESSION['rememberMe']) 

.......... 

所以除了我無法將會話變量從「主登錄」頁面轉移到後續頁面(其中包含受限制的內容)之外,這一點非常好。下面是我打算在每一個受限制的內容頁面

<?php 
session_name('tzLogin'); 
session_set_cookie_params(1*7*24*60*60); 
session_start(); 
    if($_SESSION['id']) <-- I believe I need more code here (incldue the cookie) 
{ 
//If all is well, I want the script to proceed and display the HTML content below. 
} 
else 
{ 
header("Location: MainLogin.html"); 
or die; 
//redirects user to the main login page. 
} 
?> 

正如你所看到的起點放置的基本代碼,我是一個新手總量,但任何幫助將不勝感激。截至目前,即使我已正確登錄,我的受限內容頁面也會繼續重定向到主頁。因此,我懷疑SESSION狀態未被結轉。再次感謝!

+0

@YogeshSuthar在開始會話後,您不能更改cookie參數或會話名稱(除非您銷燬並重新啓動它) – dethtron5000

回答

0

你或許應該確保你當你調用session_set_cookie_params設置路徑和域:(這是設置HTTPOnly屬性,以及一個好主意)

session_set_cookie_params (1*7*24*60*60, '/','.yourdomain.com') 

http://php.net/manual/en/function.session-set-cookie-params.php

另外,請確保您實際上爲您的會話ID密鑰指定了一些值(您在代碼示例中並不清楚):

$_SESSION['id'] = 'some value'; 

最後,您可能希望在調試時使用session_status()以驗證您是否已正確啓動會話(http://php.net/manual/en/function.session-status.php)。