Web Hosting Talk







View Full Version : Access file only through specific website?


Xiety
09-28-2008, 05:44 PM
I have an html file which I want to close to public access and make it available only through clicking on a link on another specific page.

So for example:

Page in question: www.x.com/1.html or php

I want to make sure people cannot just type in the address for it and view it but rather I want them to go to www.x.com/2.html and click on <a href="www.x.com/1.html>Click</a> to view it.

I guess I could do prohibit hotlinking but what I am looking for is to disable viewing the page by typing the URL on address bar.

Is this possible? Thanks :):confused:

bbowers
09-28-2008, 05:54 PM
In your php file, try something like this:
<?php
define('ALLOWED_REFERRER', 'your allowed referrer url goes here');

if (ALLOWED_REFERRER !== ''
&& (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false)
) {
die("Internal server error. Please contact system administrator.");
}

//REST OF CODE FOLLOWS THIS LINE

?>

Assuming the referrer is not spoofed, the visitor to the page will have to have been referred to the page by the url specified in the config above, otherwise the Internal error defined above will be displayed.

Xiety
09-28-2008, 06:57 PM
Thanks bbowers. I tried that but it gave me a parse error on line 7, which is the die command. Any ideas? Thanks again.

Parse error: syntax error, unexpected T_STRING in /home/yasiyoru/public_html/extrafiles/TurkceFormlar/TurkceForm-2010T.php on line 7

ThatScriptGuy
09-28-2008, 07:21 PM
You can do this by checking the referring page, although it can be easily spoofed by those with the know-how:

<?php
define('ALLOWED_REFERRER', 'your allowed referrer url goes here');

if (ALLOWED_REFERRER !== '' && (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false))
{
die("Internal server error. Please contact system administrator.");
}

//REST OF CODE FOLLOWS THIS LINE

?>

Xiety
09-28-2008, 07:37 PM
Thanks Kevin but as I already said in my previous post, that code gives me a php parse error. Here`s the code of my page:

<?php
define('ALLOWED_REFERRER', 'http://www.yasiyorum.com/index.php/yesilkart/35-yk-dv2010/57-yk-turkce-form');
if (ALLOWED_REFERRER !== '' && (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false))
{
die("Internal server error. Please contact system administrator.");
}
<head>
<title>Elektronik Cesitlilik</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="KEYWORDS"
content="education, undergraduate, masters, PhD, postgraduate, post doc, assistantship, scholarship, grant, school, college, university, sert">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<link href="edv.css" type="text/css" rel="stylesheet">
<meta content="-1" http-equiv="EXPIRES">
</head>
<body leftmargin="0" topmargin="0" style="background-color: white;"
marginheight="0" marginwidth="0">
</body>
?>

ThatScriptGuy
09-28-2008, 07:40 PM
You need to remember to put '?>' after the PHP section. IE,

<?php
define('ALLOWED_REFERRER', 'http://www.yasiyorum.com/index.php/yesilkart/35-yk-dv2010/57-yk-turkce-form');
if (ALLOWED_REFERRER !== '' && (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false))
{
die("Internal server error. Please contact system administrator.");
}
?>
<head>
<title>Elektronik Cesitlilik</title>

Also, for future reference, it is very helpful to know the "Unexpected..." phrasing that usually accompanies a parse error.

Xiety
09-28-2008, 08:29 PM
Thank you Kevin. I had put the '?>' part at the very end of html. Oh, and I had already posted the unexpected part of the parse error on my second message in this thread :) Thanks again!

ThatScriptGuy
09-28-2008, 08:30 PM
Glad we could help.

bbowers
09-28-2008, 08:35 PM
Thanks bbowers. I tried that but it gave me a parse error on line 7, which is the die command. Any ideas? Thanks again.

Parse error: syntax error, unexpected T_STRING in /home/yasiyoru/public_html/extrafiles/TurkceFormlar/TurkceForm-2010T.php on line 7

sorry, did you say it was solved and working now?

ThatScriptGuy
09-28-2008, 09:15 PM
His code is working fine now - The closing PHP ?> tag was in the wrong spot before.

Xiety
09-29-2008, 06:52 AM
Yes solved. Thank you both.

rmaldeney
08-27-2009, 01:24 PM
I have used the attached code and it works great for one of my sites. But I have one that comes from a https://my.site.com. Can this code be edited to work for my needs?



In your php file, try something like this:
<?php
define('ALLOWED_REFERRER', 'your allowed referrer url goes here');

if (ALLOWED_REFERRER !== ''
&& (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false)
) {
die("Internal server error. Please contact system administrator.");
}

//REST OF CODE FOLLOWS THIS LINE

?>

Assuming the referrer is not spoofed, the visitor to the page will have to have been referred to the page by the url specified in the config above, otherwise the Internal error defined above will be displayed.