#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2

    Redirect best method


    Hello,

    I have seen many types of redirects on the internet. The following are some. Which one is the best ? Also Do I have to specify the absolute url in the Location ? Is there any benefit ?

    Code:
    header('Location: http://www.example.com/', true, 302);
    exit();
    Code:
    header( 'HTTP/1.1 301 Moved Permanently' );
    header( 'Location: http://www.example.com' );
    exit();
    Code:
    You can just simply specify URL to which form will go after being submitted.
    
    <form action="http://localhost/redirected.php" method="post">
    
    You can check with PHP whether form is submitted and then redirect:
    
    if (isset($_POST['answer']))
        header('Location: redirect.php');
    
    And You can achieve this with javascript/jquery.
    
    $('#submit_button').click(function () {
        // do everything with variables
        window.location = 'redirect.php';
    });
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,238
    Rep Power
    9400
    First and second are equivalent (except for the different status code) and are the best overall. They result in an immediate redirect and don't require Javascript or an HTML meta redirect to work. However they do not allow you to show the user a message before the redirection.

    Second best is a meta redirect with HTML.
    Code:
    <meta http-equiv="Refresh" content="10; url=http://www.example.com" />
    which will redirect after 10 seconds. This allows you to show a message, but if you don't need to then even with a timeout of 0 seconds it's not quite an immediate redirect.

    Using a form or Javascript for an automatic redirection (ie, no user input required) sucks. Don't do it that way.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2
    okay, I will go with this one:
    header('Location: http://www.example.com/register.php', true, 302);

    What is the difference between this one and
    header('Location: http://www.example.com/register.php'); ?

    Also what is the difference when I give a relative path for the url as follows:
    header('Location: register.php);
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    this refresh stuff is awful in my opinion.

    It may have been acceptable in the 90s, but nowadays, I certainly don't wanna be forced to helplessly watch some half-empty page for 10 seconds until finally the browser redirects me to the actual content which again has to be loaded.

    Just put the message on the target page, maybe in a small non-modal(!!) dialog box. Then you can notify your users without getting on their nerves.



    Originally Posted by aniyanrajan6
    What is the difference between this one and
    header('Location: http://www.example.com/register.php'); ?
    As you can see, the first variant uses additional parameters (namely the response code). Things like this are explained in the PHP manual.



    Originally Posted by aniyanrajan6
    Also what is the difference when I give a relative path for the url as follows:
    header('Location: register.php);
    This is only valid in HTTP 1.1.

    However, the old standard has been violated so often that most browsers will support relative URLs regardless of the HTTP version.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0
    If you are using PHP then you should use PHP header function to redirect.

    Although you can use Javascript redirect for your page redirection . You can follow this tutorial related to Javascript Rediret.

IMN logo majestic logo threadwatch logo seochat tools logo