in this video how to make a redirect in
php
[Music]
now a lot of the stuff that we build is
in wordpress and so there is a way to do
a redirect
in wordpress with the wp underscore
redirect
function that's built into wordpress but
we're not talking about that function
we're talking about going back to the
raw root language of php
and doing a redirect in php so i'm going
to show you guys how to do that in this
video let's get started
so here on the ideaproio example page
we have our code here and we've built
this page that's just a standalone php
page now this site
is wordpress but we're not bringing into
wordpress into this
site so we've got our example page here
and we want to redirect this page
on page load to another page so
what we're going to do is we're going to
put in header
location and
then we're going to redirect to example
2 dot php okay so
now we need to create a new file
example 2.php
and we need to save that and
we're going to open some php tags here
and we're going to say
this is page 2.
okay and so now our example is going to
do nothing but
redirect it to that next page
and there we are we're on this is page
two now
if we would have left in here at the top
if we would have said
welcome to the example page
and we came back over here and we went
back to the
example page let's go to example
there we go it's still going to push us
to that example to page
but there is instances where
echoing anything out to the page could
cause
a headers already sent to the page
so typically anything that's displayed
on the page
you don't want it to be displayed on the
page
if you're using the header location
to redirect a page so in this case
it's just php so it says don't worry
about it
now after this redirect here
we want to say die or we could even use
exit so
either one of these both of them do
basically the exact same thing it just
depends on which one you want to use
die or exit it doesn't really matter the
reason why you want to do that
is because let's say you have this in an
if statement right
and you say if
it's set let's do this is set
post name
right if if the post exists
redirect to the next page else
[Music]
welcome so
let's take these out take those out for
a second
okay so now we come back over here to
the example page
and boom it says welcome because
we haven't submitted a form a post
so if we didn't
have die or exit after this
and we had code down here that ran
you know
if that was a function or something like
that that um
did something and we didn't put dye up
here
php may rin may continue on
faster then it will redirect you to that
next page and it could
run the code that's down here so it's
important to
after the redirect either use the exit
or
die function to stop php from doing
anything further because you're already
telling it to
redirect to this page so it's going to
do that
but dying or exiting
after that is going to keep it from
continuing on down the page
this was a quick simple video but it's
one that i use all the time i you know i
use
the header function to redirect
pages after you know a submit or
something like that
after someone's logged in you know it
captures their login and then redirects
them back to
an account page or something like that
as always i hope you enjoyed this video
thank you for watching subscribing and
commenting
and i will see you in the next one
thanks