-1

My code tried to redirect the user based on the appname=x parameter in the url, which has now been removed as only one parameter is possible, i.e. the code works like this:

$appName="dev" //variable still set because it's used a lot

if($appName!=null){
    //write to log file
    if($appName="dev"){
        echo "<script>console.log('test successful');</script>";
        header('Location: '. 'https://dev.company.com'); //doesn't matter which address I put here
    else{
        echo "<script>console.log('not the dev');</script>";
    }
}

Interestingly, while I keep the line 'header()' commented, the echos log to the console just fine. As soon as any header('Location: ....) is in there, I'm getting a 500 Internal Server Error. This happens no matter to which page I set the function to go.

For security reasons I can't print the entire error, but it basically looks like this:

GET https://login.company.com/callback.php?param1=...

Where the individual params are highlighted.

What is even weirder is that the callback.php's first line is a console.log. In this case, this is never reached while the header() runs. I'm failing to see what I'm doing wrong here. Any ideas?

  • I am _almost_ sure, that the solution to this problem lies here: https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php/8028987#8028987 – Pavel Janicek Nov 27 '20 at 08:41
  • 1
    Error 500 often means a syntax error. You have one in your question, only not in the place you think you have it. – KIKO Software Nov 27 '20 at 08:42
  • 1
    becareful, this is an assignment, not testing for equality `if($appName="dev"){` and just like others have said, check the error log – Kevin Nov 27 '20 at 08:43
  • 1
    _“I'm failing to see what I'm doing wrong here.”_ - you came asking here about a 500 error, without checking what the server’s error log has to say first of all … – CBroe Nov 27 '20 at 08:43

1 Answers1

1

I don't want to be rude, but I think your code is messed up, you forgot a semicolon and a closing brace, could you please try the next code?

<?php
    $appName="dev"; //You forgot this semicolon

    if($appName!=null){
        if($appName=="dev"){ // You have to use == operator to compare, not =
            echo "<script>console.log('test successful');</script>";                                         
            header('Location: '. 'https://dev.company.com');
        }  // And also this closing brace                   
        else{
            echo "<script>console.log('not the dev');</script>";                                            
        }
    }

Also, if you are not familiarized with PHP, try using an IDE for autocomplete.

I should also add, that you don't need to print errors to clients in order to know what is happening, those errors are generally logged to a log file, if you don't have a log for your PHP error consider configuring logging options.

AngeLOL
  • 106
  • 2
  • 9