Php session error to logout
My login works as it redirects me to index.php.
But after I click the login button, it goes to index.php and the session is not logged in. It should say "Hello $ username", but it still says "Log in again" instead of "Log out".
index.php code:
<?php
session_start();
if( !empty($_SESSION) && isset($_SESSION['username'])){
echo 'Olรก ' . $_SESSION['username'];
echo '<a href="logout.php"><br/>Logout</a>';
}
else{
echo '<a href="login.php" class="hiper">Fazer Login</a>';
echo '<a href="criarconta.php" class="hiper"><br/>Criar Conta</a><br/>';
}
?>
login.php code:
<?php
if(!empty($_POST)){
$username=$_POST['username'];
$password=$_POST['password'];
require_once 'Validate.php';
$flag_error = false;
$errors = array ('username' => array (false,'username incorrecto.'),'password' => array (false,'Password tem de conter pelo menos 8 caracteres.'));
/*if(!checkusername($username)){
$errors['username'][0] = true;
$flag_error=true;
}
if(!Valid_Pass($password)){
$errors['password'][0]=true;
$flag_error=true;
}*/
if(!$flag_error){
require_once 'ligacao.php';
$query = "SELECT * FROM utilizadores` WHERE 'username' = '$username' AND 'password' = '$password'";
$verificar=mysql_query($query) or die (mysql_error());
if (mysql_num_rows($verificar)==true){
$_SESSION['username'] = $username;
header('Location: index.php');
}else{
echo '<font color="red"> Esta conta nรฃo existe. </font></a>';
}
}
}
?>
I know the login works because it redirects to "header" ("Location: index.php"); ", but the session remains active. What could it be?
+3
source to share
2 answers
session_start();
This will start a session and you have to use it in every file you need to login to login to it.
$_SESSION['color']='red';
$_SESSION['size']='small';
$_SESSION['shape']='round';
This sets the variables in the session
session_unset();
Remove all session variables
session_destroy();
Destroy the session
0
source to share