我有这个网站,我想将用户重定向到3个不同的页面,我创建了2个注册页面,每个注册页面上都有一个名为role的隐藏字段,每个字段具有不同的值,其中一个是“ corporate”。因此,我使用以下脚本根据用户创建的帐户类型重定向用户。有没有一种方法可以在下面的代码中添加另一个功能,该功能会将用户重定向到第3页,从而使其总共变为3?请提供的任何帮助深表谢意
if($role === "Corporate"){
$_SESSION['username'] = $username;
echo $welcome = "<script type=\"text/javascript\">
swal({
title: \"Welcome $username!\",
text: \"You're being logged in.\",
type: 'success',
timer: 3000,
showConfirmButton: false });
setTimeout(function(){
window.location.href = '../Main/index.php';
}, 3000);
</script>";
}
else echo $welcome = "<script type=\"text/javascript\">
swal({
title: \"Welcome $username!\",
text: \"You're being logged in.\",
type: 'success',
timer: 3000,
showConfirmButton: false });
setTimeout(function(){
window.location.href = '../welcome/index.php';
}, 3000);
</script>";
}
答案 0 :(得分:0)
尝试这样更改代码:
if($role === "Corporate"){
$_SESSION['username'] = $username;
echo $welcome = "<script type=\"text/javascript\">
swal({
title: \"Welcome $username!\",
text: \"You're being logged in.\",
type: 'success',
timer: 3000,
showConfirmButton: false });
setTimeout(function(){
window.location.href = '../Main/index.php';
}, 3000);
</script>";
}
else if($role === "ROLE_NAME_HERE") {
$_SESSION['username'] = $username;
echo $welcome = "<script type=\"text/javascript\">
swal({
title: \"Welcome $username!\",
text: \"You're being logged in.\",
type: 'success',
timer: 3000,
showConfirmButton: false });
setTimeout(function(){
window.location.href = '../Main/NEW_INDEX.php';
}, 3000);
</script>";
}
else {
echo $welcome = "<script type=\"text/javascript\">
swal({
title: \"Welcome $username!\",
text: \"You're being logged in.\",
type: 'success',
timer: 3000,
showConfirmButton: false });
setTimeout(function(){
window.location.href = '../welcome/index.php';
}, 3000);
</script>";
}
或更好地使用DRY规则:https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
if ($role === "Corporate") {
$_SESSION['username'] = $username;
$redirectUri = '../Main/index.php';
} else if ($role === "ROLE_NAME_HERE") {
$_SESSION['username'] = $username;
$redirectUri = '../Main/NEW_INDEX.php';
} else {
$redirectUri = '../welcome/index.php';
}
echo $welcome = "<script type=\"text/javascript\">
swal({
title: \"Welcome $username!\",
text: \"You're being logged in.\",
type: 'success',
timer: 3000,
showConfirmButton: false });
setTimeout(function(){
window.location.href = '$redirectUri';
}, 3000);
</script>";
答案 1 :(得分:0)
我知道这个问题已经有了答案,但这是更简洁的代码和纯PHP替代方法。
$role
和$username
在if语句之前定义用于测试。可以在您的代码将其填充到其他地方时将其删除。
<?php
$role = "Corporate";
$username = 'Syfer';
if($role === "Corporate"){
$_SESSION['username'] = $username;
echo "Welcome $username! <br> You're being logged in.";
header( "refresh:3; url=../Main/index.php" );
}
else {
echo "Welcome $username! <br> You're being logged in.";
header( "refresh:3; url=../welcome/index.php" );
}
?>
重定向有效,并通过PHP 7.2进行了测试。