php - Can't store $_SESSION variables -
i develop set of 2 scripts in order utilize $_session variables check if user logged in. didn't utilize cookies because didn't want "jazz" yet.
here login.php
require 'host.php'; // connection 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 * utilizadores nome_utilizador='$username' , 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 = '<a href="calendario.php">clique aqui para ir para o calendário.</a>'; // message shown redirect page. } else{ $message = 'os dados que inseriu estão incorrectos!'; // info inserted incorrect. } } }
and here file checks if user logged in
session_start(); include 'host.php'; if (isset($_session['id'])) { $id = $_session['id']; $password = $_session['password']; //verificar se o user existe. $sql = "select * utilizadores id_utilizador='$id' , palavra_chave='$password' limit 1"; $query = mysql_query($sql) or die('could not connect.'); if (mysql_num_rows($query) > 0) { header('location: logout.php'); } else{ // logged in stuff $logged = 1; } } else{ $logged = 0; }
the want check session include file in every page , check $logged == 0.
but isn't storing session info page page.
hope can help me!
thanks!
your id variable within while loop , cant read outside while loop. thats why session storing nothing
try define id variable before while loop
$id = ""; while ($consulta = mysql_fetch_array($query)) { ......
you can seek if echo after while loop that
echo $id ;
php mysql session-variables
No comments:
Post a Comment