Does not redirect to index.php

0

I have a website in PHP with a login that once the login button is pressed, it does not redirect me to the index but it stays inside the login. I enclose the codes for index , login , registration_login

index.php

<?php require_once('config.php') ?>
<?php require_once('public_functions.php') ?>

<?php $posts = getPost(); ?>

<a href="logout.php">Logout</a>

<?php

if(!isset($_SESSION['username'])) {
  $_SESSION['msg'] = 'You must log in first';
  header('location: login.php');
}

if(isset($_GET['logout'])) {
  session_destroy();
  unset($_SESSION['username']);
  header('location: login.php');
}

?>

<a href="register.php">Register</a>

<h1>Hello</h1>

<?php foreach ($posts as $post): ?>
  <span><?php echo $post['id'] ?></span>
  <h2><?php echo $post['title'] ?></h2>
  <p><?php echo $post['body'] ?></p>
<?php endforeach ?>

login.php

    <?php include('config.php') ?>
<?php include('registration_login.php') ?>

<h1>Login Form</h1>


<form method="post" action="login.php">
  <?php include('errors.php') ?>

  <label>Username</label>
  <input type="text" name="username">

  <label>Password</label>
  <input type="password" name="password">

  <button type="submit" name="login_user">Login</button>

  <p>
    Not yet a member? <a href="register.php">Sign up</a>
  </p>

</form>

registration_login.php

<?php

session_start();

//inicializo variables, almacenaran datos
$username = "";
$email = "";
$password_1 = "";
$errors = array();

//conecto a la db
$conn = mysqli_connect('localhost', 'root', '', 'prueba');

//registro de usuarios
if(isset($_POST['reg_user'])) {
   //recibe todos los input values del form y los guarda en las variables declaradas
   $username = mysqli_real_escape_string($conn, $_POST['username']);
   $email = mysqli_real_escape_string($conn, $_POST['email']);
   $password_1 = mysqli_real_escape_string($conn, $_POST['password_1']);
   $password_2 = mysqli_real_escape_string($conn, $_POST['password_2']);

   //validacion del form: "SE ASEGURA QUE LA FORMA ESTE BIEN LLENADA"
   // por cada verificacion en caso de ser negativa se iran agregando los errores al final del array dentro de la variable errors.php!!!!!!!
   if(empty($username)) {
       array_push($errors, 'username is required');
   }
   if(empty($email)) {
       array_push($errors, 'email is required');
   }

   if(empty($password_1)) {
       array_push($errors, 'password is required');
   }

   if($password_1 != $password_2) {
       array_push($errors, 'passwords do not match, please check');
   }
}

//antes de enviar chequear con una consulta a la db que el usuario o email ingresado no exista

//creo una variable que guarde la informacion de la consulta
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($conn, $user_check_query);
$user = mysqli_fetch_assoc($result);

//advertencia mediantes sentencias if para la variable user
if($user) {
    //toma de los valores de la variable user el username en la bd
    if($user['username'] === $username) {
        array_push($errors, 'username already exists');
    }

    if($user['email'] === $email) {
        array_push($errors, 'email already exists');
    }
}

//si no hay ningun error, registra el usuario en la db insertando los datos guardados en las variables

//si los errores en la form == 0
if(count($errors) == 0) {
    $password = md5($password_1); //encripta la password antes de guardarla en la bd

    $query = "INSERT INTO users (username, email, password)
              VALUES ('$username', '$email', '$password')";

    mysqli_query($conn, $query);
    //variables superglobales de session, almacena datos del usuario para las sesiones
    $_SESSION['username'] = '$username';
    $_SESSION['success'] = 'you are logged in';
    //redirecciona
    header('location: index.php');
}

// Login user

//login de usuario
if(isset($_POST['login_user'])) {
    //recibe los datos del form y los los guarda 
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

    //validacion del form
    if(empty($username)) {
        array_push($errors, 'Username is required');
    }

    if(empty($password)){
        array_push($errors, 'Password is required');
    }

    //Si no hay errores, consulta en la bd y devuelve el valor con la informacion de log consultada
    if(count($errors) == 0){
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $results = mysqli_query($conn, $query);

        if(mysqli_num_rows($results) == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = 'You are logged in';
            header('location: index.php');
        } else {
            array_push($errors, 'Wrong username/password');
        }
    }

}

?>
    
asked by ricardo leiva sikic 19.11.2018 в 20:16
source

1 answer

0

You have to change the action of your form to point to index.php like this:

<?php include('config.php') ?>
<?php include('registration_login.php') ?>

<h1>Login Form</h1>


<form method="post" action="index.php">
  <?php include('errors.php') ?>

  <label>Username</label>
  <input type="text" name="username">

  <label>Password</label>
  <input type="password" name="password">

  <button type="submit" name="login_user">Login</button>

  <p>
    Not yet a member? <a href="register.php">Sign up</a>
  </p>

</form>
    
answered by 19.11.2018 в 20:24