Solved

PHP: Need to use HTTP_REFERER

Posted on 2004-05-03
12
1,167 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
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…

707 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

16 Experts available now in Live!

Get 1:1 Help Now