需要知道是否有人从我网站内部的 link 访问我的网站,而不是外部网站
Need to know if a person comes to my website from a link inside my website, not an external website
我需要知道是否有人从我网站内部的 link 访问我的网站,而不是外部网站。更准确地说,links 需要类似于
http://localhost/library/cat/5/fashion
http://www.mywebpage.com/library/cat/4/design
http://localhost/library/publication/7/pronto
http://www.mywebpage.com/library/publication/7/pronto
我如何确定 HTTP_REFERER 是否至少包含 'library/cat' 或 'library/publication',并且它是内部 link(来自我的实时主机或本地主机)?
这是我目前所做的
if ( isset($_SERVER['HTTP_REFERER') ) {
//if the referer contains something that resembles the above url's, add icon to go back
}
完成此操作的最佳方法是将随机令牌附加到您的内部 url 并检查接收 url 是否匹配。有很多方法可以做到这一点,除了将值附加到 url 之外,最常见的是将值存储在会话或数据库中,然后根据存储的令牌检查 url 令牌的值在接收端。
somelink.com?token=$yourRandomToken
试试这个...
$subject = $_SERVER['HTTP_REFERER');
$pattern1 = '/^http:\/\/([\w.]*)\/library\/cat\//';
$pattern2 = '/^http:\/\/([\w.]*)\/library\/publication\//';
if( 1 == preg_match($pattern1, $subject, $matches) or 1 == preg_match($pattern2, $subject, $matches) ){
// Do stuff
}
如果您需要知道它是本地主机还是 www.mywebpage.com 将在 $matches[0].
中
或者...
$subject = $_SERVER['HTTP_REFERER');
$pattern = '/^http:\/\/([\w.]*)\/(library\/cat|library\/publication)\//';
if( 1 == preg_match($pattern, $subject, $matches) ){
// Do stuff
}
其中 $matches[0]
是主机,$matches[1]
现在是 "library/cat" 或 "library/publication"。
看看preg_match and test the patterns out for yourself at a website like this。
实际上你的方法没问题,但请记住,TRASTING ANY EXTERNAL DATA 是不可靠的并且容易被黑客攻击。
使用HTTP_REFERER,这就是我要做的:
if ( !empty($_SERVER['HTTP_REFERER']) && strpos(strtolower($_SERVER['HTTP_REFERER']), '/library/cat/' ) !== false ) {
//if the referer contains something that resembles the above url's, add icon to go back
}
但是如果你真的想给用户一个"Go Back"按钮,你有更好的解决方案:
在 JS 中使用简单的 history(-1),这样可以将用户返回到他来自的搜索引擎:
function goBack() {
window.history.go(-1);
}
<button onclick="goBack()">Go Back</button>
你的第二个选择是依赖 Cookies,这有点复杂,但会给你主要思想
if (empty($_SESSION['current_page'])){
// This is our first page in this website!
$_SESSION['current_page'] = $_SERVER['REQUEST_URI'];
} else {
// This is our second+ page in this website, we have a back button
$_SESSION['prev_page'] = $_SESSION['current_page'];
$_SESSION['current_page'] = $_SERVER['REQUEST_URI'];
}
现在你可以玩这个例子了,你可以把它做成一个数组来拥有更多的历史事件等等。
我需要知道是否有人从我网站内部的 link 访问我的网站,而不是外部网站。更准确地说,links 需要类似于
http://localhost/library/cat/5/fashion
http://www.mywebpage.com/library/cat/4/design
http://localhost/library/publication/7/pronto
http://www.mywebpage.com/library/publication/7/pronto
我如何确定 HTTP_REFERER 是否至少包含 'library/cat' 或 'library/publication',并且它是内部 link(来自我的实时主机或本地主机)?
这是我目前所做的
if ( isset($_SERVER['HTTP_REFERER') ) {
//if the referer contains something that resembles the above url's, add icon to go back
}
完成此操作的最佳方法是将随机令牌附加到您的内部 url 并检查接收 url 是否匹配。有很多方法可以做到这一点,除了将值附加到 url 之外,最常见的是将值存储在会话或数据库中,然后根据存储的令牌检查 url 令牌的值在接收端。
somelink.com?token=$yourRandomToken
试试这个...
$subject = $_SERVER['HTTP_REFERER');
$pattern1 = '/^http:\/\/([\w.]*)\/library\/cat\//';
$pattern2 = '/^http:\/\/([\w.]*)\/library\/publication\//';
if( 1 == preg_match($pattern1, $subject, $matches) or 1 == preg_match($pattern2, $subject, $matches) ){
// Do stuff
}
如果您需要知道它是本地主机还是 www.mywebpage.com 将在 $matches[0].
中或者...
$subject = $_SERVER['HTTP_REFERER');
$pattern = '/^http:\/\/([\w.]*)\/(library\/cat|library\/publication)\//';
if( 1 == preg_match($pattern, $subject, $matches) ){
// Do stuff
}
其中 $matches[0]
是主机,$matches[1]
现在是 "library/cat" 或 "library/publication"。
看看preg_match and test the patterns out for yourself at a website like this。
实际上你的方法没问题,但请记住,TRASTING ANY EXTERNAL DATA 是不可靠的并且容易被黑客攻击。
使用HTTP_REFERER,这就是我要做的:
if ( !empty($_SERVER['HTTP_REFERER']) && strpos(strtolower($_SERVER['HTTP_REFERER']), '/library/cat/' ) !== false ) {
//if the referer contains something that resembles the above url's, add icon to go back
}
但是如果你真的想给用户一个"Go Back"按钮,你有更好的解决方案:
在 JS 中使用简单的 history(-1),这样可以将用户返回到他来自的搜索引擎:
function goBack() {
window.history.go(-1);
}
<button onclick="goBack()">Go Back</button>
你的第二个选择是依赖 Cookies,这有点复杂,但会给你主要思想
if (empty($_SESSION['current_page'])){
// This is our first page in this website!
$_SESSION['current_page'] = $_SERVER['REQUEST_URI'];
} else {
// This is our second+ page in this website, we have a back button
$_SESSION['prev_page'] = $_SESSION['current_page'];
$_SESSION['current_page'] = $_SERVER['REQUEST_URI'];
}
现在你可以玩这个例子了,你可以把它做成一个数组来拥有更多的历史事件等等。