簡體   English   中英

如何將用戶重定向到他們通過php輸入的代碼的特定網頁庫

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

當用戶輸入1234p時,他們將轉到google.com;如果輸入5678n,則他們將轉到yahoo.com,依此類推。 現在我只在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";
}

?>

我將使用switch語句執行此switch -假設您知道在此示例中如何設置變量$page ..:

<?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";
}
?>

您的問題僅提到php所以我假設您是從$_POST$_GET獲取輸入的。

更新我已經編輯了答案,以更好地適合您的代碼..我仍然認為switch語句最適合您嘗試的輸出。

一些代碼重新引用,但我認為您的邏輯就在這里。

不要猶豫,問是否不清楚

<?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"]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM