How to destroy a session started in php

0

Which is the easiest way to destroy a Sesión , which even if they go back in the browser, does not allow them to enter after they have left.

This is the code I have to verify the session started:

<?php
    include("conexion.php");
    //Variables que recoge lo ingresado x el usuario
    $tab_usu = $_POST["for_nom"];
    $tab_cla = $_POST["for_cla"];
    //Verifica si los campo estan vacios y lo devuelve
    if(empty($tab_usu) || empty($tab_cla)){
        echo"<script>alert('Debe Llenar los campos vacios');window.location.href='index.php';</script>";
    }
    //Consulta Busqueda de un usuario
    $bus_usu = "select apodo_usuario,nombres,apellidos,clave,correo_usuario,correo_recuperacion from usuario where clave='".$tab_cla."' and apodo_usuario='".$tab_usu."' || correo_usuario='".$tab_usu."'";

    //Verifica si el usuario existe
    if($search = mysqli_query($conexion,$bus_usu)){
        $row = mysqli_num_rows($search);
        if($row >= 1){
            //Ciclo que recorre la variable de la consulta
            while($row = mysqli_fetch_assoc($search)){
                //Verifica si se loguea con su apodo_usuario
                if($row["apodo_usuario"] == $tab_usu and $row["clave"] == $tab_cla){
                    session_start();
                    $bus_usu = "select concat(nombres,apellidos) from usuario where clave='".$tab_cla."' and apodo_usuario='".$tab_usu."'";
                        $_SESSION["apodo_usuario"] = $tab_usu;
                    echo"<script>alert('Bienvenido $_SESSION[nombres]');window.location.href='index.html';</script>";
                }
                //Verifica si se loguea con su correo_usuario
                else if($row["correo_usuario"] == $tab_usu and $row["clave"] == $tab_cla){
                    session_start();
                    $_SESSION["correo_usuario"] = $tab_usu;
                    echo"<script>alert('Bienvenido $_SESSION[correo_usuario]');window.location.href='index.html';</script>";
                }
                //Verifica si al querer ingresar digita erroneamente usuario o contraseña
                else{

                }
            }   
        }
        else{
            echo"<script>alert('Error en los datos ingresados');window.location.href='index.php';</script>";
        }
    }   
?>
    
asked by Carlos Alfredo Rodriguez Silve 19.04.2017 в 20:49
source

6 answers

1

session_destroy()

It's just a matter of seeing how you will execute the function, and with that the user's session will cease to exist.

    
answered by 19.04.2017 в 20:50
0

For the case that, the tab is closed. You can do it with javascript destroy it if it's started, I'll give you an example:

  

In javascript

$( window ).unload(function() {
  $.post(Ruta donde esta el archivo con el método de borrado de la session);
});
  

In the php

        session_start();
        session_unset();
    
answered by 19.04.2017 в 21:14
0
  

First the function session_start() must be executed < strong> just one   once at the beginning of the whole of your PHP code.

Staying this way:

<?php
session_start(); //Aquí y solo una vez!
include("conexion.php");
    //Variables que recoge lo ingresado x el usuario
    $tab_usu = $_POST["for_nom"];
    $tab_cla = $_POST["for_cla"];
    //Verifica si los campo estan vacios y lo devuelve
    if(empty($tab_usu) || empty($tab_cla)){
        echo"<script>alert('Debe Llenar los campos vacios');window.location.href='index.php';</script>";
    }
    //Consulta Busqueda de un usuario
    $bus_usu = "select apodo_usuario,nombres,apellidos,clave,correo_usuario,correo_recuperacion from usuario where clave='".$tab_cla."' and apodo_usuario='".$tab_usu."' || correo_usuario='".$tab_usu."'";

    //Verifica si el usuario existe
    if($search = mysqli_query($conexion,$bus_usu)){
        $row = mysqli_num_rows($search);
        if($row >= 1){
            //Ciclo que recorre la variable de la consulta
            while($row = mysqli_fetch_assoc($search)){
                //Verifica si se loguea con su apodo_usuario
                if($row["apodo_usuario"] == $tab_usu and $row["clave"] == $tab_cla){

                    $bus_usu = "select concat(nombres,apellidos) from usuario where clave='".$tab_cla."' and apodo_usuario='".$tab_usu."'";
                        $_SESSION["apodo_usuario"] = $tab_usu;
                    echo"<script>alert('Bienvenido $_SESSION[nombres]');window.location.href='index.html';</script>";
                }
                //Verifica si se loguea con su correo_usuario
                else if($row["correo_usuario"] == $tab_usu and $row["clave"] == $tab_cla){

                    $_SESSION["correo_usuario"] = $tab_usu;
                    echo"<script>alert('Bienvenido $_SESSION[correo_usuario]');window.location.href='index.html';</script>";
                }
                //Verifica si al querer ingresar digita erroneamente usuario o contraseña
                else{

                }
            }   
        }
        else{
            echo"<script>alert('Error en los datos ingresados');window.location.href='index.php';</script>";
        }
    }   
?>
  

As you have been told in the comments, you should use    session_destroy() .

To make sure the session_destroy() function is running correctly, use this little PHP script ;

if (session_destroy()) {
    echo "Sesión destruida correctamente";
} else {
    echo "Error al destruir la sesión";
}

I hope it helps you

    
answered by 19.04.2017 в 21:43
0

The best thing you can do to destroy a php session is to start it and then destroy it, something like this:

session_start(); // este por si no la has iniciado en la pagina que planeas destruirla, de lo contrario no te destruirá nada
session_destroy();
session_unset();

with this the whole session should be controlled.

    
answered by 19.04.2017 в 22:25
0

The recommended thing to handle a session is the following step by step: We declare some constants that we will use

if (!defined('SES_COMPANY')) {
    define('SES_COMPANY', 'tusesion');
}

if (!defined('SES_TIMEOUT')){
    define('SES_TIMEOUT'        ,time() + 28800); # 8 Horas
}

Verify if it exists and establish the name:

if(!isset($_SESSION)) { session_name(SES_COMPANY); }

check if it exists again and start the session

if(!isset($_SESSION)) { session_start(); }

check if it exists and indicate the duration time

if(!isset($_SESSION['Ses_TimeOut'])){
    $_SESSION['Ses_TimeOut'] = SES_TIMEOUT;
}

Once built in this way your Session can destroy it in the following way: Verify if it exists and verify if the time expires.

function Ses_TimeOutDest(){
    if(isset($_SESSION['Ses_TimeOut'])){
        if ($_SESSION['Ses_TimeOut']  < time()) {
            session_destroy();
        }
    }
}
    
answered by 24.07.2017 в 16:30
0

You can use the PHP function session_destroy ()

This function destroys the session, if you need to know more about it, I will leave you the following links:

link
link
link

    
answered by 24.07.2017 в 16:14