How to redirect php pages

3

I have a login system in a PHP program that I want it to forwards users to different pages depending on your profile. There is a login table that has a field named " perfil " which is either 0 or 1 . The goal is to route users depending on your profile to different pages. How can I do it in a simple way?

<?php
    include ("incs/ligacao.inc.php");
    include('session.php');
    $user_check=$_SESSION['login_user'];
    session_start();

        $utilizador = $mysqli->query("SELECT perfil from login where username='$user_check'");
        $result = $utilizador->fetch_assoc();

        if($result['perfil'] == 0){
      header('Location: menu.php');
        }


    elseif($result['perfil'] == 1){
        header('Location:menu.php');
    }



?>

Always stay on the login page when trying to do this

    
asked by anonymous 16.06.2016 / 13:35

5 answers

3

You can easily use the "Location" header syntax, something like this:

<?php
header('Location: /paginadestino.php');
?>
    
16.06.2016 / 13:44
3

The% w /% is missing to retrieve the profile value. The fetch_assoc command returns an object, not a row.

$result = $mysqli->query("SELECT 'perfil' from login where username='$user_check'");
$result = $result->fetch_assoc();

Now just use $mysqli-> query( ... ) to check the profile type and make the redirect as the code you are using.

if($result['perfil'] == 0)
{
    header('Location: aaa.php');
}

elseif($result['perfil'] == 1)
{
    header('Location:bbb.php');
}
    
16.06.2016 / 16:12
2

To redirect according to the profile you can do:

if($perfil == 0)
  header('Location: /pagina_perfil_0.php');
elseif($perfil == 1)
  header('Location: /pagina_perfil_1.php');

Or so:

switch ($perfil) {
    case 0: header('Location: /pagina_perfil_0.php');
      break;
    case 1: header('Location: /pagina_perfil_1.php');
      break;
}

Note: If you have more profiles just add adding to the loops.

Hope it helps. Any questions post there.

    
16.06.2016 / 13:50
2

Define where you need it. Just use.

function redirect($url)
{
    header(sprintf('Location: %s', $url));
    exit;
}
    
16.06.2016 / 13:53
-3

People need help ... I created a program using Notepad ++ and need help. I need to redirect to another page that should be displayed the person's registration data help plis

<body>
<h1>Bem vindo a rede social 3.0</h1>
<h2>Informe os seus dados para continuar</h2>
<p>&nbsp;</p>
<hr align="left" width="400" noshade="noshade" />
<form action="processar.php" method="post" id="frmLogin">
  <table width="400" border="0" cellspacing="1" cellpadding="0">
  <tr>


     <td width="60" align="right">Nome:</td>
      <td><label>
        <input type="text" name="txtNome" id="txtNome" />
        </label></td>
    </tr>
    <tr>
      <td align="right">Idade:</td>
      <td><label>
        <input type="text" name="txtIdade" id="txtIdade" />
        </label></td>
    </tr>
    <tr>
      <td width="60" align="right">Endereço:</td>
        <td><label>
    <input type="text" name="txtEndereço" id="txtEndereço" />
        </label></td>    
    </tr>
    <tr>

        </label></td>
        <td width="60" align="right">Salário:</td>
      <td><label>
        <input type="text" name="txtSalário" id="txtSalário" />
        </label></td>
        </tr>
    <tr>
            </label></td>
        <td width="60" align="right">Tempo de Serviço:</td>
      <td><label>
        <input type="text" name="txtTempoDeServiço" id="txtTempoDeServiço" />
        </label></td>
        </tr>
    <tr>
     </label></td>
        <td width="60" align="right">Registrado Em:</td>
      <td><label>
        <input type="text" name="txtRegistradoEm" id="txtRegistradoEm" />
        </label></td>
        </tr>
    <tr>
    <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="btnEnviar" id="btnEnviar" value="Entrar" />
        </label></td>
    </tr>
    
06.10.2017 / 18:44