[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Form Script - Redirect Based on Page URL

Posted on 2010-08-26
20
Medium Priority
?
557 Views
Last Modified: 2013-12-13
Experts,

I've seen a couple of posts similar to what I'm looking for but frankly I'm on a time crunch here and need to figure this out ASAP. I have a PHP script that I'm using to send my form data to email and I need to add a piece to that script that redirects to a specific "thank you" page depending on what page a user submits a form from.

So in plain english, the PHP script needs to say "If user is currently on www.mysite.com/page1.php redirect them to www.mysite.com/thankyou_page1.php. If user is on www.mysite.com/page2.php redirect them to www.mysite.com/thankyou_page2.php. If user is on www.mysite.com/page3.php redirect them to www.mysite.com/thankyou_page3.php...."

Please let me know how I would do this. Thank you in advance!!
0
Comment
Question by:ambientsbs
  • 12
  • 8
20 Comments
 
LVL 9

Expert Comment

by:Snarfles
ID: 33539380
Try this.
<?php
$pages = 3;
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
echo $curpage = curPageName();
for ( $counter = 1; $counter <= $pages; $counter ++) {
	if ($curpage == "page".$counter.".php"){
		header('Location: thankyou_page'.$counter.'.php');
	}
}
?>

Open in new window

0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33539390
Hmm remove the echo from line 6.
0
 

Author Comment

by:ambientsbs
ID: 33539431
Thanks for the info.....I think my "example" was a little misleading. My pages aren't actually called page1.php, page2.php, etc. and the thankyou pages aren't named thankyou_page1.php, thankyou_page2.php, etc.

The page names are totally random so I'll have something like www.mysite.com/auto-insurance.php and I want to redirect it to www.mysite.com/thankyou_autoinsurance.php. So basically, the naming of the pages isn't standardized and completely depends on what I've named them.

I'm guessing that an IF/ELSE statement is more what I'm going to need, but not sure.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Snarfles
ID: 33539449
Hmm ok try this then... even simpler.
<?php
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$curpage = curPageName();

header('Location: thankyou_'$curpage);

?>

Open in new window

0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33539450
crap forgot a dot...
<?php
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$curpage = curPageName();

header('Location: thankyou_'.$curpage);

?>

Open in new window

0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33539453
Or do you mean that even the suffix of 'thankyou_' wont be the case for all pages? If not then we are going to need a switch statement...
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33539464
This would work if the thank you page names aren't standardised... but you will have to go through and add the switches for each page that you need.
<?php
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$curpage = curPageName();
switch ($curpage) {
    case 'autoinsurance.php':
        $redirect = 'thanks_autoinsurance.php';
        break;
    case 'boatinsurance.php':
        $redirect = 'thanking_you_autoinsurance.php';
        break;;
    case 'planeinsurance.php':
        $redirect = 'ta_autoinsurance.php';
        break;
}
header('Location: '.$redirect);

?>

Open in new window

0
 

Author Comment

by:ambientsbs
ID: 33539511
It looks like the switch statement is exactly what I'm looking for. I won't be able to implement this until first thing in the morning but will let you know and award points right away. Thanks for your help! Much appreciated!!
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33539567
excellent and you're welcome :)
0
 

Author Comment

by:ambientsbs
ID: 33542300
Well, it is close but for some reason the script is redirecting to the home page. There was an extra semicolon after the second break which I removed but that didn't do it.

Is it possible that the header('Location: ' .$redirect); line is wrong? Browsing the Internet it looks like this can be written different ways. Thanks!
0
 

Author Comment

by:ambientsbs
ID: 33544885
Sorry to bother you but any thoughts on why it might redirect to the home page rather than the thank you page?
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33547415
Hmm it shouldn't... that works fine on my site... http://snarfles.net/code/autoinsurance.php (i dont have a thank you page of course)

Its possible your url could be a bit different..

Try this modified code, it will actually grab your full URL, strip off the page name and then reconstruct it with the new page name after it goes through the switch statement.
<?php
function curPageName() {
	return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$curPageName = curPageName();

function curPageURL($curPageName) {
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
	$pageURL = str_replace($curPageName, "", $pageURL);
	return $pageURL;
}

$cur_path = curPageURL($curPageName);
switch ($curPageName) {
    case 'autoinsurance.php':
        $redirect = 'thanks_autoinsurance.php';
        break;
    case 'boatinsurance.php':
        $redirect = 'thanking_you_autoinsurance.php';
        break;;
    case 'planeinsurance.php':
        $redirect = 'ta_autoinsurance.php';
        break;
}
header('Location: '.$cur_path.$redirect);

?>

Open in new window

0
 

Author Comment

by:ambientsbs
ID: 33554795
Thanks for the feedback. I'm still seeing the same result....Is there any debugging tool (perhaps from Firefox) that I can use on my end to see what's happening? Thank you!
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33555316
Yep get firebug. www.getfirebug.com which allows you to inspect code... that wont help you with this though.

Comment out the header and print out the results instead

//header('Location: '.$cur_path.$redirect);
echo $cur_path.$redirect;

That will tell you if you are making the correct path or not.
0
 

Author Comment

by:ambientsbs
ID: 33555723
Ok....I think I see what the problem is here based on your "echo" recommendation. Currently my forms are using the following script:

<form id="form1" name="form1" method="post" action="/email.php">

I included an echo $curPageName in my script and it shows "email.php" no matter which page I try using a form from. Any thoughts?
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33555786
Hmm not sure what you mean..

curPageName(); will return the current file name of the page you are on...

So if you are on example.com/email.php it will return email.php

if you are on example.com/blah.php it will return blah.php

Is this not what is happening? What is the full url of the page you are on that is running this function?

The above form tag will always take you to email.php when you hit submit so yes curPageName() will return email.php.
0
 

Author Comment

by:ambientsbs
ID: 33555828
No that's not what's happening. The full url of the site I'm running this script on is www.heuerinsurance.com. I've got forms on many of the pages of the site that are using the form tag script I posted above.

I have a file called email.php that I threw all of the script that you gave me (and some more) in and I'm using the action piece of the form tag on each page to tell the form to use the email.php file to send me the information from the form.

So, what I mean is.....if I'm on www.heuerinsurance.com/index.php or www.heuerinsurance.com/manage-my-account.php and submit a form, it doesn't matter, the form returns email.php as the curPageName().

If what you're saying about redirecting to the email.php file is true, how can I tell the form which script file to use without redirecting to the email.php?
0
 
LVL 9

Accepted Solution

by:
Snarfles earned 2000 total points
ID: 33555902
Ok I think this is different to previously described... so you have multiple form pages all linking directly to email.php right? So on the email.php page you dont need to find the name of the current page but rather of the referring page. Instead of using the current page name in the switch statement... use the previous page by using this in the form pages
<?php
function curPageName() {
        return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$curPageName = curPageName();
?>
<form id="form1" name="form1" method="post" action="/email.php">
<input type="hidden" name="page_name" value="<?php echo $curPageName; ?>">
</form>

so this should pass through the current page of the form via the $_POST method..

and then in email.php use this
<?php
function curPageName() {
        return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$curPageName = curPageName();

function curPageURL($curPageName) {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
                $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } else {
                $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        $pageURL = str_replace($curPageName, "", $pageURL);
        return $pageURL;
}

$cur_path = curPageURL($curPageName);
$previous_page = $_POST['page_name'];
switch ($previous_page) {
    case 'autoinsurance.php':
        $redirect = 'thanks_autoinsurance.php';
        break;
    case 'boatinsurance.php':
        $redirect = 'thanking_you_autoinsurance.php';
        break;;
    case 'planeinsurance.php':
        $redirect = 'ta_autoinsurance.php';
        break;
}
header('Location: '.$cur_path.$redirect);

?>

Open in new window

0
 

Author Closing Comment

by:ambientsbs
ID: 33555927
Thank you so much! You are the man Snarfles!!
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33555935
Hehe excellent :) You're very welcome.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 8 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question