-1

I am working on a agency portal but a problem came with my headers, especially the first page when you are logging in normally you would get redirected to the dashboard.

When i run the code locally it works perfectly fine and redirects me to the dashboard but if i upload the code to the hosting and test the code i get this error:

Warning: Cannot modify header information - headers already sent by (output started at directoryname:11) in directoryname on line 75

This is the code that i'm running in the login.php file

if(password_verify($pass, $row["password"]))
{
  $_SESSION["valid"] = $user;
  $_SESSION['username'] = $row['username'];
  $_SESSION['userid'] = $row['userid'];
  echo '<div class="alert alert-success" role="alert">Logged in successfully</div>';
  header("refresh: 1; url=https://portal.habbofests.com/v1/index.php?page=dashboard");
  exit;
}
Devin
  • 1
  • 4
  • 1
    And, what was still unclear, after you researched the core phrasing of the error message? – CBroe Mar 16 '21 at 13:50
  • (Kinda nonsense to abuse `header` for a refresh to begin with. Create the appropriate HTML `meta` element that performs the same job instead, then you don’t run into this problem here in this instance in the first place.) – CBroe Mar 16 '21 at 13:51
  • i try researching but found inrelevant answers and i tried some solutions like changing my header to `header(Location: url/index.php?page=dashboard)` but still get that same error – Devin Mar 16 '21 at 13:52
  • 2
    _"When i run the code locally it works perfectly fine"_ - no, it works just the same. The difference is probably in the error reporting configuration, so your local server isn't showing you the warning (but it still happens). – El_Vanja Mar 16 '21 at 13:52
  • @El_Vanja or locally output buffering might be on. – CBroe Mar 16 '21 at 13:52
  • 1
    Does this answer your question? [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – El_Vanja Mar 16 '21 at 13:52
  • 1
    _“i try researching but found inrelevant answers”_ - how where they irrelevant? That you _should not_ create any output before using `header`, surely that was mentioned in what you found? – CBroe Mar 16 '21 at 13:53
  • @El_Vanja the strange thing is that local it redirects me but online it doesn't – Devin Mar 16 '21 at 13:55
  • @CBroe true, it was mentioned though but maybe i need to use something else for redirects than header – Devin Mar 16 '21 at 13:56
  • @Cbroe `Kinda nonsense to abuse header` well i'm using it everywhere it works fine by me but if you mean im abusing it what else can i use? except for meta because i need it more than one time in a script – Devin Mar 16 '21 at 13:57
  • 1
    _“except for meta because i need it more than one time in a script”_ - what does that have to do with anything? You are not going to need _multiple_ such redirects at the same time (because that would not make sense either) - so, create the appropriate meta element to refresh, there where you need it … and don’t create any other. – CBroe Mar 16 '21 at 14:02

1 Answers1

2

Do not use echo or print before Header redirections

Apart from this. you can use something like following in your scenario:

<?php

if(password_verify($pass, $row["password"]))
{
  $_SESSION["valid"] = $user;
  $_SESSION['username'] = $row['username'];
  $_SESSION['userid'] = $row['userid'];
  echo '<div class="alert alert-success" role="alert">Logged in successfully</div>';
   ?>
    <script>
    window.location.href = 'https://portal.habbofests.com/v1/index.php?page=dashboard';
    </script>
    </php } ?>
MSQ
  • 489
  • 5
  • 15