I am trying to get the URL of the site which referred the user to the site with the script on it.
Here is the script I am using:
if (isset($_SERVER['HTTP_REFERER'])) {
$ref_url = $_SERVER['HTTP_REFERER']; //get referrer
}else{
$ref_url = 'none'; // show failure message
}
echo $ref_url;
The issue I am having is that no matter what site I come to the main site from, I always get 'none' echoed. What is causing this and how can I make the script actually get the referrers URL?
Related
I need a code to display referrer URL (full URL if possible, like example.com/1234). if full URL is not possible, then at least the domain is fine. Also, I want to see if the referrer is NOT coming from somewhere for example: if referer is NOT example2.com then //do action
I have a code but it's not working properly. If you can help, that would be great. Thanks
<?php
// Check if Referral URL exists
if (isset($_SERVER['HTTP_REFERER'])) {
// Store Referral URL in a variable
$refURL = $_SERVER['HTTP_REFERER'];
// Display the Referral URL on web page
echo $refURL;
} else {
if (( $refURL == !'https://example2.com' ) ) {
// sorry, you didnt come from example2.com, you NEED to come from example2.com}
?>
This doesnt seem to work. Also, http refer only displays domain of referrer without path like example.com/1234/321. If its possible to get the full URL please share, otherwise i can do without it.
I'm trying to receive the referrer URL. If an external website has a link to my website and someone clicks on that link, I want to receive (and save) that external website's URL. After clicks on my website, I still need that same referrer URL.
I've tried the following code:
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
$refURL = $_SERVER['HTTP_REFERER'];
echo $refURL;
} else {
echo "No referer URL";
}
?>
But if you click on the link on the external website, it doesn't show anything.
If I click on a link on my website, I always get the previous page.
It seems that I don't get the URL if I'm redirected from an external website.
I'm using the following code to restrict traffic to a web site unless it was clicked on from the referral domain. For some reason it doesn't work consistently. Sometimes it restricts the site even though the referral is correct. Have no idea why!
<?php
// This is to check if the request is coming from a specific domain
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] !== 'allowed-domain.com') {
// Output string and stop execution
die("Access Restricted. Internal Use Only.");
}
echo "";
?>
I'm trying to blocked website based proxy's. These are normally in the format of:
http://3.hidemyass.com/ip-8/encoded/Oi8vZ29kbGV5ZGVzaWduLmNvLnVrL0xDcmVkaXJlY3QvZnVuY3Rpb25zL2Z1bmN0aW9
My theory of blocking these is to get the URL of the address bar and check that it's actually direct access to my site, rather than visiting via a website proxy.
However, when i try to visit my site and attempt to capture the url of the user it still reports that its my sites url.. not this web based proxy one.
I've tried the following ways of detecting it:
$url= $_SERVER['HTTP_HOST']; //get the url
$url = $_SERVER["SERVER_NAME"];
any ideas on how to resolve this?
UPDATE
Ok i've rewrote part of this, however it always seems to be returning false... the $url is being passed correct as i can echo this out within the function. However it doesnt seem to be matching and returning false
<script>
var url = window.location.href;
<?php $url = "<script>document.write(url)</script>"; ?>
</script>
<?php
//
function checkURLIsSafe($url){
if(preg_match('/www/',$url)){
echo 'true';
} else {
echo 'false';
}
}
checkURLIsSafe($url);
?>
PHP runs on the server. It can only see the URL that was requested from it.
hidemyass.com will be requesting the normal URL from your server. There is no way to tell what URL the browser requested from hidemyass.com.
Approaches you could take include:
Checking the source ip against a list of known proxies
Using client-side JavaScript to read location.href
You cant do it with PHP only. What you can do is to check window.location.href with javascript, and if it's incorrect, send ajax request to server, which will block IP address.
Let say we've the following
Objective : User will post certain exact URL $refere to lock viewing text content and only be allowed for view if the viwer is coming from the same exact URL $refere.
$refere = "http://www.site_site.com"; // User will post it
$r = $_SERVER['HTTP_REFERER']; // To get real referral
and i want to do the following
<?PHP
if(stripos($r, $refere) == false){
echo "Wrong";
} else { ?>
echo "Go";
}
?>
It always gives me $r = $_SERVER['HTTP_REFERER']; blank ! so does it deprecated on any PHP version 4 or 5 whatever !
Also
what is the user posted $refere like https:// or missed www. or only posted site_site.com while the $r = $_SERVER['HTTP_REFERER']; showing www.site_site.com
so can anyone help me to adjust this code to be working fine no matter the user posted the $refere link fully or only site_site.com.
The $_SERVER['REFERER'] variable will only be set when you click a link to your page from another page and if the browser (or an eventual proxy or firewall you're on) isn't removing the referer header.
To your second question: do some string comparisons. The functions strpos() and substr() will be of great help.