Redirect system with PHP

Asked

Viewed 297 times

-2

Hello, how could I make a page redirect system with PHP? Being more specific, it would be the following, I have a test page with only 2 inputs, one to send and the other to insert a text. If it type a particular code correctly it is redirected to some page, otherwise an error message appears to it (but nothing like an Alert in the middle of the screen, I am more to make appear a small stylized box containing some text, for example "Nonexistent code"). Exemplifying further, let’s assume I have these 4 codes: teste1 teste2 teste3 teste4

They would be like passwords, if the user type the teste1 it is redirected to the página1.php if type the teste2 is redirected to the página2.php and so on..
But if he doesn’t type anything, there’s the one about the mistake I quoted.

  • 1

    Your question has been suspended by the community because information is missing to make it more specific, such as where the code list comes from (are they in an array?), and the part where you’re having problems (can you redirect with a fixed URL?). As it is, it is very wide, including involving aspects of user interface ("nothing like an Alert in the middle of the screen, I am more to make appear a small stylized box containing some text"). I suggest editing the question to make it as objective as possible, and the community can decide on the reopening.

1 answer

2

You can do something like in the example below:

<?php
var $campo = $_POST["campo"];
switch($campo) {
    case "teste1":
        header("Location: teste1.php");
    // mais casos
    case "testeN":
        header("Location: testeN.php");
    default: 
        echo "<div class='box-estilizada-erro'>Código inválido</div>";
}

However if they are codes that are registered dynamically, or if there are many, I recommend to make a table in the database and check there if there is this code, then if there is, make the redirect, if not, display the message.

Browser other questions tagged

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