Solved

PHP: Need to use HTTP_REFERER

Posted on 2004-05-03
12
1,170 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
  • 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 26

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 26

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 26

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 26

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 26

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
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.

776 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