Forum Moderators: coopster

Message Too Old, No Replies

Change content based on referal URL

         

sun818

3:45 am on Nov 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi, can someone help me with some PHP scripting? I want to run a promotional for my store where buyers coming from a specific URL can view the promotion. Those visiting directly or linked from elsewhere would get a "sorry" type notice.

pseudo code:
if
referal url is from otherwebsite.com/promo.htm
then
show promo page
else
sorry, promo page not available

toadhall

4:35 am on Nov 18, 2002 (gmt 0)

10+ Year Member



Hi sun818

Put this at the top of the page. No spaces separating it from the top.

echo ($HTTP_REFERER == "http://otherwebsite.com/promo.htm"?header('Location:promopage.htm'):"Sorry, no promo page available");

...it's all one line. Substitute a variable with a longer "Sorry" notice, or another header("location:etc... if you like.

T

sun818

5:46 am on Nov 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Great, that works. For my specific web host, I had to start with <?php then end with a ?> for your code to work. Very cool :) Thank you.

toadhall

4:55 pm on Nov 18, 2002 (gmt 0)

10+ Year Member



Great!
Just one note. If you were to use the "variable" suggestion I made it would mean the variable declaration would come before the header() function which might (read probably would) cause a problem. So, scratch that. But a second use of the header() redirect would be OK.

T

kcartlidge

9:17 pm on Nov 18, 2002 (gmt 0)

10+ Year Member



Lovely idea - I may nick it!

I'd just mention though that it's fine to put as much PHP as you want before the header statement, provided that no HTML appears prior to it.
When I say no HTML, that includes extraneous spaces before the PHP, so you could put
<?PHP
echo ".....";
$name = "....";
header etc..
?>
but the <?PHP MUST be the very first characters of the very first line.

andreasfriedrich

9:47 pm on Nov 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



kcartlidge´s code will not work. You`ll get the infamous Warning: Cannot add header information - headers already sent by error message.

Unless you enable output buffering header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP (echo '...'; in kcartlidge´s example).

Andreas

andreasfriedrich

9:55 pm on Nov 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



kcartlidge has a point though, it's fine to put as much PHP as you want before the header statement, provided that no HTML appears prior to it and this PHP code does not send any output.

So there´s no cause for toadhall´s concern about any variable declaration [coming] before the header() function.

Andreas

kcartlidge

10:32 pm on Nov 18, 2002 (gmt 0)

10+ Year Member



Andreas,

You're very right. I just went ahead and typed in some 'random' PHP above the header. Unfortunately, the most common statement I use is 'echo' and I typed it without even thinking [doh].

In one breath I say NO OUTPUT, in the next I use an ECHO command - pretty stupid huh?

Yes, NO OUTPUT before header and all's fine.
No, don't use the ECHO statement before the header.

Many thanks for pointing it out ...