Link to home
Start Free TrialLog in
Avatar of dr34m3rs
dr34m3rs

asked on

PHP: Need to use HTTP_REFERER

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
ASKER CERTIFIED SOLUTION
Avatar of jkna_gunn
jkna_gunn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skullnobrains
skullnobrains

<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.
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"]?>
<< forced to add a backslash (/) >>
the backslash is this one (\)
Avatar of dr34m3rs

ASKER

<< note: HTTP_REFERER is not always available >>

It's not available for me. :)

Thanks.
why recommend echo over <?=

is there some advantage i dont know about?
$_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>
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.
<< $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.
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.
I actually ended up using:


javascript:history.back()
<<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...