简体   繁体   English

$ _SESSION无法启动

[英]$_SESSION doesn't start

When user is logged in nothing is displayed on the screen(Error-reporting is on). 用户登录后,屏幕上不显示任何内容(错误报告已打开)。
It's like the $_SESSION is not true? 就像$_SESSION不正确吗?

<?php

 include_once('../includes/connection.php');


 if (isset($_SESSION['logged_in'])) {
 ?>
<html>
 <head>
 <title>wa</title>
 <link rel="stylesheet" type="text/css" href="../assets/stylesheet.css">
 </head>

 <body>
 <div class="container">
 <a href="index.php" id="logo">CMS</a>

 <br />
 <ol>

 <li><a href="add.php">Add Article</a></li>
 <li><a href="delete.php">Delete Article</a></li>
 <li><a href="logout.php">Logout</a></li>

</ol>       
</div>
</body>

</html>



<?php

 } else {

 if (isset($_POST['username'], $_POST['password'])) {
 $username = $_POST['username'];
 $password = md5($_POST['password']);


 if (empty($username) or empty($password)) {
 $error = 'All fields are required!';
 }else {

$query = $pdo->prepare("SELECT * FROM users WHERE user_name = ? AND user_password = 
         ?");

$query->bindValue(1, $username);
$query->bindValue(2, $password);

$query->execute();
$num = $query->rowCount();

 if ($num == 1) {
 $_SESSION['logged_in'] = true;

 header('Location: index.php');
 exit();

} else{

 $error = 'Incorrect details!';
}
}


}

  ?>
<html>
<head>
 <title>Visuality dashboard</title>
<link rel="stylesheet" type="text/css" href="../assets/stylesheet.css">
</head>

<body>
<div class="container">
<a href="index.php" id="logo">CMS</a>

 <br /><br />

 <?php if (isset($error)) { ?>
 <small style="color:#aa0000;"><?php echo $error; ?>
 <br /><br />
<?php } ?>





<form action="index.php" method="post" autocomplete="off">
<input type="text" name="username" placeholder="något">
<input type="password" name="password" placeholder="något">
<input type="submit" value="Login" />



</div>
</body>

<footer>
</footer>
 </html>

<?php
}






?>

You need to call session_start(); 您需要调用session_start(); at the beginning of your PHP file. 在您的PHP文件的开头。 If you have multiple files then adding it once to your connection file will work for all of them. 如果您有多个文件,则将其添加到连接文件一次将适用于所有文件。

Read more on Sessions in PHP here 此处阅读有关PHP会话的更多信息

At line 39 md5$_POST['password']); 第39行md5$_POST['password']); you missed "(" 你错过了 ”(”

md5($_POST['password']);

Every php file which has $_SESSION variable needs to include session_start(); 每个具有$ _SESSION变量的php文件都需要包含session_start(); on the top of the page. 在页面顶部。

When page gets a white screen after loading, you should see php_error_log for possible syntax errors (could be missing ";"). 当页面在加载后显示白屏时,您应该看到php_error_log以获取可能的语法错误(可能缺少“;”)。 Please, review your code. 请检查您的代码。 Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM