How to go to new Html page from PHP script? - php

After the php script is over, how do I go to another html page?

Why would you want to do that? When the page is over (which I understand as the script ended execution), it is usually sent to the client and then it can be viewed by the user. So, basically, what you are trying to do is to redirect the user after the script stopped executing.
If so, you have two solutions available:
Do not output anything, and after your script stopped executing, use the header() PHP function:
header('Location: http://example.com/some/url');
where you should replace the example URL with your own.
If you are outputting the HTML page and sending it gradually to the user, you can just put JavaScript redirection script at the end of the page (so after everything has been sent):
<script>
window.location = 'http://example.com/some/url';
</script>
Does any of these solutions work for you?

header('Location: /page.html');

Make sure you don't output anything else, then simply
header('Location: http://www.example.com/');

Related

Show a loading message while php is processing followed by a redirect to another page

I am trying to write a page in php which shows a loading message while it does some processing and then auto redirects to another page
<?php
//show a loading message - this is the bit I need help with
// do some processing - don't need help with this bit
header("Location: http://www.mysite.com/mynextpage.php");
exit;
?>
I can't use echo or javascript otherwise I get a "Cannot modify header information - headers already sent" error when the page executes.
Any clues?
First of all, you mustn't use any header change after outputing some data, that is why you get the error above.
Another way, use header redirections by refreshing page on next page:
<?php
header('Refresh: 5; url=http://www.mysite.com/mynextpage.php' );
echo 'Wait 5 sec then redirected';
Note:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Best way is to use ajax requests. Via javascript you should show the loading element, perform the request, on success redirect on target page
Using java script to redirect after the processing has finished seems to be the way to go.
I'm using
<script type="text/javascript">
window.location="http://www.mysite.com/mynextpage.php";
</script>
at the end of the page and it's working

window.location (JS) vs header() (PHP) for redirection

using JS : (in <head> tag)
<script>window.location="https://stackoverflow.com";</script>
using PHP : (in <head> tag)
header('Location: https://stackoverflow.com');
end();
Which one I should use ? or another ?
and what about using <meta>?
<meta http-equiv="refresh" content="0;url=https://stackoverflow.com"/>
Many good answers , I don't know which answer I will accept, Thanks so much
The result is same for all options. Redirect.
<meta> in HTML:
Show content of your site, and next redirect user after a few (or 0) seconds.
Don't need JavaScript enabled.
Don't need PHP.
window.location in JS:
Javascript enabled needed.
Don't need PHP.
Show content of your site, and next redirect user after a few (or 0) seconds.
Redirect can be dependent on any conditions if (1 === 1) { window.location.href = 'http://example.com'; }.
header('Location:') in PHP:
Don't need JavaScript enabled.
PHP needed.
Redirect will be executed first, user never see what is after. header() must be the first command in php script, before output any other. If you try output some before header, will receive an Warning: Cannot modify header information - headers already sent
A better way to set the location in JS is via:
window.location.href = 'https://stackoverflow.com';
Whether to use PHP or JS to manage the redirection depends on what your code is doing and how. But if you're in a position to use PHP; that is, if you're going to be using PHP to send some JS code back to the browser that simply tells the browser to go somewhere else, then logic suggests that you should cut out the middle man and tell the browser directly via PHP.
It depends on how and when you want to redirect the user to another page.
If you want to instantly redirect a user to another page without him seeing anything of a site in between, you should use the PHP header redirect method.
If you have a Javascript and some action of the user has to result in him entering another page, that is when you should use window.location.
The meta tag refresh is often used on download sites whenever you see these "Your download should start automatically" messages. You can let the user load a page, wait for a certain amount of time, then redirect him (e.g. to a to-be-downloaded file) without Javascript.
PHP redirects are better if you can as with the JavaScript one you're causing the client to load the page before the redirect, whereas with the PHP one it sends the proper header.
However the PHP shouldn't go in the <head>, it should go before any output is sent to the client, as to do otherwise will cause errors.
Using <meta> tags have the same issue as Javascript in causing the initial page to load before doing the redirect. Server-side redirects are almost always better, if you can use them.
The first case will fail when JS is off. It's also a little bit slower since JS must be parsed first (DOM must be loaded). However JS is safer since the destination doesn't know the referer and your redirect might be tracked (referers aren't reliable in general yet this is something).
You can also use meta refresh tag. It also requires DOM to be loaded.
window.location.href = 'url';
is beter than
header('location:url');
because the header command is mustly return an error "Warning: Cannot modify header information - headers already sent"
using js window.location.href = 'url';
this is beter

script is send then Page is not redirect

If click the submit button, then this code should execute, but this is not working.
header('Location: http://www.yourtargeturl.com'); //script api
echo '<script>window.open("'.$_SERVER['PHP_SELF'].'","_self")</script>';
You're mixing PHP and Javascript. The header alone will send the user to the new location in that window.
Note: PHP exists on the server, and is then sent to the browser after being generated/ran, so the javascript you have needs to be echoed to the browser to do anything.
Your header('Location: http://www.yourtargeturl.com'); will redirect the page, and make the browser load that page instead.
Everything after that is ignored. So, your JavaScript window.open is never written to the page and ran.

Refreshing a page with javascript using php header('location')

I want use a single php file to handle all of my voting requests.
Currently the script will, if siteType isn't set, forward the user and display a message. If the user has JS on then it will return a json object and ajax the page.
if(!isset($_COOKIE['siteType'])){
displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
header('Location:/');
exit;
}
I need it as that if this php code above is executed the page will reload, i assume with javascript and reading the http headers?
[Edit]
I didn't make myself clear enough, but this is a ajax request. I don't output any html. So this really is just about getting js to handle the header?
You can't Refreshing a page with javascript using php header('location')
Because, header('Location: xxx'); must be the only output of your PHP script, it is a header, you can not put it after javascript syntax
PHP
echo '<meta http-equiv="refresh" content="0">';
Javascript
window.location.reload();
Maybe this would be some solution for you,
if(!isset($_COOKIE['siteType'])){
displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
echo '<script>window.location.reload()</script>';
exit;
}

How do i get a link to run a Php script?

Hey, does anyone know how i cant get a href link too run a PHP script? Like the href dosent change page just runs a PHP script above? Any pointers would be great :))
Use Ajax to make the request to the PHP page in the background.
If you link to a script which, after executing some code, tells the browser to redirect back to your main page, you will achieve that.
example:
<?php
//some code...
header("Location: $_SERVER['HTTP_REFERER']");
?>
It's one way to do it. It doesn't work on every server.
Alternatively, you can hardcode your url, like:
header("Location: path/to/the/page/you/want/to/go/back/to");
...

Categories