0

I am new to php coding and I hope you can help a java-boy out of the trouble.

I try to include navigation menu and content dynamicly in the "index.php". If Í test the code below, I only get the same index.php again, without changing the navigation menu, which should happen in my opinion, but maybe I don't know php good enough.

My index.php :

<?php
    //^ E_NOTICE ^ E_WARNING
    error_reporting(E_ALL);    
    if (!defined("ABS_PATH"))define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);

    if (!isset($_SESSION['login'])) {
        session_start();
        $_SESSION['login'] = 1;
        $_SESSION['role'] = "guest";
        $_SESSION['navi'] = "/view/navigation_guest.php";
        $_SESSION['content'] = "/view/content_login.php";
        $_SESSION['calendar'] = "/view/calendar.php";
    }
?>

<!DOCTYPE html>
<html>
    <head>        
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Klausurplaner</title>

        <link href="view/design.css" type="text/css" rel="stylesheet">

    </head>

    <body>
        <div id="frame">
            <div id="header">
                <?php
                    include_once "/view/header.php";
                ?>
            </div>

            <div id="main">
                <div id="navi">
                    <ul>
                        <?php
                            include_once $_SESSION['navi'];
                        ?>
                    </ul>
                </div>

                <div id="content">
                    <?php
                        include_once $_SESSION['content'];
                    ?>
                </div>

                <div id="calendar">
                    <?php
                        include_once $_SESSION['calendar'];
                    ?>
                </div>
            </div>
        </div>     
    </body>

</html>

If I try to modify the index.php with the following content, as stated above, I only get the same website again, without having the navigation menu changed.

The content_login.php :

<?php
    if (!defined("ABS_PATH"))define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);
    include_once(ABS_PATH . "/Klausurplaner/Control/LoginControl.php");
?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

        <link href="design.css" type="text/css" rel="stylesheet">  

    </head>

    <body>

        <h1>Bitte loggen Sie sich ein.</h1>

        <form method="post" action="control/btnLoginClicked.php">
            <p1>Benutzername:</p1>
            <br>
            <input id="edt_username" type="text" size="25%"/>
            <br>

            <p1>Passwort:</p1>
            <br>
            <input id="edt_password" type="password" size="25%"/>
            <br><br>
                <input name="btn_login" type="submit" value="login" size="5%"/>
        </form>
        <br><br>

           <!-- <br><br>
            <a href="content_guest_newPassword.php">Passwort vergessen</a> 
            <!-- E-Mail an die eigene E-Mail-Adresse mit neuem PW -->
    </body>

</html>

The btnLoginClicked.php :

<?php
    if (!defined("ABS_PATH"))define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);

/**
 * @author Falk Libor
 * @version 1.0
 * 
 * Date: 19.03.2013
 */

if (isset($_POST['btn_login'])) {
    $_SESSION['navi'] = "/view/navigation_admin.php";
    $path = ABS_PATH . "/Klausurplaner/index.php";
    header("Location: " . $path);
    exit();
}

?>

Edit: session_start() is now before the if-block, but somehow it now shows the "navigation_admin.php" instead of the defined "navigation_guest.php" before I clicked the "btn_login" in the "content_login.php". Any thoughts?

3
  • 3
    $_SESSION doesn't exist til after session_start(). Just so you know. :) Commented Mar 19, 2013 at 14:55
  • cHao is right, btw you need to put session_start() at the very beginning of your PHP code, before any output is made. Commented Mar 19, 2013 at 14:57
  • thanks all :) But some how its loading the admin navigation at start, not after I clicked the button to do so.. Commented Mar 19, 2013 at 16:38

2 Answers 2

1

The $_SESSION array doesn't exist until you use session_start(), so before you start it, you will never get any value from session and you cannot add to it either.

Sign up to request clarification or add additional context in comments.

Comments

0

Try calling session_start before

   session_start();
    if (!isset($_SESSION['login'])) {   
            $_SESSION['login'] = 1;
            $_SESSION['role'] = "guest";
            $_SESSION['navi'] = "/view/navigation_guest.php";
            $_SESSION['content'] = "/view/content_login.php";
            $_SESSION['calendar'] = "/view/calendar.php";
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.