简体   繁体   中英

How to redirect a user to a certain webpage base of a code they enter through php

When a user enters 1234p they will go to google.com, if 5678n they will go to yahoo.com and so on. Right now the code I have only worked for 1 page with 1234

<?php

session_start();
$redirect = true; 
$url_redirect = 'http://www.google.com'; 



$pass = "1234";
$msg;

if (isset($_POST['btn_go'])) {
    if ($_SESSION['count'] >= 3) {
        $msg['msg'] = "max_count";
    } else {
        $pwd = trim($_POST['pwd']);
        if ($pwd == $pass) {
            $_SESSION['count'] = 0;
            $_SESSION['user_auth'] = 1;
            $msg['msg'] = "ok";
            if ($redirect) {
                $msg['redirect'] = 1;
                $msg['url'] = $url_redirect;
            }
        } else {
            $_SESSION['count'] = $_SESSION['count'] + 1;
            $msg['msg'] = "wrong";
        }
    }

    echo json_encode($msg);

} else {
    echo "wrong";
}

?>

I would do this with a switch statement -- Assuming you know how to set the variable $page in this example .. :

<?php
session_start();
$redirect = true; 


if (isset($_POST['btn_go'])) {
    if ($_SESSION['count'] >= 3) {
        $msg['msg'] = "max_count";
    } else {
        $pwd = trim($_POST['pwd']);

        switch ($pwd) {
            case '1234p':
                $_SESSION['count'] = 0;
                $_SESSION['user_auth'] = 1;
                $msg['msg'] = "ok";
                if ($redirect) {
                    $msg['redirect'] = 1;
                    $msg['url'] = 'http://google.com';
                }
                break;
            case '5678n':
                $_SESSION['count'] = 0;
                $_SESSION['user_auth'] = 1;
                $msg['msg'] = "ok";
                if ($redirect) {
                    $msg['redirect'] = 1;
                    $msg['url'] = 'http://yahoo.com';
                }
                break;

            default:
                $_SESSION['count'] = $_SESSION['count'] + 1;
                $msg['msg'] = "wrong";
        }

    }

    echo json_encode($msg);

} else {
    echo "wrong";
}
?>

Your question only mentions php so I am assuming you're getting your input from a $_POST or $_GET .

UPDATE I have Edited my answer to better fit your code .. I still think that a switch statement is best suited for the output you are attempting.

Some code refacto but I think your logic is here.

Don't hesitate to ask if something isn't clear

<?php

session_start(); 

// Define an array with pass as key and the value is the url to redirect
$passToUrl = [
    '1234p' => 'http://www.google.com',
    '5678n' => 'http://yahoo.com'
];

if (isset($_POST['btn_go'])) {
    if ($_SESSION['count'] >= 3) {
        $msg['msg'] = "max_count";
    } else if(array_key_exists('pwd', $_POST)) { // Test if the user submitted a pass before using it
        $pwd = trim($_POST['pwd']);
        // The pass is defined in the array 
        if (array_key_exists($pwd, $passToUrl)) {
            $_SESSION['count'] = 0;
            $_SESSION['user_auth'] = 1;
            // let's redirect to the good url
            header('Location: ' . $passToUrl[$pwd]);
            exit();
        } else {
            $_SESSION['count'] = $_SESSION['count'] + 1;
            $msg['msg'] = "wrong";
        }
    }

    echo json_encode($msg);

} else {
    echo json_encode(["msg" => "wrong"]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM