Forum Moderators: coopster
I have a Recommend page script written in PHP that requests the referer and allows a form to be filled in. It then stuffs the referer parameter into the email and then also into a hidden field. Trouble is that is submits to itself for validation etc, so the referer is the mailto.php page and not the original referer. Any suggestions.
I was trying to avoid the use of Java and i was using the _target to open a new window.
Cheers
Richard
By the way, that's what ww.php.net does for their comment form for the manual. When you click to "Add a comment" in the documentation, they pass the original URL (the man page your on) to the comment page as a GET param rather than depending on the referer var. I'm not sure why.
Tom
If you're having trouble with the sessions ukgimp, post the code. It should be relatively easy, you're probably just missing something obvious to everyone but you ;) Happens to me everyday!
Nick
CODE
<?PHP
# Edit variables below:
#
# Title of the "poped" page:
$GLOBALS["title"]="E-mail this page to a collegue";
# Path to mailto.php script:
$GLOBALS["path"]="mailto.php";
# Site name:
$GLOBALS["site_name"]="MY Site";
# webmaster's email:
$GLOBALS["your_email"]="my email";
$GLOBALS["url"]= "$HTTP_REFERER";
if($url_of_referring_page!= $PHP_SELF){
$url_of_referring_page = $HTTP_REFERER;
}
##############################################
function show_form() {
?>
<html>
<head>
<title><?PHP echo $GLOBALS["title"];?></title>
</head>
<body>
Recommend <b><?PHP echo $GLOBALS["url"];?></b> to a friend ...
<form method="post" action="<?PHP echo $GLOBALS["path"];?>">
<input type=hidden name="url" value="<?PHP echo $GLOBALS["url"];?>">
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr align="left" valign="top">
<td width="50%"> Your name: </td>
<td>
<input type="text" name="form[from]" size="30">
</td>
</tr>
<tr align="left" valign="top">
<td> <small>*</small> Your e-mail: </td>
<td>
<input type="text" name="form[from_email]" maxlength="40" size="30">
</td>
</tr>
<tr align="left" valign="top">
<td> <small>*</small> Friend's e-mail:</td>
<td>
<input type="text" name="form[to_email]" size="30">
</td>
</tr>
<tr align="left" valign="top">
<td> Short note about this page: </td>
<td>
<textarea name="form[comment]" rows="5" cols="30"></textarea>
</td>
</tr>
<tr align="left" valign="top">
<td>
<input type="submit" name="submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</td>
<td> </td>
</tr>
</table>
<p><small>* - required!</small></p>
</form>
</body>
</html>
<?PHP
}
function error($string) {
print ("<div align=center valign=center><b>Warning:</b> $string<br><br>
[ <a href=\"javascript:history.go(-1)\">Back</a> ] ¦
[ <a href=\"javascript:window.close()\">Close this window</a> ]
</div>");
exit;
}
function check_email ($address) {
# this function was copied from PHP mailing list
return
//mod note: line breaks added for side scroll
(ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+'.'@'.' [-!#$%&\'*+\\/0-9=?A-Z^_`a-z{¦}~]+\.'.'[-!#$%&\'*+\\. /0-9=?A-Z^_`a-z{¦}~]+$',$address));
}
// end mod note: jatar_k
if ($submit) {
if (! check_email ($form["from_email"]) ¦¦! check_email ($form["to_email"]) ) error ("Invalid e-mail address!");
$date=date( "D, j M Y H:i:s -0600");
$to_email=$form["to_email"];
$from=$form["from"];
$from_email=$form["from_email"];
$comment=$form["comment"];
$site_name=$GLOBALS["site_name"];
$your_email=$GLOBALS["your_email"];
$message="Hi!\n\n$from ($from_email) invited you to visit $site_name!\nCheck out this URL: $url_of_referring_page";
if ($form["comment"]!= "") {
$message.="\n\n$from left you a note:\n$comment";
}
$message.="\n\n\n-------------------------------------------------------------------\n";
$subject="You were invited by $from to visit ".$GLOBALS["site_name"]."!";
$add="From: $site_name <$your_email>\nReply-To: $from_email\nDate: $date\n";
if (@mail ("$to_email","$subject","$message","$add")) {
echo "<center>Message sucessfully send!<br>Thank you!<br><br>[ <a href=\"javascript:window.close()\">Close this window</a> ]</center>";
} else error ("Internal server error. Cannot send email, please try later!");
} else show_form();
?>
[edited by: jatar_k at 10:52 pm (utc) on Mar. 19, 2003]
The email checker was throwing errors so I substituted a simple one for testing.
<?PHP
# CHANGES - lines 16-19; 32,33; 58,59; 70,71. Removed show_form function and moved html (ll. 62-108)
# CAUTION - line 37
# Edit variables below:
#
# Title of the "poped" page:
$GLOBALS["title"]="E-mail this page to a collegue";
# Path to mailto.php script:
$GLOBALS["path"]="mailto.php";
# Site name:
$GLOBALS["site_name"]="MY Site";
# webmaster's email:
$GLOBALS["your_email"]="my email";
$GLOBALS["url"]= "$HTTP_REFERER";
# added this; removed the other conditional
if (empty($url_of_referring_page)){
$url_of_referring_page = $HTTP_REFERER;
}
############################################################
function error($string) {
print ("<div align=center valign=center><b>Warning:</b> $string<br><br>
[ <a href=\"javascript:history.go(-1)\">Back</a> ] ¦
[ <a href=\"javascript:window.close()\">Close this window</a> ]
</div>");
exit;
}
function check_email ($address) {
# this function you copied from PHP mailing list was returning errors so I threw in this simple one for testing purposes
return (ereg("^.+@.+\\..+$",$address));
}
if ($submit) {
/*** re-enter the "pipes' below after copying ***/
if (! check_email ($form["from_email"]) ¦¦! check_email ($form["to_email"]) ) error ("Invalid e-mail address!");
$date=date( "D, j M Y H:i:s -0600");
$to_email=$form["to_email"];
$from=$form["from"];
$from_email=$form["from_email"];
$comment=$form["comment"];
$site_name=$GLOBALS["site_name"];
$your_email=$GLOBALS["your_email"];
$message="Hi!\n\n$from ($from_email) invited you to visit $site_name!\nCheck out this URL: $url_of_referring_page";
if ($form["comment"]!= "") {
$message.="\n\n$from left you a note:\n$comment";
}
$message.="\n\n\n-------------------------------
$subject="You were invited by $from to visit ".$GLOBALS["site_name"]."!";
$add="From: $site_name <$your_email>\nReply-To: $from_email\nDate: $date\n";
/*
if (@mail ("$to_email","$subject","$message","$add")) {
echo "<center>Message sucessfully send!<br>Thank you!<br><br>[ <a href=\"javascript:window.close()\">Close this window</a> ]</center>";
} else error ("Internal server error. Cannot send email, please try later!");
*/
# the following line for testing purposes; remove and uncomment above
echo "$to_email<br>$subject<br>$message<br>$add";
} else {
?>
<html>
<head>
<title><?PHP echo $GLOBALS["title"];?></title>
</head>
<body>
Recommend <b><?PHP echo $GLOBALS["url"];?></b> to a friend ...
<form method="post" action="<?PHP echo $GLOBALS["path"];?>">
<input type=hidden name="url" value="<?PHP echo $GLOBALS["url"];?>">
<!-- added the following line -->
<input type=hidden name=url_of_referring_page value="<?=$url_of_referring_page?>">
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr align="left" valign="top">
<td width="50%"> Your name: </td>
<td>
<input type="text" name="form[from]" size="30">
</td>
</tr>
<tr align="left" valign="top">
<td> <small>*</small> Your e-mail: </td>
<td>
<input type="text" name="form[from_email]" maxlength="40" size="30">
</td>
</tr>
<tr align="left" valign="top">
<td> <small>*</small> Friend's e-mail:</td>
<td>
<input type="text" name="form[to_email]" size="30">
</td>
</tr>
<tr align="left" valign="top">
<td> Short note about this page: </td>
<td>
<textarea name="form[comment]" rows="5" cols="30"></textarea>
</td>
</tr>
<tr align="left" valign="top">
<td>
<input type="submit" name="submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</td>
<td> </td>
</tr>
</table>
<p><small>* - required!</small></p>
</form>
</body>
</html>
<?
}
?>
[edited by: jatar_k at 10:51 pm (utc) on Mar. 19, 2003]
[edit reason] sidescroll [/edit]
I have been trying this for some time now, I did not get it to work with the sessions as mentioned above. Thanks to all of you that helped especially rewboss and toadhall. I will keep trying the ternary operators and sessions as this is something I need to get to grips with.
I did come up with another solution that for those that are intersted.
Append the url of the linking page to the link by using :
$url = $HTTP_SERVER_VARS["HTTP_HOST"] . $HTTP_SERVER_VARS["REQUEST_URI"]
and then adding the $url to the href
<a href="page.php?url=<?php print ("$url") ?>" target="_blank ">Email link </a>
Then use the original code above.
Cheers
The problem stems from here (in the top section of the code):
if($url_of_referring_page != $PHP_SELF){
$url_of_referring_page = $HTTP_REFERER;
}
Two parter:
1) Variable scope. You need to set the $url_of_referring_page variable as global too.
2) As it stands the conditional will return the url of the mailto script each time the form is submitted ($url_of_referring_page would not equal the url of the script therefore $url_of_referring_page would now become $HTTP_REFERER, the url of the script).
Use this (if I may be so bold):
if (empty($GLOBALS["url_of_referring_page"])){
$GLOBALS["url_of_referring_page"] = $GLOBALS["url"];
}
...and this new hidden field in your form (substitute it for the one there):
<input type=hidden name=url_of_referring_page value="<?=$GLOBALS["url_of_referring_page"]?>">
...at line 97 you'll need to change the variable reference:
$message="Hi!\n\n$from ($from_email) invited you to visit $site_name!\nCheck out this URL:" . $GLOBALS["url_of_referring_page"];
check_mail() function errors:
If you've copied this script and pasted it you may have bad pipes. Sounds gruesome. Often a copied pipe will, when pasted, end up as two short lines in a vertical stack rather than one unbroken pipe, ¦ (bad pipe). Pipes are used in your check_mail() function and in a conditional statement just below it. In the function they appear between curly braces thus {¦}, and in the conditional they're used as a logical OR (¦¦). Just overwrite them with fresh pipes from your keyboard. Otherwise the function throws errors. (There's another one in the javascript of your error() function, but it's not crucial).
T