Skip to content

Redirecting users to different pages after login

i have this website and i want to redirect users to 3 different pages, i have created 2 sign up pages that has a hidden field called role with different values on each of the sign up pages, one being “corporate”..So i used the script below to redirect the user based on the account type he or she creates. Is there a way to add another function to the code i have below that will redirect the user to a 3rd page making it 3 in all? Please any help offered is deeply appreciated

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

Advertisement

Answer

Try to change the code like this:

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

or better using DRY rule: 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>";
User contributions licensed under: CC BY-SA
10 People found this is helpful