Redirect with PHP

User 2336860 Photo


Registered User
252 posts

Here is what I am trying to do:
Check a variable in a url (www.something.com/stuff.php?variable1=x with x being an integer)
See if it matches a set integer
If it matches go to this url
If no match continue rest of php script

What I have so far, and it seems to work. But with my club-fisted way with php is there a better way?
if($_GET['variable'] == 54){
header("Location: file-name.php");
die();
}

else
// continue with rest of php script here

User 187934 Photo


Senior Advisor
20,190 posts
Online Now

Looks good to me.;)
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2336860 Photo


Registered User
252 posts

Great, I just was not sure if my method/thinking was good. I Googled and read and ended up with that. And in testing via WAMP it seems to do exactly what I want, but it is always reassuring to have some other eyes on things at times.

User 10077 Photo


Senior Advisor
1,095 posts

Chef-Scott wrote:

if($_GET['variable'] == 54){
header("Location: file-name.php");
die();
}

else
// continue with rest of php script here

Is the whole page conditional? For example, if the whole page will always run EXCEPT when the variable condition is met then you do not need the "else." Just put the code at the top and you're good.
if($_GET['variable'] == 54){
header("Location: file-name.php");
die();
}
// the rest of the page goes here


However, if you want the page to run ONLY if the variable value is not met, then use the else.
if($_GET['variable'] == 54){
header("Location: file-name.php");
die();
}else{
// the rest of the page goes here.
}


If you need to forward to multiple URLs based on the variable, you can use a variable in header().

if($_GET['variable']){
if($_GET['variable'] == '54'){
$gotourl = 'http://url1.com';
}elseif($_GET['variable'] == '62'){
$gotourl = 'http://url2.com';
}
header("Location: $gotourl");
die();
}

ASK ME ANYTHING
I provide personalized help for Coffeecup Users including personal or group training for Site Designer, Web Form Builder and more via Zoom.
Email me at support@uscni.org or call 865-687-7698.

Did you know that Web Form Builder can be used for both simple and complicated forms and that it's not limited to the default fonts and buttons? Take a look at a form we developed for WindowTinting.com.
https://forms.windowtinting.com/forms/w … ppingcart/
User 2336860 Photo


Registered User
252 posts

Brian Durfee wrote:

Is the whole page conditional? For example, if the whole page will always run EXCEPT when the variable condition is met then you do not need the "else." Just put the code at the top and you're good.
if($_GET['variable'] == 54){
header("Location: file-name.php");
die();
}
// the rest of the page goes here


So I can drop the use of else with no ill effects? Cool. Because this page does run except when it matches the condition.
Should I check to make sure the variable is numeric first? Or should I be okay just by matching? Am I just trying to make this more complicated than it is? :D

User 10077 Photo


Senior Advisor
1,095 posts

If the whole page is conditional based on a variable match, then:

URL: http://something.com/pagename.php?var1=xyz

if($_GET['var1'] == 'xyz'){
// all your page coding goes here.
}else{
header("Location: file-name.php");
die();
}

Even if the variable is numeric, go ahead and put the value in single quotes, and it will handle numbers or strings

// URL: http://something.com/pagename.php?var1=xyz
if($_GET['var1'] == 'xyz'){

// URL: http://something.com/pagename.php?var1=123
if($_GET['var1'] == '123'){

ASK ME ANYTHING
I provide personalized help for Coffeecup Users including personal or group training for Site Designer, Web Form Builder and more via Zoom.
Email me at support@uscni.org or call 865-687-7698.

Did you know that Web Form Builder can be used for both simple and complicated forms and that it's not limited to the default fonts and buttons? Take a look at a form we developed for WindowTinting.com.
https://forms.windowtinting.com/forms/w … ppingcart/

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.