[英]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. 当用户输入1234p时,他们将转到google.com;如果输入5678n,则他们将转到yahoo.com,依此类推。 Right now the code I have only worked for 1 page with 1234
现在我只在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 .. : 我将使用
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";
}
?>
Your question only mentions php
so I am assuming you're getting your input from a $_POST
or $_GET
. 您的问题仅提到
php
所以我假设您是从$_POST
或$_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. 更新我已经编辑了答案,以更好地适合您的代码..我仍然认为
switch
语句最适合您尝试的输出。
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"]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.