• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 564
  • Last Modified:

PHP Form Script - Redirect Based on Page URL

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
ambientsbs
Asked:
ambientsbs
  • 12
  • 8
1 Solution
 
SnarflesCommented:
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
 
SnarflesCommented:
Hmm remove the echo from line 6.
0
 
ambientsbsAuthor Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
SnarflesCommented:
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
 
SnarflesCommented:
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
 
SnarflesCommented:
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
 
SnarflesCommented:
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
 
ambientsbsAuthor Commented:
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
 
SnarflesCommented:
excellent and you're welcome :)
0
 
ambientsbsAuthor Commented:
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
 
ambientsbsAuthor Commented:
Sorry to bother you but any thoughts on why it might redirect to the home page rather than the thank you page?
0
 
SnarflesCommented:
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
 
ambientsbsAuthor Commented:
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
 
SnarflesCommented:
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
 
ambientsbsAuthor Commented:
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
 
SnarflesCommented:
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
 
ambientsbsAuthor Commented:
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
 
SnarflesCommented:
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
 
ambientsbsAuthor Commented:
Thank you so much! You are the man Snarfles!!
0
 
SnarflesCommented:
Hehe excellent :) You're very welcome.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 12
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now