Showing posts with label PHP Redirect. Show all posts
Showing posts with label PHP Redirect. Show all posts

Monday, 24 September 2018

PHP redirect – go back to previous page

To go back to the previous page the superglobal variable $_SERVER can be used.
$_SERVER['HTTP_REFERER'] has the link to the previous page.
So to redirect simply :
1
2
3
4
5
6
7
8
#Method to go to previous page
function goback()
{
    header("Location: {$_SERVER['HTTP_REFERER']}");
    exit;
}
 
goback();

Tuesday, 18 September 2018

303 redirect with PHP

When you submit a form using a web browser the script processing the form often redirects after it has done its work to a thank you page (or similar). The response type people usually use when coding with PHP is a 302 redirect because it's the default used when issuing a Location: header. The problem with this is on a page refresh the browser is likely to submit the form again. Instead you can use a 303 redirect to prevent the form being submitted again. This post looks at what a 303 response code / redirect is and how to send one with PHP.
Typically after processing the form a script will simply display some content to show it's done (e.g. with a thank you page, or the form again in an admin system). This will always repost the data on a page refresh (although most browsers will warn you before doing so). Another alternative is to redirect to another page afterwards like this:
header("Location: http://$_SERVER[HTTP_HOST]/path/to/my/script");
This will issue the default 302 response code in PHP which means a temporary redirect. On page refresh the browser is also likely to repost the form again although this behaviour can vary between browsers and versions. Having the form be resubmitted is generally not what you want to happen - for example it may mean someone submitting an email form on your site multiple times - so you can use a 303 response instead.
A 303 response code, in the words of the W3C, is as follows:
The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.
The different URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).
What this means is that the location you specify as part of your redirection should only ever be requested using the GET method and that the original page should not be requested again.
To do this in PHP you would do the following:
header("HTTP/1.1 303 See Other");
header("Location: http://$_SERVER[HTTP_HOST]/path/to/my/script");
The browser would therefore post the form to this page, then be redirected using a GET request to /path/to/my/script. If the user then refreshed the page in the browser it would again make a GET request to /path/to/my/script and not a POST to the original page.
You can also read my post on doing a 301 redirect with PHP, which is what you need to do when permanently redirecting a regular page request from one URI to another.

Related posts:

301 redirect with PHP

When you change the URL/URI of a page, a permanent redirection should be set up from the old address to the new address to let user-agents know where it can now be found. When using a web browser the redirection is usually seamless; for search engine bots they will now know to stop requesting the old page and instead request the new address in the future. This post looks at how to do this with PHP.
In PHP it's very simple to do a 301 redirect. The example below shows how to do this by sending the new location as the website's homepage, using the $_SERVER['HTTP_HOST'] variable to set the domain name.
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://$_SERVER[HTTP_HOST]/");
Depending on what your script is doing, you may also need to add an "exit;" line underneath the code above to prevent it doing anything else.
If you need to redirect to a specific page, you can add it after the hostname part:
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://$_SERVER[HTTP_HOST]/path/to/my/script");
Although you are supposed to include the full URI, including the http:// and domain name part, it is possible to omit these and the user-agent will still follow through to the new address. For example:
header("HTTP/1.1 301 Moved Permanently");
header("Location: /path/to/my/script");
However you should really include the domain name to follow the specification.

Related posts:

Monday, 13 July 2015

PHP redirect – go back to previous page

To go back to the previous page the superglobal variable $_SERVER can be used.
$_SERVER['HTTP_REFERER'] has the link to the previous page.
So to redirect simply :

#Method to go to previous page


function goback(){
    header("Location: {$_SERVER['HTTP_REFERER']}");
    exit;
}
goback();

Wednesday, 3 June 2015

HTTP Redirection in PHP

Allows to perform PHP redirection (must be placed before any browser output).
  <?php
       // stick your url here
        header('Location: http://you_address/url.php');
        exit;
    ?>