Hi friend, I think you're doing things a little bit wrong by running session_start (); without to see decided anything, I give you an example so that you adapt it to your needs, I think you should update PDO since mysql is obsolete
form to start the session
<form action="validar.php" name="frmContacto" method="post">
<h6 class="left-align white-text">Usuario</h6>
<input id='usuario' class="white-text" type="text" name="usuario" />
<h6 class="left-align white-text">Contraseña</h6>
<input id='clave' class="white-text" type="password" name="clave"
onkeyup="if(event.keyCode == 13) accountLogin($(this).parents('form'));" />
<div class="center-align">
<div class="row">
<div class="col s12">
<button class="waves-effect waves-light btn-large transparent"
type="submit" name="guardar">Aceptar
</button>
<button class="waves-effect waves-light btn-large transparent"
type="reset">Cancelar
</button>
<a href='index.php'
button class='btn waves-effect transparent btn-large' type='submit'>
Inicio
</button></a>
</form>
file that validates the session
<?php
include("conexion/conexion.php");
// Preparamos la consulta y la ejecutamos
$sql = $DB_con->prepare('SELECT * FROM usuario WHERE usuario = :usuario AND
clave = :clave LIMIT 1');
$sql->bindParam(':usuario', $_POST['usuario']);
$sql->bindParam(':clave', $_POST['clave']);
$sql->execute();
// Comprobamos si ha devuelto algun registro
if($sql->rowCount()>0){
// Iniciamos sesion
session_start();
// Recuperamos los datos de la consulta
$row = $sql->fetch(PDO::FETCH_ASSOC);
//definimos las variables necesarias para la sesion
$_SESSION['id'] = $row['id'];
$_SESSION['usuario'] = $row['usuario'];
$_SESSION['clave'] = $row['clave'];
$_SESSION['fk_nivel_usuario'] = $row['fk_nivel_usuario'];
//datos personales
$_SESSION['cedula'] = $row['cedula'];
$_SESSION['nombres'] = $row['nombres'];
$_SESSION['apellidos'] = $row['apellidos'];
$_SESSION['email'] = $row['email'];
$_SESSION['telefono'] = $row['telefono'];
$_SESSION['direccion'] = $row['direccion'];
// Redirigimos la pagina segun idnivel_usuario
switch ($_SESSION['fk_nivel_usuario']) {
case 'Administrador':
header("Location: admin/home.php");
exit;
break;
case 'Secretarias':
header("Location: secretarias/home.php");
exit;
break;
// Definimos que hacemos en caso de no detectar ningun idnivel_usuario
valido
default:
header("refresh:5;iniciar.php");
?>
<div class='col s12 card-panel blue lighten-2 center'>
<h5 class='black-text text-darken-2 center CONDENSED LIGHT5'>¡ No se
pudo determinar el nivel del usuario !</h5>
</div>
<?php
session_destroy();
exit;
break;
}
}
else{
header("refresh:5;iniciar.php");
?>
<div class='col s12 card-panel blue lighten-2 center'>
<h6 class='black-text text-darken-2 center CONDENSED LIGHT5'>¡ Ups Aviso:
Contraseña o usuario incorrectos !
</h6>
</div>
<?php
}
$sql = null;
$DB_con = null;
?>
the session
<!-- la session-->
<?php
session_start();
if(!isset($_SESSION['fk_nivel_usuario']))
{
header('Location: ../index.php?Error=Acceso denegado');
echo "<script>alert('Disculpe Acceso registringido, Usuario no
Autorizado')</script>";
exit();
}
?>
<!-- fin de la session-->
this is a good example in PDO what you would need is to adapt them to your needs, but as I told you before you should thicken to work with PDO, luck ...