Capture the id of the user logged in javascript and php

0

I would like to capture the id of my logged-in user, my goal is that every time an insert or update is generated, the username that executed the operation is captured.

I am new programming but I have been thinking about some solutions:

  • I have my file modelinventario.php and is thinking that I could capture the username in a variable and give it the value when updating or inserting a record.

    class ModeloInventario
    {
    public function obtenerUnidadxID($nombre)
    {
        $sql = "SELECT * FROM unidad WHERE desunidad = '$nombre'";
        $data   = ModeloConexion::ejecutar( $sql , DB_NAME , DB_SELECT );
    
        return $data['LISTA'][0]->idunidad;
    }
    
    public function actualizar($post)
    {
        pre($post);
        $obtenerTipoUnida   = $this->obtenerUnidadxID($post['m_idunidad']);
    
        $sql = "UPDATE inventario SET
            idusuario          =   '".$post['m_idusuario']."',
            idmarca            =   '".$post['m_idmarca']."',
            idtipoplanta       =   '".$post['m_idtipoplanta']."',
            modelo             =   '".$post['m_modelo']."',
            serie              =   '".$post['m_serie']."',
            ubicacion          =   '".$post['m_ubicacion']."',
            sala               =   '".$post['m_sala']."',
            idservicio         =   '".$post['m_idservicio']."',
            idcuadrofuerza     =   '".$post['m_idcuadrofuerza']."',
            idunidad           =   '".$obtenerTipoUnida."',
            capacidad          =   '".$post['m_capacidad']."',
            carga              =   '".$post['m_carga']."',
            factorpotencia     =   '".$post['m_factorpotencia']."',
            eficiencia         =   '".$post['m_eficiencia']."',
            aniofab            =   '".$post['m_aniofabricacion']."',
            anioinst           =   '".$post['m_anioinstalacion']."',
            idestado           =   '".$post['m_idestado']."',
            idsubestado        =   '".$post['m_idsubestado']."',
            observacion        =   '".$post['m_observacion']."'
            WHERE idinventario =   '".$post['m_idinventario']."'
            ";
        pre($sql);
        $data   = ModeloConexion::ejecutar( $sql , DB_NAME , DB_UPDATE );
    }
    
    ?>
    
  • The other thing would be to go to where I have my form: diagram.php. For this I must say that I am using the framework www.fancygrid.com and run in javascript

  • I have my session start:

    <?php
    session_start();
    ?>
    

    And I have a field of fancygrid hidden, the code is summarized:

    <script>
    type:'hidden',
    name: 'm_idusuario',
    id: 'm_idusuario',
    value: usuario
    </script>
    

    What you are thinking about is some function that captures the username in the user variable.

    Here is my code for accessing the system:     

    if(!empty($_POST)){
        if(isset($_POST["usuario"]) &&isset($_POST["password"])){
            if($_POST["usuario"]!=""&&$_POST["password"]!=""){
                //include "conexion.php";
                $obj = new ModeloAcceso();
    
                $user_id    = null;
                $fullname   = "";
                $usuario    = $_POST['usuario'];
                $password   = md5($_POST['password']);
    
                $retorno = $obj->validaAcceso($usuario, $password);
    
                if($retorno['TOTAL'] == 0){
                    print "<script>alert(\"Acceso invalido.\");window.location='../login.php';</script>";
                }else{
                    session_start();
                    $_SESSION["user_id"]    = $retorno['LISTA']->idusuario;
                    $_SESSION["fullname"]   = $retorno['LISTA']->nombres;
                    $_SESSION['start'] = time();
                    $_SESSION['expire'] = $_SESSION['start'] + (30 * 60) ;
                    //$usuario = $_SESSION['usuario'];
                    print "<script>window.location='../inventario.php?menu=inventario';</script>";
                }
            }
        }
    }
    
    ?>
    
        
    asked by juanpastortapara 09.03.2018 в 18:34
    source

    0 answers