Solved

PHP Form Script - Redirect Based on Page URL

Posted on 2010-08-26
20
499 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
Comment Utility
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
Comment Utility
Hmm remove the echo from line 6.
0
 

Author Comment

by:ambientsbs
Comment Utility
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
 
LVL 9

Expert Comment

by:Snarfles
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
excellent and you're welcome :)
0
 

Author Comment

by:ambientsbs
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ambientsbs
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Thank you so much! You are the man Snarfles!!
0
 
LVL 9

Expert Comment

by:Snarfles
Comment Utility
Hehe excellent :) You're very welcome.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now