PHP会话开始不工作

PHP会话开始不工作,php,html,Php,Html,未登录时,您可以在浏览器中键入此页面的URL,表单仍会显示。如果没有登录,我不希望HTML显示-只是说必须登录的消息。我在其他页面上使用了相同的会话代码,它也可以工作——但确实给出了“未定义索引”的通知,这有点令人恼火。有什么想法吗 <?php session_start(); if ($_SESSION['first_name']&& $_SESSION['username']) echo "Welcome ".$_SESSION['first_name']."&

未登录时,您可以在浏览器中键入此页面的URL,表单仍会显示。如果没有登录,我不希望HTML显示-只是说必须登录的消息。我在其他页面上使用了相同的会话代码,它也可以工作——但确实给出了“未定义索引”的通知,这有点令人恼火。有什么想法吗

<?php

session_start();

if  ($_SESSION['first_name']&& $_SESSION['username'])

echo "Welcome ".$_SESSION['first_name']."<br><a href='login/logged_out.php'>log    
out</a>";

else
die("You must be logged in. Click <a href='login/login_page.php'>here</a> to log    
in.");

?>
<html>
<head>
</head>
<body>
<form id="1" class="rounded" action="test4.php" method="post"/>
<input type="submit" name="submit"  class="button" value="5" />

<form id="2" class="rounded" action="test4.php" method="post"/>
<input type="submit" name="submit"  class="button" value="6" />

<form id="3" class="rounded" action="test4.php" method="post"/>
<input type="submit" name="submit"  class="button" value="7" />

<form id="4" class="rounded" action="test4.php" method="post"/>
<input type="submit" name="submit"  class="button" value="8" />

<form id="5" class="rounded" action="test4.php" method="post"/>
<input type="submit" name="submit"  class="button" value="9" />

<form id="6" class="rounded" action="test4.php" method="post"/>
<input type="submit" name="submit"  class="button" value="10" />

</form>
</body>
</html>

它不认为名字和/或用户名是会话中的索引

检查是否设置了first_name和/或username

isset($_SESSION['first_name'])

isset($_SESSION['username'])
此外,您可能不希望基于这些值验证布尔条件,除非它们本身是布尔值(尽管first_name的读取方式与布尔值不同)。

尝试以下操作:

<?php

session_start();

if(isset($_SESSION['first_name']) && isset($_SESSION['username']) && $_SESSION['firstname']!= ''){
    echo "Welcome ".$_SESSION['first_name']."<br><a href='login/logged_out.php'>logout</a>";
}else{
    die("You must be logged in. Click <a href='login/login_page.php'>here</a> to log in.");
}
?>

您的代码编写得很糟糕。我将修改为:

<?php
session_start();
if (empty($_SESSION['first_name']) || empty($_SESSION['username'])) {
    die("You must be logged in. Click <a href='login/login_page.php'>here</a> to log in.");
}
echo "Welcome " . $_SESSION['first_name'];
?>
<br><a href='login/logged_out.php'>logout</a>



除非知道数组索引存在,否则不应引用它们。

我不确定“未启动”问题是什么。可以通过检查来防止未定义的索引。您的代码还可以通过在if/else部分使用变量名和带花括号的括号来改进:

session_start();
$isLoggedIn = isset($_SESSION['first_name']) && isset($_SESSION['username']);
if ($isLoggedIn)
{
    echo "Welcome ", htmlspecialchars($_SESSION['first_name']), 
         "<br><a href='login/logged_out.php'>log out</a>";

}
else
{
    echo "You must be logged in. Click <a href='login/login_page.php'>here</a> to log in.";
    return;
}
session_start();
$isLoggedIn=isset($_会话['first_name'])和&isset($_会话['username']);
如果($isLoggedIn)
{
echo“Welcome”,htmlspecialchars($_SESSION['first_name']),
“
”; } 其他的 { echo“您必须登录。单击以登录。”; 返回; }

此变体还使用
return
而不是
die
,将程序流返回到更高的实例。

在处理会话时,您可以添加一些内容

print_r($_SESSION);

查看您是否已设置/取消设置该会话。

您确定已“登录”吗?人们在处理会话时最常见的错误之一是在调用session_write_close()之前重定向。可能重复感谢您的帮助。只是不使用isset()。再次感谢