read $ _session php in javascript

0

Hi, you know I'm doing a login and I do not know how to restrict access via url but I read some php sessions and I'm trying but I'm a bit lost and I have something like my login page that calls through an ajax request an intermediate file called call_login and this returns a value of true or false, I use it only to see if everything was correct and that call_login passes my username and password to read.php, which in this return is true or false, depending on whether I found that user or not. I know that I have to capture the user with whom I log in and that I do in the login after I return all good to read and that and I do so:

<script>   $('#log').on('click',function(e){
    e.preventDefault();
    var user = $("#user-login").val();

    var parametros = {
    'user'     : user, 
    'pass'     : $("#pass-login").val(),
                  }

    $.ajax({
      data: parametros,
      url: 'php/php-pdo/call_login.php',
      type: 'post',
      dataType: 'json',
      success:function(d){
        console.log(d);
        if (d == true) {
          console.log("Acceso concedido");
          // console.log(user);
          <?php $_SESSION['username'] = $user; ?> //Si se loguea correctamente creamos sesion con nombre de el usuario
          window.location.href = "index.php"; 
        } else {
          console.log("Acceso denegado");
        }
      },
      error:function(e){
        console.log(e);

      }


    })

  })

</script>

my question is how I capture the name of my user who initiates the session, this line is what generates the error $ _SESSION ['username'] = $ user; and the other after that let me give error what else do? : Or I know I should start something like the session again on the page that I'm restricting with session_start () and then verify that it is not empty but I'm not sure hehe

    
asked by Naoto 28.09.2017 в 22:03
source

1 answer

0

This line <?php $_SESSION['username'] = $user; ?> must place it in your php in the file that tells you if the user exists or not, it is clear that before the line in question you must place session_start(); and any file that you want to be accessible only for logueados users it must have it and you must compare with a condition, something like this:

File that makes the login logic (php):

session_start();
$_SESSION['username'] = $user;

Exclusive view file for logged in users (php):

<?php
    session_start();
    if(!isset($_SESSION['username'])){
        header('location: ubicacion.php');
    }
?>
<!DOCTYPE html>
<html lang="en">
..........

Another important point is that all the files that you want to restrict must have an extension .php

I hope I have been clear and I hope it will help you

    
answered by 28.09.2017 / 22:11
source