如何将用户重定向到他们通过php输入的代码的特定网页库

时间:2018-09-11 22:27:11

标签: 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";
}

?>

2 个答案:

答案 0 :(得分:0)

我将使用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语句最适合您尝试的输出。

答案 1 :(得分:0)

一些代码已被引用,但我认为您的逻辑就在这里。

不要犹豫,问是否不清楚

NameError at /friendship/profile/
global name 'user' is not defined

\views.py in profile_view, line 51


def profile_view(request):
    p = Profile.objects.filter(user=user).first()
    u = p.user
    sent_friend_requests = FriendRequest.objects.filter(from_user=p.user)
    rec_friend_requests = FriendRequest.objects.filter(to_user=p.user)

    friends = p.friends.all()