简体   繁体   中英

$_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?

<?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(); at the beginning of your PHP file. 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

At line 39 md5$_POST['password']); you missed "("

md5($_POST['password']);

Every php file which has $_SESSION variable needs to include 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 ";"). Please, review your code. Hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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