0

I've recently bought a vps and setup CPanel, I have php version 7.3 installed and I have a script which redirects who enters it to another website, but the problem is that if there is html code it won't work, I've tried googling on why it happens and finding a fix which keeps the html code there and it still redirects but I couldn't manage that's why I'm here.

The same script worked on my xampp server.

The fix I found is by removing all the html code but then there is no design.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Header Test</title>
</head>
<body>
    <div>Hi! this is a header test!</div>
</body>
<?php

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    header("Location: https://www.testing.com/");
?>
</html>
Keeron
  • 21
  • 2
  • 1
    you have html/output before setting header, that simply wont work, a HTTP redirect doesn't need styling, maybe you're after doing it in js `window.location = 'https://www.testing.com/'` – Lawrence Cherone Jul 31 '20 at 17:21
  • Adding to what Lawrence Cherone said (no output before "header" or it won't work), you can also make a redirection only in HTML via a meta tag like `` – Veve Jul 31 '20 at 17:49

1 Answers1

0

Headers cannot be sent after any output has occurred. You will need to place your header() code at the very top of the file, or below any other PHP before anything is placed for output.

Alternatively you can use ob_start() and ob_end_clean() to buffer the output, set your header and the provide the output.

Peter B
  • 437
  • 4
  • 14