?
Solved

PHP: Need to use HTTP_REFERER

Posted on 2004-05-03
12
Medium Priority
?
1,176 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 1000 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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