No $HTTP_REFERER at first time visit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

No $HTTP_REFERER at first time visit

Post by salto »

I have again a question on the printer friendly version script, earlier discussed in this forum at Sat Jul 13, 2002 10:27 pm
http://www.devnetwork.net/forums/viewto ... highlight=

The script needs the $HTTP_REFERER to function properly

Code: Select all

$file = substr($HTTP_REFERER, strrpos($HTTP_REFERER,"/")+1);
$fd= fread(fopen($file, "r"), 100000);
This works only when there is a referrer. Anyone coming first time to a page has no referer and in that case the script gives an error.
< fopen("", "r") - Inappropriate ioctl for device > and < not a valid File-Handle resource >
I have been looking for a workaround but either it is impossible or my knowledge is too limited (the latter is definitely sure).
Any help welcome. If there is no workaround the possibility to lead the visitor to a friendly errorpage with explanation would do as well.
I tried to accomplish the latter by

Code: Select all

if(!$HTTP_REFERER)&#123;
Header("Location: http://www.domain.com/error.htm");&#125;
but still get an error, I quess because I don't put this right.
Thanks for your attention
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

http_referer gets sent to the browser, so if you directly type in the URL there is no referer....

so i'd do something like:

if(empty($HTTP_REFERER)){
header("Location: freindly_url");
} else {
//open it
}

but, here is a script i wrote that you can feel free to use if you have mysql:

Code: Select all

<?

/* PHREF, CREATED BY DAN - HTTP://WWW.VEXDEV.COM/ */

/* RUN THIS QUERY IN MYSQL: 

CREATE TABLE phref (
   url varchar(255) NOT NULL,
   count int(5) DEFAULT '0' NOT NULL,
   time varchar(30) NOT NULL,
   UNIQUE url (url)
);

*/

/* EDIT THESE */

$website = "yourdomain.com";
//YOUR DOMAIN, NO WWW. OR HTTP://

$server = "localhost";
//MYSQL SERVER

$username = "user";
//MYSQL USERNAME

$password = "password";
//MYSQL PASSWORD

$database = "db";
//MYSQL DATABASE

$days = "5";
// DAYS TO KEEP IN RECORD IF UNACTIVE

$num = "5";
// TOP # TO DISPLAY

$block = array("$website", 'bob.com', 'foo.com');
// BLOCKED URLS, SEPERATE THEM WITH COMMAS, DO NOT INCLUDE "http://" or "www."

/* STOP EDITING */

/* YOU'RE DONE, TO USE THIS YOU NEED TO HAVE A PHP FILE AND USE <? include('phref.php'); ?> */


mysql_connect("$server", "$username", "$password"); 
mysql_select_db("$database");

$c_time = time();


function checkblock($block_array, $url)&#123;
foreach($block_array as $key)&#123;
 if($key == $url)
  return TRUE;
&#125;
&#125;

if(!empty($HTTP_SERVER_VARS&#1111;'HTTP_REFERER']))&#123;
$ref = $HTTP_SERVER_VARS&#1111;'HTTP_REFERER'];
$ref = str_replace('http://www.', '', $ref);
$ref = str_replace('http://', '', $ref);
$ref = explode('/', $ref);
$ref = $ref&#1111;0];
if(!checkblock($block, $ref))&#123;
$qry = mysql_query("SELECT * FROM phref WHERE url = '$ref'");
 if(mysql_num_rows($qry) == "0")&#123;
mysql_query("INSERT INTO phref (url,time,count) values ('$ref','$c_time','1')");
&#125; else &#123;
mysql_query("UPDATE phref SET time = '$c_time' where url = '$ref'");
mysql_query("UPDATE phref SET count=count+1 where url = '$ref'");
&#125;



    $time = time()-(60*60*24*$days); 
    mysql_query("DELETE FROM phref WHERE time < $time"); 
&#125;
unset($qry);
unset($ref);
&#125;

$qry = mysql_query("SELECT url,count FROM phref group by url order by count desc limit $num");
echo "<table>";
echo "<tr><td>URL:</td><td>Hits in:</td></tr>";

while($row = mysql_fetch_array($qry, MYSQL_ASSOC))&#123;
echo "<tr><td>".$row&#1111;'url']."</td><td>".$row&#1111;'count']."</td></tr>";
&#125;

echo "</table>";

?>
salto
Forum Commoner
Posts: 31
Joined: Thu Jul 04, 2002 7:50 am

Post by salto »

hob_goblin, thank you!
That's it:

Code: Select all

if(empty($HTTP_REFERER))
empty is the keyword here to get it working and send visitors from direct typed URLs to a friendly error page. I was on the wrong track with

Code: Select all

if (!($HTTP_REFERER)) etc
)
Yes, I have mysql and in future I have to build the whole site in a DB but before that I have to learn a lot more. I keep your script until then.
Thanks very much, you've helped a lot!
Post Reply