Redirecting to another page using PHP: how, why and best practices [w/ subs]



00:00:00 – redirecting to another page using PHP
00:00:04 – how why and best practices normally a
00:00:11 – user requests a page by typing in a URL
00:00:14 – clicking on a link or submitting a form
00:00:17 – the server receives the request and send
00:00:23 – back the response for example a page of
00:00:26 – HTML sometimes though instead of
00:00:30 – replying with some content the server
00:00:33 – replies with a message to tell the
00:00:35 – browser to request a different URL this
00:00:40 – is known as a redirect it's common to
00:00:43 – redirect to another page after inserting
00:00:46 – some data into a database or logging in
00:00:49 – for example to issue a redirect in PHP
00:00:54 – we use the header function and we pass
00:00:57 – in a string that contains the URL of the
00:01:00 – page to redirect to let's take a look at
00:01:04 – an example here we have two files page
00:01:08 – one and page to page one contains code
00:01:12 – that issues a redirect and page two just
00:01:15 – contains some HTML if we go to page one
00:01:21 – in a browser it simply redirects to page
00:01:24 – two here I'm redirecting to a relative
00:01:30 – URL in other words a URL that doesn't
00:01:34 – contain the host name this works in
00:01:37 – modern browsers but redirecting to a
00:01:39 – relative URL has only being part of the
00:01:44 – HTTP standard since 2014 so to be on the
00:01:48 – safe side it's better to redirect to an
00:01:51 – absolute URL in other words one that
00:01:54 – contains the host name you could hard
00:01:59 – code the full URL in the header function
00:02:02 – call but it's easier to get the host
00:02:04 – name from the server like this
00:02:10 – and this still works as before one thing
00:02:19 – to be careful of is not to send any
00:02:22 – content before you call the header
00:02:24 – function for the redirect to work the
00:02:28 – HTTP header needs to be the first thing
00:02:30 – that the browser receives this can be as
00:02:33 – simple as a blank line before the
00:02:36 – opening PHP tag like that so if we try
00:02:40 – this again in a browser we get an error
00:02:47 – so make sure that the header function is
00:02:50 – called before anything else is written
00:02:53 – to the browser another thing you will
00:02:58 – want to do is exit the script as soon as
00:03:01 – you've redirected although not required
00:03:03 – it's good practice as the script keeps
00:03:06 – executing even though you've sent the
00:03:09 – header but the browser will never see
00:03:11 – the results of whatever comes after the
00:03:13 – V direction down here so we'll add a
00:03:16 – call to exit after the header like that
00:03:20 – and in the browser it still works as
00:03:26 – before
00:03:30 – one final thing that we can do is send a
00:03:33 – different HTTP status code depending on
00:03:36 – the type of redirection we're doing for
00:03:39 – example temporary or permanent hour
00:03:42 – which version of HTTP we're using and
00:03:44 – you can see the various options for
00:03:46 – redirect status codes here by default if
00:03:53 – we don't specify the status code a 302
00:03:56 – is sent using the Firebug browser plugin
00:04:02 – we can see what's happening when we
00:04:04 – issue a redirect if we open our page we
00:04:10 – can see that a 302 redirect has occurred
00:04:13 – normally this is ok but when doing
00:04:19 – things like using the post redirect get
00:04:22 – pattern for example when redirecting to
00:04:24 – an order confirmation page after
00:04:26 – processing an order form submitted using
00:04:29 – the post method we should really be
00:04:33 – using these 303 status code we can
00:04:38 – specify the status code in the third
00:04:41 – argument to the header function so let's
00:04:46 – add that to our function call like that
00:04:51 – and now if we go to our page we get the
00:04:57 – redirect with the 303 status code so
00:05:05 – doing all this is the recommended way to
00:05:08 – redirect to another page using PHP


Video Url:
http://youtu.be/OwmKRznMaO0

Comments

comments