0

有没有办法在第一次加载时为 index.php 页面设置一个值?

我希望 index.php 在第一次加载时像“index.php?subject=1”一样加载。因为这个值会随着他们在站点中移动而改变,所以我不希望它是一个固定值。

有人建议

 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;
}
}

索引.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>
4

1 回答 1

0

如果您的值未通过,则不要将所有内容设置为 null,只需假设默认值:

// 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"]);
    }
}
于 2013-07-23T19:30:16.253 回答