Solved

PHP: Need to use HTTP_REFERER

Posted on 2004-05-03
12
1,175 Views
Last Modified: 2011-10-03
I'm a total noob only familiar with perl.

Trying to get this to work:

{$ref = $_SERVER["HTTP_REFERER"]}

<form action="{$ref}" method="get">
<input type=submit style=color:blue;background:white; value="back">
</form>

Thank you very much.

Best regards,

Dr34m3r
0
Comment
Question by:dr34m3rs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 6

Accepted Solution

by:
jkna_gunn earned 250 total points
ID: 10975905
maybe try this

<a href="<?=$_SERVER["HTTP_REFERER"]?>Back</a>

note: HTTP_REFERER is not always available
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 10975966
<a href="<?=$_SERVER["HTTP_REFERER"]?>">Back</a>
same withe the end quote and end of the <a...> tag. (will work out of the <? ... ?> tags)

inside php tags, u can use
echo "<form action=\"$ref\" method=\"get\">";
   or
echo "<form action=\"".$_SERVER["HTTP_REFERER"]."\" method=\"get\">";

outside, same as previous
<form action="<?=$_SERVER["HTTP_REFERER"]?>" method="get">

the <? ?> are parsed before the html code and independantly.
you can of course use simple quotes if you don't like stripping
php does not parse variables contained in a string enclosed with simple quotes.

see ya.
0
 

Expert Comment

by:loureiro
ID: 10979189
when echoing html be sure to use single (') quotes and not double quotes as you will be forced to add a backslash (/) wherever you have a doublequote ("). When creating queries to send to mySQL use doublequotes as this will allow you to insert variables directly into your query without having to concatenate the string. just a little tip. I noticed that some of the post above were using doublequotes for the html. just thought i'd help.

I would also suggest using a css button as it appears you are using css for formating. just keeps consistency. Also i recommend using echo to print details to the html. so use <?php echo $_SERVER["HTTP_REFERER"]; ?> instead of <?=$_SERVER["HTTP_REFERER"]?>
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 27

Expert Comment

by:skullnobrains
ID: 10981511
<< forced to add a backslash (/) >>
the backslash is this one (\)
0
 
LVL 1

Author Comment

by:dr34m3rs
ID: 10983252
<< note: HTTP_REFERER is not always available >>

It's not available for me. :)

Thanks.
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10984285
why recommend echo over <?=

is there some advantage i dont know about?
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 10985005
$_SERVER["HTTP_REFERER"] is only available when the page has a referer ie there must be a page just before in the history of the client's browser.

$HTTP_REFERER is available if register globals is set on and the previous is true.

can u try ""print_r($_SERVER);"" + make sure that the page has a referer before you test.

<off topic>
u can use both simple and double quotes for both php and html.
php parses variables enclosed in double-quoted strings, and prints single-quoted as typed.
html double-quotes anre most common, single-quotes is a tolerance.
u cannot escape chars in html, the backslash will show in the text, and the quote be parsed.
</off>
0
 

Expert Comment

by:loureiro
ID: 10987871
i was just trying to make a recommendation. i find that what i wrote before makes the code easier to use. if you use single quotes you can put html directly in without having to add any slashes to get it to read characters like " ect.

echo '<html><body align="center">Hello World</body></html>';

instead of...

echo "<html><body align=\"center\">Hello World</body></html>";

as you can imagine it gets very annoying when you have a bunch of code you want php to write that has something similar to the above.

doublequotes allows you to directly insert variables into your code example:

$post is a variable that was sent through from another post form.

extract($_POST);
$query ="SELECT * FROM `customer` WHERE `clientID` = '$post' AND support = '12'";

instead of...

extract($_POST);
$query ='SELECT * FROM `customer` WHERE `clientID` = \''.$post.'\' AND support = \'12\'';

it just cuts down on some potentially unneeded parse errors.
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 10995581
<< $query ='SELECT * FROM `customer` WHERE `clientID` = \''.$post.'\' AND support = \'12\'';>>
definitely a parse error.

try
$query ="SELECT * FROM `customer` WHERE `clientID` = '$post' AND support = '12';'';
rather convenient for sql.
0
 

Expert Comment

by:jtw90210
ID: 11131986
some clarifications on what was posted above.

Not all web browsers provide HTTP_REFERER. Some browsers let you shut it off, some personal security products block it, if somebody types an address it won't be there at all, and in my experience it is not a great thing to rely upon entirely, especially as posed in the question here.

Second, not all versons of PHP seem to be configured to recgonize the <?= syntax which is probably why 'echo' is preferred.

Third, there's one big difference between single and double quotes in PHP: single quotes supress the intrepretation of the contents. so if $w='world'; then

echo "Hello $w\n"; says Hello World with a carriage return while the single-quote equivalent
echo 'Hello $w\n"; says Hello $w\n without a carriage return.

Finally, when putting the contents of variables into a database, using single ticks alone is asking for trouble. Use addslashes() instead.

"SELECT lastvisit FROM registrants WHERE username='".addslashes($userenteredusername)."'"

because otherwise an apostrophie will err your sql.
0
 
LVL 1

Author Comment

by:dr34m3rs
ID: 11132019
I actually ended up using:


javascript:history.back()
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 11132285
<<Not all web browsers provide HTTP_REFERER>> thanks jt, don't actually ever used it but nice tip to know of.
cool you got it, dr...
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

729 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