How to redirect php pages

Asked

Viewed 40,467 times

3

I have a login system in a program PHP that I want it to forward users to different pages depending on their profile. There is a login table that has a field called "perfil" who is either 0 or the 1. The goal is to route users depending on their 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 stays on the login page when trying to do this check

  • 1

    Possible duplicate => http://answall.com/questions/35092/howto create a system-of-control-of-permiss%C3%B5es

  • If you are an echo in $user what it returns?

  • cannot check because of Session, but doing the query in mysql returns 1

  • 1

    The fetch. You’re comparing an object, so don’t enter the if

  • but even with fetch it always goes back to the login page. I already entered fetch and still does not enter if

  • Update the issue with the use of fetch, show how it turned out.

  • with fetch gets this way, the problem is that when I enter user it always goes back to login page

  • Give a var_dump( $result ) and say what comes up.

  • Hey, I have a login system very similar to yours in this question, if you want to use, will: https://answall.com/questions/241731/login-com-system-validate%C3%A7%C3%A3o

Show 4 more comments

5 answers

4

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

<?php
header('Location: /paginadestino.php');
?>

4

Missing use the fetch_assoc to recover the value of profile. The command $mysqli-> query( ... ) returns an object and not Row.

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

Now just use $result['perfil'] to check the profile type and redirect as the code memory you are using.

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

elseif($result['perfil'] == 1)
{
    header('Location:bbb.php');
}
  • Give a var_dump( $result ) and say what comes up.

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 to the loops.

I hope it helps. Any doubt post there.

  • thanks for your help. i am unable to redirect, do this query ( $user = $mysqli->query("SELECT profile from login Where username='$user_check'"); ) $rst = $user->fetch_array(MYSQLI_NUM); but then I can’t access it

  • You’re not getting your registered profile back then? If so, ask the question the information about the table to help build the query.

  • I already wrote the post with the page code to validate the type of user. The problem is that it does not leave the index page :/

2

Define where you need it. Just use.

function redirect($url)
{
    header(sprintf('Location: %s', $url));
    exit;
}
  • 1

    +1, I find more favorable to retweeting

-3

People need help... I created a program using Notepad++ and I 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>
  • 1

    My dear fellow, you used the answer field to ask a question. Search the page for a "Ask a Question" button and click on it to ask a new question. And use the tags to sort the theme.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.