2013-07-23 98 views
0

有沒有一種方法爲index.php頁面的第一次加載設置一個值?爲index.php設置一個值頁面

我想index.php它加載像這樣「index.php?subject = 1」當它加載第一次。 ,因爲這個值會隨着他們在網站中移動而改變,我不希望它成爲一個固定值。

有人sugested

if(empty($_SESSION['visited'])) { 
    //DO STUFF 
    $_SESSION['visited'] = true; } 

我不能似乎得到的是與我的工作的功能。

find_selected_pa​​ge()

function find_selected_page() { 
    global $current_subject; 
    global $current_page; 
    if (isset($_GET["subject"])) { 
    $current_subject = find_subject_by_id ($_GET["subject"]); 
    $current_page = find_default_post($current_subject ["id"]); 
} elseif (isset($_GET["page"])) { 
    $current_subject = null; 
    $current_page = find_page_by_id ($_GET["page"]); 
} else { 
    $current_page = null; 
    $current_subject = null; 
} 
} 

的index.php

<?php 
    require_once($_SERVER['DOCUMENT_ROOT'].'/session/session.php'); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/db/dbcon.php'); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/inc/functions.php'); 
    $context = "public"; 
    find_selected_page(); 
?> 
<html> 
    <head> 
    <title>Home</title> 
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/style.php'); ?> 
<body> 
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/header.php'); ?> 
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/nav_ribbon.php');?> 
    <div id="p2dbg"> 
     <div id="p2dcontent"> 
      <div class="p2dcontent"> 
     <h1><?php echo htmlentities($current_subject ["menu_name"]); ?></h1><br /> 
       <?php if ($current_page) { ?> 
         <p><?php echo nl2br($current_page ["content"]); ?></p><br /> 
         <?php } else { ?> 
          This page does not exist! please slect a page from the menu. 
         <?php } ?>      
      </div> 
     </div> 
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php'); ?> 
    </div> 
</body> 
</html> 
+3

你有'session_start();'在那裏,對吧? –

+1

在代碼之前你使用過session_start會話會是最好的嗎? –

+0

在'if(empty($ _ SESSION ['visited'])之後設置'$ _SESSION ['subject'] = 1'){' – Fallen

回答

0

而不是設置一切空,如果你的價值不通過的,只是採用默認值:

// Set this to the value you want as your default 
define("DEFAULTSUBJECT",1); 
function find_selected_page() { 
    global $current_subject; 
    global $current_page; 
    if (isset($_GET["subject"])) { 
     $current_subject = find_subject_by_id ($_GET["subject"]); 
     $current_page = find_default_post($current_subject ["id"]); 
    } elseif (isset($_GET["page"])) { 
     $current_subject = null; 
     $current_page = find_page_by_id ($_GET["page"]); 
    } else { 
     $current_subject = find_subject_by_id (DEFAULTSUBJECT); 
     $current_page = find_default_post($current_subject ["id"]); 
    } 
} 
+0

給了這個嘗試,我得到了一個錯誤! (空($ _ SESSION ['visited'])){ // DO STUFF $ _SESSION ['visited'] = true; } ...所以它是一個開始的好地方。只需要找出錯誤並找出如何解決它。 –

相關問題