window.location.href redirects but is causing problems on the webpage

32 Views Asked by At

I'm struggling with sessions and need some help. The following is my processing.php file which starts the session and assigns session variables for a user. If the user is a participant, then they are assigned usertype='Participant' and upon login, they are redirected to a different webpage: participant.php.

<?php session_start(); ?>
<?php 
    include("connect.php");
    
    $email=$_POST['un'];
    $password=$_POST['pw'];
    
    $loginsql="SELECT * from Users WHERE email = '$email' AND password = '$password'";
    $result = $smeConn->query($loginsql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $_SESSION['id'] = $row['id'];
        $_SESSION['email'] = $row['email'];
        $_SESSION['first'] = $row['first'];
        
        $userID = $row['id'];
        $participantSql = "SELECT * FROM Participants WHERE user = $userID";
        $participantResult = $smeConn->query($participantSql);
    
        if ($participantResult->num_rows > 0) {
            $_SESSION['usertype'] = 'Participant';
        } else {
            $_SESSION['usertype'] = $row['usertype']; 
        }
        
        echo "success";
    } else {
        echo "error";
    }
    
?>

The problems come up in these lines of code on my navigate.php:

<?php
     if(isset($_SESSION['usertype']) && $_SESSION['usertype']=="Participant"){
         echo "<script>location.href='https://beroz.pwcswebdev.com/SMU_Part5/signMeUp04_v5-1-3/participant.php';</script>";
     }
?>

When I login as a user who is a participant, I am redirected ONLY AFTER refreshing the webpage. After being redirected to participant.php, the page just starts rapidly refreshing over and over again. I am only able to see SOME of the navbar content, and there is no "Welcome, user" or log out button (which works on index.php). In addition to this, nothing I do gets me back to index.php, even if I refresh or close the webpage. The only way to fix this and go back to index.php is by commenting out the echo .

If you want to test it, the webpage is live at this link: https://beroz.pwcswebdev.com/SMU_Part5/signMeUp04_v5-1-3/index.php

For the login,

email: [email protected]

password: 8roadw4Y

1

There are 1 best solutions below

0
b-rad90 On

The reason I was getting trouble is because I was redirecting through my nav.php, which is an include file containing only the navbar information. When I put the window.location.href within my index.php, I was able to redirect with no issues.