drtoclr046994545 2017-10-30 11:02
浏览 32
已采纳

登录php时显示导航栏中的注销链接

When user logged in, the login link on nav bar should be gone and logout link should appear how should i do it?

index.html:

<nav>
    <p class="menu"><a href="index.html">Home</a></p> 
    <p class="menu"><a href="Product1.html">Products</a> </p> 
    <p class="menu"><a href="login.html">Login</a></p> 
    <p class="menu"><a href="logout.php">Logout</a></p> 
    </nav>

Login.php file:

<?php
require 'db.php';
session_start();

$password = $mysqli->escape_string($_POST['Pass']);
$email = $mysqli->escape_string($_POST['EmailAdd']);
$result = $mysqli->query("SELECT * FROM Account WHERE Usermail='$email'");

//check email in db
 if ($result->num_rows == 0)
 {
 $_SESSION['message'] = "Email does not exist";

 print '<script type="text/javascript">alert("' . $_SESSION['message'] . 
 '");
</script>';
header("Location: ../register.html");
}
else
{
//get user array
$user = $result->fetch_assoc();

if ($password == $user['password'])
{
    $box = "Login successful";
    $_SESSION['email'] = $user['Usermail'];
    $_SESSION['logged_in'] = true;

echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Login Successful')
window.location.href='../index.html';
</SCRIPT>");

}
else
{
    $_SESSION['message'] = "Wrong password";

    header("Location: ../account.html");

    echo "failed";
    echo '<script language="javascript">';
    echo 'alert("Wrong password")';
    echo '</script>';
   }
 }
 ?>

I've gone through some of the post in stack overflow and apply things like if (!isset($_SESSION['email'])) and else statement on my index.php but its not working and i don't know what's the prob

Ps Previously was using index.php, since its not working so i change it back to index.html

  • 写回答

5条回答 默认 最新

  • drrvnbwle80177811 2017-10-30 11:09
    关注

    Assuming your code is correctly validating the credential and setting the auth state in the session $_SESSION['logged_in'] = true;

    You can do something like this:

    <nav>
        <p class="menu"><a href="index.html">Home</a></p> 
        <p class="menu"><a href="Product1.html">Products</a> </p> 
        <?php if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true): ?>
        <p class="menu"><a href="logout.php">Logout</a></p>
        <?php else: ?>
        <p class="menu"><a href="login.html">Login</a></p> 
        <?php endif; ?>
    </nav>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退