You can't do it that way. PHP header must sent out before any content, so the correct order is:

header("location: login6.php");
echo "Please Log In First";

But these codes will redirect instantly and wouldn't let you see the content. So I would do the redirection by JavaScript:

echo "Please Log In First";
echo "<script>setTimeout(\"location.href = 'http://www.example.com';\",1500);</script>";
Answer from Sunry on Stack Overflow
🌐
WebDeveloper.com
webdeveloper.com › community › 298129-how-to-display-message-before-redirect-to-other-page
How to display message before redirect to other page
October 7, 2015 - @NogDogJul 16.2014 — #[code=php] header("refresh:5;url=Location :login.php"); // really should be a fully qualified URI echo '<script type="text/javascript">alert("Please Enter Correct Details!");</script>'; [/code] Note that header() must be executed before [i]anything[/i] gets output to the browser. ... php mail function - diffferent subject based on the recipientsNewbie: Alert Box from Class Methods?File Uploads Not Working from Some Systems ... about: ({ version: 0.1.9 — BETA 11.12, social: @webDeveloperHQ, }); legal: ({ terms: of use, privacy: policy analytics: Fullres }); ↴changelog: ( version: 0.1.9, notes: added community page version: 0.1.8, notes: added Davinci•003 version: 0.1.7, notes: upvote answers to bounties version: 0.1.6, notes: article editor refresh )...
Discussions

Display thank you message and redirect to another page
Currently, I managed to create alert pop-up "Thanks" after on-click event. It works for 1 second and redirect to https://www.w3schools.com. How to create not pop-up alert, but a kind of More on stackoverflow.com
🌐 stackoverflow.com
asp.net mvc - How to display message before redirecting to another page? - Stack Overflow
I have a problem... I have registration form and when user has registred it redirect him to Home page..i want to display popup message before redirecting him that he is successfuly registred and then More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
How to display message before redirect
I have to display message before redirect and it has to be without header refresh - because I can't use that on my hosting. I made it with JavaScript but what if someone has not JavaScript enabled?... More on stackoverflow.com
🌐 stackoverflow.com
display a message before (or after) a redirect javascript - Stack Overflow
I'm trying to display a message after or before a redirect...The problem is that the redirect is on the server side (so I cannot call a javascript function or I cannot put a document.getElementByID). Exactly. So you can't do that. Instead, you need to return a redirect to a page with your message ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Allwebdevhelp
allwebdevhelp.com › php › help-tutorials.php
PHP - How To Display Message Before Redirect To Other Page - AllWebDevHelp.com
PHP - How To Display Message Before Redirect To Other Page - Free PHP Programming Tutorials, Help, Tips, Tricks, and More.
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › 3ce34b8d-ee24-491a-b4f7-46629639afb3
Want to display a message before redirecting | Microsoft Learn
I suspect I'm going to run into trouble when the place I want to go to itself takes URL parameters: "anotherpage.aspx?recordID=N" ... You can use a simple message box, so if the user click on the OK in the msgbox before the redirect will happen
🌐
ASPSnippets
aspsnippets.com › Articles › ASPNet-MVC-Display-message-before-redirect.aspx
ASP.Net MVC: Display message before redirect
April 13, 2021 - explained with an example, how to display a message before redirecting in ASP.Net MVC Razor. When a Button is clicked, first an operation will be performed, then a message will be displayed using JavaScript Alert Message Box and finally the page will be redirected in ASP.Net MVC Razor.
🌐
Stack Overflow
stackoverflow.com › questions › 30641733 › how-to-display-message-before-redirecting-to-another-page
asp.net mvc - How to display message before redirecting to another page? - Stack Overflow
May 24, 2017 - You will need to use ajax to post the registration data, then in the ajax success callback, display a message. Alternatively do a normal post and redirect to a view that displays your message and from there redirect to the home page.
🌐
Stack Overflow
stackoverflow.com › questions › 29844579 › how-to-display-message-before-redirect
How to display message before redirect
I believe that Javascript is a proper-way to do your redirects, Now days, it's pretty rare to see people browsing with Javascript disabled, Plus Javascript is growing really fast, I wouldn't worry about people not using Javascript, they are probably not your audience anyways.
Find elsewhere
🌐
HubSpot
blog.hubspot.com › home › website › how to set up an html redirect on your website
How to Set Up an HTML Redirect on Your Website
June 17, 2022 - This delay comes in handy if you want to display a brief message before the redirection occurs. To redirect one HTML page to another page, you need to add a <meta> tag inside the <head> section of the old HTML page.
🌐
Talkerscode
talkerscode.com › howto › alert-in-php-and-redirect.php
Alert In PHP And Redirect
August 26, 2014 - First one is to alert a message in php and next is to redirect that page. Now, let see how to done this in php. <!DOCTYPE html> <html> <head> <title> alert in php and redirect </title> <script src=’js/jQuery.js’> </script> </head> <body> ...
🌐
GravityRanger
gravityranger.com › home › display text confirmation before redirecting in gravity forms
Display Text Confirmation Before Redirecting in Gravity Forms - Gravity Ranger
January 29, 2007 - You can use the gform_confirmation filter to output a confirmation message and then perform a redirect (code snippet below). Add custom JavaScript to the text confirmation (code snippet below).
🌐
Stack Overflow
stackoverflow.com › questions › 43997081 › how-do-i-show-a-message-before-redirect
How do I show a message before redirect - javascript
May 16, 2017 - Easy to implement if you are using ajax for posting to your controller. ... LiteralTypeMessage.Text = MessageType; if (string.IsNullOrWhiteSpace(redirect) || redirect == ""){ LiteralMessage.Text = " " + message; } else{ LiteralMessage.Text = " " + message + ", <script>window.setTimeout(function () { $('.alert').fadeTo(1500, 0).slideUp(500, function() {$(this).remove();});window.location = '" + redirect + "'}, 3000);</script>"; } PanelMessage.CssClass = String.Format("alert alert-{0} alert-dismissable", type.ToString().ToLower()); PanelMessage.Attributes.Add("role", "alert"); PanelMessage.Visible = true;