-1

I'm trying to get a link in my menu to change once the user logged in. Meaning that "My Page" is displayed, rather than "Log In". Now, the user can log in and log out. Yet the name/link will not change directly after the user logged in.

My code for the menu: (I use include('menu.php' on all sites. For flexibility of the code)

<div id="menu">
<ul>
    <li><a href="index.php">Home</a></li>
    <li><?php echo $mypage?></li>
    <li><a href="community.html">Community</a></li>
    <li><a href="media.html">Media</a></li>
    <li><a href="contact.html">Contact</a></li>
</ul>

My code for the config.php:

<?php

session_start();

$mypage;

if(isset($_SESSION['name'])) {
$mypage = '<a href="mypage.php">My Page</a>';
}
else {
    $mypage = '<a href="login.php">Log In</a>';
}
?>

Only when the user clicks on another menu the $mypage variable will update. I've tried refreshing the page with a header() function. But this only lead into a redirection-loop.

It would be great if someone could help me out.

EDIT

To clear some things up:

For a better understanding of the website: http://www.askmephilosophy.camilstaps.nl/ (The only thing that can be viewed (so far) are the 'Welcome' and 'Log In'. After a login you will be redirected to a mypage.php site. (this can also be accessed by just typing it after the name. */mypage.php)

4

1 回答 1

1

因此,如果我理解:1/您登录 2/您已登录但名称保持“登录”3/您刷新页面然后菜单更改为“我的页面”

我认为您在执行此操作后设置了 $_SESSION['name']

if(isset($_SESSION['name'])) {
    $mypage = '<a href="mypage.php">My Page</a>';
}
else {
    $mypage = '<a href="login.php">Log In</a>';
}

尝试这样的事情:

if (isset ($_POST['name']) && isset ($_POST['password']) && ($_POST['password'] == $hash)) {
    $_SESSION['name'] = $_POST['name'];
}
if(isset($_SESSION['name'])) {
    $mypage = '<a href="mypage.php">My Page</a>';
} else {
    $mypage = '<a href="login.php">Log In</a>';
}
于 2013-04-18T10:50:22.030 回答