PHP 网站会话的 isset 函数无法正常工作
PHP isset function for website session not working correctly
所以我正在尝试为一个网站实现登录,我希望它能更改顶部的菜单栏。
但是,在使用 php 会话
时,我没有得到预期的结果
开头我用session_start
<?php
session_start();
?>
然后为了改变我使用的菜单栏
<?php
if (!isset($_SESSION['username'])){
?>
<ul>
<li><a href="Index.php">Home</a></li>
<li><a href="About.php">About</a></li>
<li><a onclick="document.getElementById('log').style.display='block'" style="width:auto;">Log In</a> </li>
</ul>
<?php
}else if (isset($_SESSION['username'])){
?>
<ul>
<li><a href="Index.php">Home</a></li>
<li><a href="About.php">About</a></li>
<li><a href="#logout">PLEASE LOG OUT</a></li>
</ul>
<?php
}
?>
我的日志模态框和脚本如下
<div id="log" class="modal">
<form class="modal-content animate" action="logindata.php" method="post">
<div class="container">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="usrname" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
</div>
<div class="container">
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</form>
</div>
<script>
// Get the modal
var modal = document.getElementById('log');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
我的 logindata.php 包含以下内容
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phpmysql";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user1 = $email1 = $pass1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user1 = test_input($_POST["usrname"]);
$pass1 = test_input($_POST["psw"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$sql = "SELECT username, password, email FROM users";
$result = $conn->query($sql);
$row = $result->fetch_array();
if ($row["username"]==$user1 && $row["password"]==$pass1) {
session_start();
$_SESSION["username"] = $row["username"];
//$_SESSION["email"] = $row["email"];
header("Location: Main_login_authentication.php");
} else {
header("Location: Denied.php");
}
$conn->close();
?>
我知道通过明文发送密码不是一个好策略。
session_start 需要在您的代码的第一行...我看到您在另一页上有这个但是,这个仍然是错误的;)否则,在 isset 之前做一个:
print_r($_SESSION);
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phpmysql";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user1 = $email1 = $pass1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user1 = test_input($_POST["usrname"]);
$pass1 = test_input($_POST["psw"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$sql = "SELECT username, password, email FROM users";
$result = $conn->query($sql);
$row = $result->fetch_array();
if ($row["username"]==$user1 && $row["password"]==$pass1) {
$_SESSION["username"] = $row["username"];
//$_SESSION["email"] = $row["email"];
header("Location: Main_login_authentication.php");
} else {
header("Location: Denied.php");
}
$conn->close();
?>
所以我正在尝试为一个网站实现登录,我希望它能更改顶部的菜单栏。 但是,在使用 php 会话
时,我没有得到预期的结果开头我用session_start
<?php
session_start();
?>
然后为了改变我使用的菜单栏
<?php
if (!isset($_SESSION['username'])){
?>
<ul>
<li><a href="Index.php">Home</a></li>
<li><a href="About.php">About</a></li>
<li><a onclick="document.getElementById('log').style.display='block'" style="width:auto;">Log In</a> </li>
</ul>
<?php
}else if (isset($_SESSION['username'])){
?>
<ul>
<li><a href="Index.php">Home</a></li>
<li><a href="About.php">About</a></li>
<li><a href="#logout">PLEASE LOG OUT</a></li>
</ul>
<?php
}
?>
我的日志模态框和脚本如下
<div id="log" class="modal">
<form class="modal-content animate" action="logindata.php" method="post">
<div class="container">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="usrname" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
</div>
<div class="container">
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</form>
</div>
<script>
// Get the modal
var modal = document.getElementById('log');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
我的 logindata.php 包含以下内容
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phpmysql";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user1 = $email1 = $pass1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user1 = test_input($_POST["usrname"]);
$pass1 = test_input($_POST["psw"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$sql = "SELECT username, password, email FROM users";
$result = $conn->query($sql);
$row = $result->fetch_array();
if ($row["username"]==$user1 && $row["password"]==$pass1) {
session_start();
$_SESSION["username"] = $row["username"];
//$_SESSION["email"] = $row["email"];
header("Location: Main_login_authentication.php");
} else {
header("Location: Denied.php");
}
$conn->close();
?>
我知道通过明文发送密码不是一个好策略。
session_start 需要在您的代码的第一行...我看到您在另一页上有这个但是,这个仍然是错误的;)否则,在 isset 之前做一个: print_r($_SESSION);
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phpmysql";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user1 = $email1 = $pass1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user1 = test_input($_POST["usrname"]);
$pass1 = test_input($_POST["psw"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$sql = "SELECT username, password, email FROM users";
$result = $conn->query($sql);
$row = $result->fetch_array();
if ($row["username"]==$user1 && $row["password"]==$pass1) {
$_SESSION["username"] = $row["username"];
//$_SESSION["email"] = $row["email"];
header("Location: Main_login_authentication.php");
} else {
header("Location: Denied.php");
}
$conn->close();
?>