Home > Net >  Azure App Service WebApp PHP8.1 redirect via header()-method not working
Azure App Service WebApp PHP8.1 redirect via header()-method not working

Time:01-22

I wanted to host an App I am building for university in Azure, so that my friends could test it. The redirects I have (e.g. after creating an account being redirected to */login.php) are not working in the online version of it and I have absolutely no Idea why - they are working fine locally. These are the two methods being used after a user clicked "submit" on his registration and the inputs have been succesfully verified.

else {
    register_user($first_name, $last_name, $password, $mail_adress, $username, $gender,  $street, $house_number, $zip_code_city, $city, $cellphone, $con);
    header('location:login.php');
}

The entry into the database is working, but I am just left on a blank Page after that with the URL unchanged.

I am running the App on a F1 Linux App Service Plan (there is no option to use windows for PHP 8.1 for me) with PHP 8.1 as the runtime stack.

The App is just barebones PHP 8.1 with some css sprinkled on top. Just switching to a framework is unfortunately not an option, as the whole point is to demonstrate the understanding of some core programming concepts. I would have figured that the whole point of PaaS is to abstract this mumbo jumbo away from me, as I do not see a reason beside the OS change (which shouldnt impact this afaik) for this to not just work

My problem seems to be analog to this post: Php HTTP redirect not working on Azure App Service

But the OP just avoided fixing anything by switching to windows (which I cannot do) if I understand correctly.

//UPDATE:

My problems are now fixed through the implementation of the "ob_start()"-Method at the top of my code. With my current understanding of the matter I would have expected this to be needed for my local environment as well. I am very curious why the redirects worked locally in the first place.

Please ignore the obvious flaws ;)

<?php
ob_start();
include_once("db_connection.php");
include_once("registration_functions.php");

if(isset($_POST["submit"])){

$first_name = $_POST["vorname"];
$last_name = $_POST["nachname"];
$password = $_POST["passwort"];
$mail_adress = $_POST["email_adresse"];
$username = $_POST["benutzername"];
$gender = $_POST["geschlecht"];
$street = $_POST["strasse"];
$house_number = $_POST["hausnummer"];
$zip_code_city = $_POST["plz"];
$city = $_POST['ort'];
$cellphone = $_POST["handynummer"];


check_registration_status($first_name, $last_name, $password, 
$mail_adress, $username, $gender, $street, $house_number, 
$zip_code_city, $city, $cellphone);
}
?>

The redirect then happens here:

function check_registration_status ($first_name,$last_name, $password, $mail_adress, $username, $gender, $street, $house_number, $zip_code_city, $city, $cellphone){
$con = new DB();
$nameOrEmail = $con->getData("SELECT username, mail_adress FROM users WHERE username= '$username' OR mail_adress= '$mail_adress'");
$result = $nameOrEmail->fetch_assoc();

if(is_array($result)){
    echo "Dieser User/diese Emailadresse wird bereits verwendet";
}
else{
    register_user($first_name, $last_name, $password, $mail_adress, $username, $gender,  $street, $house_number, $zip_code_city, $city, $cellphone, $con);
    header('location:login.php');
    exit;

}

}

CodePudding user response:

  • I tried to deploy azure app service using PHP 8.1 and Linux in azure portal using Git-Hub actions.

  • I have used Header to redirect the page.

  • AFAIK, header will work only if it is on the top of the file.

  • If we click on the URL in app service it will redirect to Google search engine to redirect URL, I have followed below steps.

  • Look at the repository for more details Git-Hub Repository.

  • In Index.PHP I have used below code.

<?php
header('Location: https://www.google.com/');
exit;
?>
    <head>
        <title>Example</title>
    </head>
    <body>
        <p>Hello, world!</p>
    </body>
</html>

  • Created an azure app service in azure portal After creating app service -> GOTO deployment center and add GitHub login details -> Select Repository -> Select Branch Name as below.

enter image description here

  • Click on save that will create YML file in Git repository and workflow will be started. We can see it in GitHub Select the repository -> GOTO Actions as shown below

enter image description here

  • After successful Deployment GOTO azure portal and open the azure app service and click on URL as below

enter image description here

  • It will redirect to the page as below

enter image description here

  • Related