Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't store $_SESSION variables
    text
    copied!<p>I develop this set of 2 scripts in order to use $_SESSION variables to check if the user is logged in. I didn't use cookies because I didn't want to get to that "jazz" yet.</p> <p>Here is the login.php</p> <pre><code>require 'host.php'; // Connection to database $message = ''; if (isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; if ((!$username) || (!$password)) { $message ='Preencha todos os campos.'; } else{ $username = preg_replace("#[^0-9a-z]#i","",$username); $password = sha1($password); $sql = "SELECT * FROM utilizadores WHERE nome_utilizador='$username' AND palavra_chave='$password' LIMIT 1"; $query = mysql_query($sql) or die(); if (mysql_num_rows($query) == 1) { // Logged in stuff session_start(); $_SESSION['username'] = $username; $_SESSION['password'] = $password; while ($consulta = mysql_fetch_array($query)) { $id = $consulta['id_utilizador']; // ID of username. } $_SESSION['id'] = $id; $message = '&lt;a href="calendario.php"&gt;Clique aqui para ir para o calendário.&lt;/a&gt;'; // Message that is shown to redirect to another page. } else{ $message = 'Os dados que inseriu estão incorrectos!'; // Data inserted is incorrect. } } } </code></pre> <p>And here is the file that checks if the user is logged in</p> <pre><code>session_start(); include 'host.php'; if (isset($_SESSION['id'])) { $id = $_SESSION['id']; $password = $_SESSION['password']; //Verificar se o user existe. $sql = "SELECT * FROM utilizadores WHERE id_utilizador='$id' AND palavra_chave='$password' LIMIT 1"; $query = mysql_query($sql) or die('Could not connect.'); if (mysql_num_rows($query) &gt; 0) { header('Location: logout.php'); } else{ // Logged in stuff $logged = 1; } } else{ $logged = 0; } </code></pre> <p>The want i would do to check the session is include the file in every page and check is $logged == 0.</p> <p>But it isn't storing session data from page to page.</p> <p>Hope you can help me!</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload