简体   繁体   中英

only allow specific URL to access my sites

i need a code written in any language, which is secure and only allow a specific url to be mypage's refferer url. you can use passwords, cookies and anything you want but at last, the page only viewable when coming from a specific URL.(edited:)i-want-the-complete-code-to-copy-and-paste, then i will learn it slowly in the future by modifying and improving your provided code.

Try using HTTP_REFERER which return the address of the page which referred to the user agent. Example:

if ($_SERVER['HTTP_REFERER']  !== "ACCESSABLE_URL_HERE") {
    // do something you want
}

in PHP you can use the $_SERVER is a PHP super global variable which holds information about headers.

<?php
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];


$url = $_SERVER['HTTP_REFERER'];
$urlParse = parse_url($url);
if($urlParse['host'] !== 'test.com') 
{
   die("you have not permission to access");
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM