0

The index.php is the login page of my website, after logging in successfully. My index.php page doesn't redirect to the Dashboard.php. I have to change it manually from URL. Or i have to login again, then it works correctly. This issue occurred after deploying my website to the server. it is working fine locally. I have used php headers to redirect: login code:

        if($result)
        {
            if (mysqli_num_rows($result) > 0) {
                while ($rows = mysqli_fetch_assoc($result)) {
                    //creating session
                    session_start();
                    $_SESSION['Id'] = $rows['user_Id'];
                    $_SESSION['Name'] = $rows['Name'];
                    header("location: Dashboard.php");
                }
            } 

After Successful login: my page redirects to the index.php again. as i mentioned above. I logged in successfully because i can access other pages by url.
here is start my index page:

<?php include_once "includes/Functions.php" ?>
<?php
session_start();
if(isset($_SESSION['Name']))
{
    ob_start();
    header("Location: Dashboard.php");
} 
?>
  • This might have answers: [How do I make a redirect in PHP?](https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php) - I would suggest redirecting with status code 302 for logins, because you have different session values/cookies set for every user. – Peter Krebs Nov 03 '21 at 15:40
  • And what exactly is not working? What have you tried to resolve the problem? – Nico Haase Nov 03 '21 at 16:28

1 Answers1

0

The session_start and header functions must be called before any output has been sent, even blanks, newlines and other kind of characters. In your code you have a new line before the <?php tag. Try to put everything in the same code block, or concatenate code blocks without newlines:

<?php include_once "includes/Functions.php" ?><?php
session_start();
...

Also, the ob_start call is very strange there, do you have it there for some reason? Maybe you are getting a warning saying the header function shouldn't be called after any output and you put it there to hide that helpful message? :)

Daniels118
  • 1,149
  • 1
  • 8
  • 17
  • Thankyou for helping me out, but i found my error after reading the code thoroughly. I'm starting the session two times, one in my login function and the other one is in my index file. which works fine locally i don't know how. but it was causing issues after deploying it to the server. And yes, i used ob_start() to hide that warning. – Rida Fatima Nov 05 '21 at 15:07
  • The reason it fails on the server could be the warning level on the server is higher than your local environment, and this cause an output before the call to the header function. As a general rule you should increase the warning level in the dev/test environments and solve all errors, not just hide them. In production env I prefer to turn the warnings into errors to prevent unexpected behaviours. – Daniels118 Nov 05 '21 at 22:54